PyGame: 每次移动鼠标时计时器重置
PyGame: Timer resets every time the mouse is moved
我目前正在编写一个需要计时器的游戏。我写了一些看起来可以工作的代码,因为它以秒为单位向上计数,但是当我移动鼠标时,计时器重置回零并再次开始计数。显然,这不是我想要发生的事情,我试图弄清楚为什么会发生这种情况,而不是连续计数,但我无法解决。
我的代码是:
passed_time = 0
timer_started = False
(^while 循环外)
(/ 在 while 循环内)
for event in ev:
if event.type == pygame.QUIT:
is_alive = False
if start_clicked:
timer_started = True
if timer_started:
start_time = pygame.time.get_ticks()
if won:
timer_started = False
if timer_started:
passed_time = pygame.time.get_ticks() - start_time
text = font.render(str(passed_time / 1000), True, font_color)
screen.blit(text, (20, 450))
以及将 [start_clicked] 或 [won] 设置为 True 的鼠标事件点击了。
根据我在 PyGame 中对计时器的基本了解,一旦 start_clicked 设置为 true,这应该会继续计数,但是每次都会重置鼠标被移动,这完全破坏了基于鼠标移动的回避游戏中计时器的意义。我能做些什么来解决这个问题并使其持续 运行,直到 won 变量为真?
如果出于某种原因我没有提供足够的信息来解决问题,the full code is here:
通过查看您的完整代码,您的事件循环是错误的,因为您的身份不正确。
目前事件循环中有 if event.type == pygame.QUIT:
,其他所有 if event.type == whatever
都在事件循环之外,因为它们缩进不够。
所有检查事件的 if
s 必须在事件循环内,而与事件循环无关的 if
s(如 if start_clicked:
)必须在外部,通常在之后,事件循环。
我无法修复您的所有代码,但修复事件循环肯定会有所帮助。
我目前正在编写一个需要计时器的游戏。我写了一些看起来可以工作的代码,因为它以秒为单位向上计数,但是当我移动鼠标时,计时器重置回零并再次开始计数。显然,这不是我想要发生的事情,我试图弄清楚为什么会发生这种情况,而不是连续计数,但我无法解决。
我的代码是:
passed_time = 0
timer_started = False
(^while 循环外)
(/ 在 while 循环内)
for event in ev:
if event.type == pygame.QUIT:
is_alive = False
if start_clicked:
timer_started = True
if timer_started:
start_time = pygame.time.get_ticks()
if won:
timer_started = False
if timer_started:
passed_time = pygame.time.get_ticks() - start_time
text = font.render(str(passed_time / 1000), True, font_color)
screen.blit(text, (20, 450))
以及将 [start_clicked] 或 [won] 设置为 True 的鼠标事件点击了。
根据我在 PyGame 中对计时器的基本了解,一旦 start_clicked 设置为 true,这应该会继续计数,但是每次都会重置鼠标被移动,这完全破坏了基于鼠标移动的回避游戏中计时器的意义。我能做些什么来解决这个问题并使其持续 运行,直到 won 变量为真?
如果出于某种原因我没有提供足够的信息来解决问题,the full code is here:
通过查看您的完整代码,您的事件循环是错误的,因为您的身份不正确。
目前事件循环中有 if event.type == pygame.QUIT:
,其他所有 if event.type == whatever
都在事件循环之外,因为它们缩进不够。
所有检查事件的 if
s 必须在事件循环内,而与事件循环无关的 if
s(如 if start_clicked:
)必须在外部,通常在之后,事件循环。
我无法修复您的所有代码,但修复事件循环肯定会有所帮助。