每按一次键,我怎么算?

时间:2017-07-25 20:59:33

标签: python pygame

每当我点击左键时,我想计算我的代码看起来像这样

    import pyautogui
    import pygame
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_LEFT:
                print (sequentially the amount of times i've hit left)

2 个答案:

答案 0 :(得分:3)

只需将数据存储在变量中并递增:

import pyautogui
import pygame
Number_of_Presses = 0
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
       if event.key == pygame.K_LEFT:
            Number_of_Presses += 1
            print (Number_of_Presses)

应该工作得很好。

答案 1 :(得分:2)

创建一个变量来存储按下按键的次数:

import pygame
left=0

for event in pygame.event.get():
    if event.type==pygame.KEYDOWN:
        if event.key==pygame.K_LEFT:
            left+=1
print(left)

注意:您可能希望将事件循环放在while循环中,以便它会运行多次。