Home  >  Q&A  >  body text

pygame - Perform different operations in response to the same event the second time in python

In a loop, the system responds to the pressing of a key on the keyboard, then performs two operations, and then responds to the pressing of the same key on the keyboard again, but only performs the second operation. What should be done?

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            sys.exit()
        elif event.key == pygame.K_RIGHT:
            tank.update1()
            tank.moving_right = True

For example, in the elif statement, starting from the second time, only tank.moving_right = True will be executed instead of tank.update1()
How to deal with it

阿神阿神2681 days ago561

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-05-18 11:00:36

    Add a switch sign inside the tank

    elif event.key == pygame.K_RIGHT:
                tank.update1()
                tank.moving_right = True
            tank.update1()
            tank.moving_right = True
            这里封装一下,然后在tank里加成员变量

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-18 11:00:36

    Add a flag variable

    flag = True
    for event in ....:
        ...
        if flag:
            tank.update1()
            flag = False
        tank.moving_right = True

    reply
    0
  • Cancelreply