pygame - Perform different operations in response to the same event the second time in python
阿神
阿神 2017-05-18 10:58:36
0
2
560

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

阿神
阿神

闭关修行中......

reply all(2)
漂亮男人

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里加成员变量
仅有的幸福

Add a flag variable

flag = True
for event in ....:
    ...
    if flag:
        tank.update1()
        flag = False
    tank.moving_right = True
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!