Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当按键时,pygame继续循环_Python_Pygame - Fatal编程技术网

Python 当按键时,pygame继续循环

Python 当按键时,pygame继续循环,python,pygame,Python,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(to

当我们点击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时,
才会增加

有没有一种方法可以让我在单击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
    ...