您没有显示代码,但我猜您使用pygame.mouse.get_pressed(),当您按住按钮时,它始终提供True。这可能是您的问题。
你可以做以下两件事之一:
使用仅创建一次的event.MOUSEBUTTONDOWN - 当按钮将状态从not-pressed 更改为pressed 时。
或者:
使用额外的变量,它将从前一帧中记住 pygame.mouse.get_pressed()。然后比较是否按下了现在按钮但在前一帧中没有按下然后将元素添加到列表中。
编辑: 来自不同问题的旧代码,使用event.MOUSEBUTTONDOWN 更改颜色。
#!/usr/bin/env python
# http://stackoverflow.com/questions/33856739/how-to-cycle-3-images-on-a-rect-button
import pygame
# - init -
pygame.init()
screen = pygame.display.set_mode((300,200))
# - objects -
# create three images with different colors
images = [
pygame.Surface((100,100)),
pygame.Surface((100,100)),
pygame.Surface((100,100)),
]
images[0].fill((255,0,0))
images[1].fill((0,255,0))
images[2].fill((0,0,255))
images_rect = images[0].get_rect()
# choose first image
index = 0
# - mainloop -
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and images_rect.collidepoint(event.pos):
# cycle index
index = (index+1) % 3
# - draws -
screen.blit(images[index], images_rect)
pygame.display.flip()
# - end -
pygame.quit()
GitHub:furas/python-examples/pygame/button-click-cycle-color