游戏中的倒计时器我开始使用游戏,我想做简单的游戏。我需要的元素之一是倒计时器。我如何做倒计时时间(如10秒)在派克游戏?
3 回答

狐的传说
TA贡献1804条经验 获得超3个赞
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)while True: 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: break else: screen.fill((255, 255, 255)) screen.blit(font.render(text, True, (0, 0, 0)), (32, 48)) pygame.display.flip() clock.tick(60) continue break

慕运维8079593
TA贡献1876条经验 获得超5个赞
pygame.time.Clock.tick
clock.tick
dt
import pygame as pgdef main(): pg.init() screen = pg.display.set_mode((640, 480)) font = pg.font.Font(None, 40) gray = pg.Color('gray19') blue = pg.Color('dodgerblue') # The clock is used to limit the frame rate # and returns the time since last tick. clock = pg.time.Clock() timer = 10 # Decrease this to count down. dt = 0 # Delta time (time since last tick). done = False while not done: for event in pg.event.get(): if event.type == pg.QUIT: done = True timer -= dt if timer <= 0: timer = 10 # Reset it to 10 or do something else. screen.fill(gray) txt = font.render(str(round(timer, 2)), True, blue) screen.blit(txt, (70, 70)) pg.display.flip() dt = clock.tick(30) / 1000 # / 1000 to convert to seconds.if __name__ == '__main__': main() pg.quit()
添加回答
举报
0/150
提交
取消