为了账号安全,请及时绑定邮箱和手机立即绑定

游戏中的倒计时器

游戏中的倒计时器

婷婷同学_ 2019-07-03 10:39:51
游戏中的倒计时器我开始使用游戏,我想做简单的游戏。我需要的元素之一是倒计时器。我如何做倒计时时间(如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


查看完整回答
反对 回复 2019-07-03
?
慕运维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()


查看完整回答
反对 回复 2019-07-03
  • 3 回答
  • 0 关注
  • 1466 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信