【发布时间】:2021-04-08 11:59:58
【问题描述】:
大家好!我想用 pygame 创建秒表。我看到了这个代码:
import pygame as pg
pg.init()
screen = pg.display.set_mode((400, 650))
clock = pg.time.Clock()
font = pg.font.Font(None, 54)
font_color = pg.Color('springgreen')
passed_time = 0
timer_started = False
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
timer_started = not timer_started
if timer_started:
start_time = pg.time.get_ticks()
if timer_started:
passed_time = pg.time.get_ticks() - start_time
screen.fill((30, 30, 30))
text = font.render(str(passed_time / 1000), True, font_color)
screen.blit(text, (50, 50))
pg.display.flip()
clock.tick(30)
pg.quit()
但是当您按下空格键时,计时器会重新启动。如何打印(在屏幕上)?
【问题讨论】:
-
这段代码很好,运行良好。当您按两次空格键时,计时器会重新启动。你想“在屏幕上打印”什么?
-
我猜你想保存结果并将它们打印在屏幕上。为此,您每次停止秒表时都需要保存结果
标签: python python-3.x pygame