微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

按下键时pygame继续循环

如何解决按下键时pygame继续循环

我试图让我的计时器在我们点击 e 时继续添加,但我不知道为什么我必须按住 e 才能让计时器继续添加我的计时器名称是(吨)有没有办法我可以继续添加我的当我们点击 e 而不是在我们不再点击 e 时停止我尝试了 'if event.type == pygame.K_e' 但它与我必须保持 e 的事情相同

        if keys[pygame.K_e]: # if we click e then it should keep adding tons
            tons += 1
            print(tons)

游戏循环 V

run = True
while run:
    # Making game run with fps
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    
# telling what to do when we say the word 'key'
    keys = pygame.key.get_pressed()

            
    if hit:
        if keys[pygame.K_e]: # if we click e then it should keep adding tons
            tons += 1
            print(tons)
            
        if tons > 10:
            playerman.direction = "att"
            if health2 > -21:
                health2 -= 0.3
            else:
                playerman.direction = "Idle"
                hit = False
                
            if tons > 30:
                tons = 0
                playerman.direction = "Idle"

解决方法

但我不知道为什么我必须按住 e 才能让计时器不断加起来

因为这就是你编码的方式。看看你的代码:

 if keys[pygame.K_e]: # if we click e then it should keep adding tons
    tons += 1
当且仅当按下 e

tons 才会递增。

有没有一种方法可以在我们点击 e 时继续添加我的计时器,而不是在我们不再点击 e 时停止

只需设置一个标志,就像这样:

pressed_e = False
run = True
while run:
    for event in pygame.event.get():
        ...
        if event.type == pygame.KEYDOWN and event.key == pygame.K_e:
            pressed_e = True

    if pressed_e: # if we click e then it should keep adding tons
        tons += 1
    ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。