在我的程序中,我希望用户能够按住一个按钮。松开按钮时,我希望打印出他们按住键的持续时间。我一直在尝试使用pygame时钟函数,但遇到了一些问题。该程序对于第一个按键操作运行良好,但在后续的按键操作中记录任何按键之间的停机时间。希望有所帮助,这是我的代码:
import pygame
from pygame.locals import *
def main():
key = 0
pygame.init()
self = pygame.time.Clock()
surface_sz = 480
main_surface = pygame.display.set_mode((surface_sz, surface_sz))
small_rect = (300, 200, 150, 90)
some_color = (255, 0, 0)
while True:
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
break;
elif ev.type == KEYUP:
if ev.key == K_SPACE: #Sets the key to be used
key += 1 #Updates counter for number of keypresses
while ev.type == KEYUP:
self.tick_busy_loop()
test = (self.get_time()/1000.0)
print "number: ", key, "duration: ", test
ev = pygame.event.poll()
main()