我开始使用 pygame,我想做简单的游戏。我需要的元素之一是倒数计时器。我如何在 PyGame 中进行倒计时(例如 10 秒)?
原文由 adamo94 发布,翻译遵循 CC BY-SA 4.0 许可协议
我开始使用 pygame,我想做简单的游戏。我需要的元素之一是倒数计时器。我如何在 PyGame 中进行倒计时(例如 10 秒)?
原文由 adamo94 发布,翻译遵循 CC BY-SA 4.0 许可协议
另一种简单的方法是简单地使用 pygame 的事件系统。
这是一个简单的例子:
import pygame
pygame.init()
screen = pygame.display.set_mode((128, 128))
clock = pygame.time.Clock()
counter, text = 10, '10'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.USEREVENT:
counter -= 1
text = str(counter).rjust(3) if counter > 0 else 'boom!'
if e.type == pygame.QUIT:
run = False
screen.fill((255, 255, 255))
screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
pygame.display.flip()
clock.tick(60)
原文由 sloth 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答879 阅读✓ 已解决
1 回答1.8k 阅读✓ 已解决
在此页面上,您会找到所需的内容 http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
您在开始倒计时之前下载一次刻度(这可以是游戏中的触发器 - 关键事件,无论如何)。例如: