所以我想在pygame中做一些非常基本的事情。这是我使用它的前几天所以我是初学者。每当我用鼠标按下它时,我都试图改变某些东西的颜色。我知道如何通过计时改变颜色,这就是我下面的代码。我正在尝试在下面的代码中更改云的颜色,如果你运行它,你会看到云是左上角我让它每三秒钟在白色和黑色之间切换但我希望它根据鼠标按钮改变。感谢
import pygame, time, sys
from pygame.locals import *
def drawItem(windowSurface, x, y):
pygame.draw.polygon(windowSurface, RED, ((0+x, 100+y),(100+x, 100+y), (50+x, 50+y)))
pygame.draw.polygon(windowSurface, GREEN, ((0+x,100+y),(100+x,100+y),(100+x,200+y),(0+x,200+y)))
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Lab 9")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
GRASS = (26, 82, 26)
SKY = (179,237,255)
color = SKY
flag = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
windowSurface.fill(SKY)
drawItem(windowSurface,200,120)
pygame.draw.rect(windowSurface, GRASS, (0,300,500,500),0)
house = ((0+50, 100+50),(100+50, 100+50), (50+50, 50+50), (50+100, 50+100))
for i in range(3):
pygame.draw.circle(windowSurface,color, house[i], 80)
if flag == False:
color = WHITE
flag = True
elif flag == True:
color = BLACK
flag = False
pygame.display.update()
time.sleep(3)
答案 0 :(得分:1)
您已经发现了如何测试事件的类型(检查是否event.type == QUIT
)。
您可以对此进行扩展以检查是否是鼠标按钮单击。将其粘贴在for event in pygame.event.get()
循环中:
if event.type == MOUSEBUTTONDOWN:
flag = not flag # This will swap the value of the flag
然后摆脱下面的flag = True
和flag = False
行,因为你不再需要它们了。还要摆脱time.sleep()调用;或至少将其改为合理的帧速率(如time.sleep(0.2)
=每秒50帧)。