我试图通过在 while 循环中使用简单的秒表来测试鼠标按下和鼠标按下事件之间的时间。鼠标按下事件工作正常,但是当我释放鼠标以进行鼠标弹起时,秒数继续增加并且不会停止。
from pygame import *
import time
screen = display.set_mode((160, 90))
sec = 0
while True:
new_event = event.poll()
if new_event.type == MOUSEBUTTONDOWN:
while True: # Basic stopwatch started
time.sleep(1)
sec += 1
print(sec)
# In loop, when mouse button released,
# supposed to end stopwatch
if new_event.type == MOUSEBUTTONUP:
break
display.update()
我希望秒表在鼠标释放后结束。例如。如果只是单击鼠标,则秒数应为 1。如果按住鼠标 5 秒,则不应持续超过 5 秒。
Use pygame.time.get_ticks https://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks获取此后的毫秒数pygame.init()
被称为。
存储时的毫秒数MOUSEBUTTONDOWN
并计算主循环中的时间差:
from pygame import *
screen = display.set_mode((160, 90))
clock = time.Clock()
run = True
started = False
while run:
for new_event in event.get():
if new_event.type == QUIT:
run = False
if new_event.type == MOUSEBUTTONDOWN:
start_time = time.get_ticks()
started = True
if new_event.type == MOUSEBUTTONUP:
started = False
if started:
current_time = time.get_ticks()
sec = (current_time - start_time) / 1000.0
print(sec)
display.update()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)