簡體   English   中英

pygame 按鍵按下時繼續循環

[英]pygame continue loop when key pressed

我試圖讓我的計時器在我們單擊 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
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM