<p>您可以使用变量<code>previous_button</code>和<code>current_button</code>来记住最后两个按钮。然后你可以检查它们是否正确。你知道吗</p>
<p>它类似于@jsbueno solution,但我使用两个变量,而不是list。
如果您想检查较长的组合,那么您可以使用列表。你知道吗</p>
<pre><code>import pygame
# - functions -
def check_which_button_was_click(buttons, pos):
for name, (rect, color) in buttons.items():
if rect.collidepoint(pos):
return name
# - main -
# - init -
screen = pygame.display.set_mode((350, 150))
# - objects -
buttons = {
'RED': (pygame.Rect(50, 50, 50, 50), (255, 0, 0)),
'GREEN': (pygame.Rect(150, 50, 50, 50), (0, 255, 0)),
'BLUE': (pygame.Rect(250, 50, 50, 50), (0, 0, 255)),
}
previous_button = None
current_button = None
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - event -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
clicked_button = check_which_button_was_click(buttons, event.pos)
if clicked_button is not None:
previous_button = current_button
current_button = clicked_button
print(previous_button, current_button)
# - updates -
if previous_button == 'RED' and current_button == 'BLUE':
print('correct buttons clicked: RED & BLUE')
previous_button = None
current_button = None
# - draws -
screen.fill((0,0,0))
for rect, color in buttons.values():
pygame.draw.rect(screen, color, rect)
pygame.display.flip()
# - FPS -
clock.tick(5)
# - end -
pygame.quit()
</code></pre>