def box_button(x,y, colour, action=None):
toggle = False
pos = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if click[0] == 1 and (x + 10) + 30 > pos[0] > (x + 10) and (y + 10) + 30 > pos[1] > (y + 10):
if toggle == True:
toggle = False
colour = red
else:
toggle = True
colour = green
pygame.draw.rect(window, white, (x, y, 50, 50))
pygame.draw.rect(window, colour, (x + 10, y+10, 30, 30))
return colour有没有人能帮我把按钮从红色切换到绿色?每次我点击这个代码的按钮时,它就会变成绿色,但是当我松开点击的时候,它又变成了红色,我想让它永久保持绿色吗?谢谢
发布于 2020-01-13 05:07:04
您必须实现MOUSEBUTTONDOWN事件。
获取主应用程序循环中的事件列表:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
# [...]向box_button添加一个events参数,并将事件列表传递给函数:
box_button(events, .....)def box_button(events, x, y, colour, action=None):
toggle = False
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
button_rect = pygame.Rect(x + 10, y+10, 30, 30)
if event.button == 1 and button_rect.collidepoint(event.pos):
toggle = not toggle
colour = green if toggle else red
pygame.draw.rect(window, white, (x, y, 50, 50))
pygame.draw.rect(window, colour, (x + 10, y+10, 30, 30))
return colourhttps://stackoverflow.com/questions/59707817
复制相似问题