当我将鼠标悬停在按钮上时,它会改变颜色,但我还想在单击它时将数字添加到列表中。我得到它来添加数字,但我希望每次点击只发生一次,
def button():
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if mouse[0] < 300 and mouse[1] < 300:
topleft.color = (255, 0, 0)
if click[0]:
playerpattern.append(1)
else:
topleft.color = (100, 0, 0)
playerpattern = []
while playing:
print(str(playerpattern))
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
button()
win.fill((0, 0, 0))
pygame.display.update()
pygame.quit()
如果按住单击,它只会发送垃圾邮件 1。因此,如果您单击 1 秒的时间过长,则会添加两个 1。
最佳答案
这是@TheLazyScripter 代码的一个刺探...
def button_hover():
mouse = pygame.mouse.get_pos()
if mouse[0] < 300 and mouse[1] < 300:
topleft.color = (255, 0, 0)
else:
topleft.color = (100, 0, 0)
def clicky_button():
mouse = pygame.mouse.get_pos()
if mouse[0] < 300 and mouse[1] < 300:
playerpattern.append(1)
playerpattern = []
while playing:
print(str(playerpattern))
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
if event.type == pygame.MOUSEBUTTONDOWN:
clicky_button()
button_hover()
win.fill((0, 0, 0))
pygame.display.update()
pygame.quit()
本质上,您在 for
循环中检查多种事件类型。将悬停代码与点击代码隔离开来,因为您可以悬停而不点击。当你悬停时,改变颜色。单击时,执行单击代码。
关于python - 我怎样才能让每次点击发生一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572717/