Pygame显示更新故障?

2024-09-30 06:14:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些代码有点奇怪。基本上,变量gameStart是一个使用函数cursorOver的变量,cursorOver可以查找并检测鼠标按钮的位置以及是否按下鼠标键。我有3个按钮,我希望当光标在按钮上时每个按钮都变大。第一个按钮实现起作用。但是,如果我尝试添加另一个按钮,按钮会变大,但是,按钮开始闪烁。在

window.blit(background,(0,0))
window.blit(title,(175,200))
pygame.draw.rect(window,PURPLE,(50,400,200,100),0)
pygame.draw.rect(window,PURPLE,(300,400,200,100),0)
pygame.draw.rect(window,PURPLE,(550,400,200,100),0)
close()

mouseX,mouseY = pygame.mouse.get_pos()
mouseClick = pygame.mouse.get_pressed()[0]

gameStartC = cursorOver(50,400,200,100,mouseX,mouseY)
instructionStartC = cursorOver(300,400,200,100,mouseX,mouseY)
objectiveStartC = cursorOver(550,400,200,100,mouseX,mouseY)
nextStartC = cursorOver(580,250,175,100,mouseX,mouseY)

if gameStartC == True:
    while True:
        pygame.draw.rect(window,PURPLE,(25,375,250,150),0)
        pygame.display.update()
        break
else:
    pygame.draw.rect(window,PURPLE,(50,400,200,100),0)
    pygame.display.update()
#this is the part where the code becomes glitchy
if instructionStartC == True:
    while True:
        pygame.draw.rect(window,PURPLE,(275,375,250,150),0)
        pygame.display.update()
        break
else:
    pygame.draw.rect(window,PURPLE,(300,400,200,100),0)
    pygame.display.update()

The image is the menu screen that I want to impliment


Tags: recttruegetdisplayupdatewindow按钮pygame
1条回答
网友
1楼 · 发布于 2024-09-30 06:14:25

这只是因为您多次调用pygame.display.update()。在

您应该创建一个标准的游戏循环,通常执行以下三个操作:

  • 手柄输入
  • 更新状态
  • 绘制到屏幕

然后重复。在

在“Drawtoscreen”步骤中,您将所有的sprite/rect/whatever绘制到屏幕表面,然后最后调用pygame.display.update()。在

多次调用pygame.display.update(),不清除循环迭代之间的屏幕,以及创建多个不必要的事件循环,这些都是初学者常见的错误,这些错误会导致此类问题。在

所以在您的例子中,代码应该看起来更像这样:

if gameStartC:
    pygame.draw.rect(window,PURPLE,(25,375,250,150),0)
else:
    pygame.draw.rect(window,PURPLE,(50,400,200,100),0)

if instructionStartC:
    pygame.draw.rect(window,PURPLE,(275,375,250,150),0)
else:
    pygame.draw.rect(window,PURPLE,(300,400,200,100),0)

pygame.display.update()

我不知道您希望while-循环做什么,也许您应该使用pygamesRect和{}类。它会让你的生活更轻松。在

相关问题 更多 >

    热门问题