我有一些代码有点奇怪。基本上,变量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()
这只是因为您多次调用
pygame.display.update()
。在您应该创建一个标准的游戏循环,通常执行以下三个操作:
然后重复。在
在“Drawtoscreen”步骤中,您将所有的sprite/rect/whatever绘制到屏幕表面,然后最后调用
pygame.display.update()
。在多次调用
pygame.display.update()
,不清除循环迭代之间的屏幕,以及创建多个不必要的事件循环,这些都是初学者常见的错误,这些错误会导致此类问题。在所以在您的例子中,代码应该看起来更像这样:
我不知道您希望}类。它会让你的生活更轻松。在
while
-循环做什么,也许您应该使用pygamesRect
和{相关问题 更多 >
编程相关推荐