Pygame倒计时器

时间:2015-06-08 23:24:38

标签: python pygame

我开始使用pygame,我想做简单的游戏。我需要的其中一个元素是倒数计时器。 如何在PyGame中进行倒计时时间(例如10秒)?

7 个答案:

答案 0 :(得分:7)

在此页面上,您可以找到您要找的内容http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
你可以在开始倒计时之前下载一次滴答声(这可能是游戏中的触发器 - 关键事件,无论如何)。 例如:

start_ticks=pygame.time.get_ticks() #starter tick
while mainloop: # mainloop
    seconds=(pygame.time.get_ticks()-start_ticks)/1000 #calculate how many seconds
    if seconds>10: # if more than 10 seconds close the game
        break
    print (seconds) #print how many seconds

答案 1 :(得分:6)

另一个简单的方法是简单地使用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)

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

enter image description here

答案 2 :(得分:4)

pygame.time.Clock.tick返回自上次clock.tick调用(delta timedt)以来的时间(以毫秒为单位),因此您可以使用它来增加或减少计时器变量。

import pygame as pg


def 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()

答案 3 :(得分:1)

有几种方法可以做到这一点 - 这是一个。据我所知,Python没有中断机制。

import time, datetime

timer_stop = datetime.datetime.utcnow() +datetime.timedelta(seconds=10)
while True:
    if datetime.datetime.utcnow() > timer_stop:
        print "timer complete"
        break

答案 4 :(得分:1)

在pygame中存在一个计时器事件。使用pygame.time.set_timer()重复创建USEREVENT。例如:

timer_interval = 500 # 0.5 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event , timer_interval)

注意,可以在pygame中定义客户事件。每个事件都需要一个唯一的ID。用户事件的ID必须在pygame.USEREVENT(24)和pygame.NUMEVENTS(32)之间。在这种情况下,pygame.USEREVENT+1是计时器事件的事件ID。
要为事件禁用计时器,请将毫秒参数设置为0。

在事件循环中接收事件:

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

         elif event.type == timer_event:
             # [...]

可以通过将0传递给time参数来停止计时器事件。


查看示例:

import pygame

pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 10
text = font.render(str(counter), True, (0, 128, 0))

timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, 1000)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == timer_event:
            counter -= 1
            text = font.render(str(counter), True, (0, 128, 0))
            if counter == 0:
                pygame.time.set_timer(timer_event, 0)                

    window.fill((255, 255, 255))
    text_rect = text.get_rect(center = window.get_rect().center)
    window.blit(text, text_rect)
    pygame.display.flip()

答案 5 :(得分:0)

有很多方法可以做到这一点,

import pygame,time, sys
from pygame.locals import*
pygame.init()
screen_size = (400,400)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("timer")
time_left = 90 #duration of the timer in seconds
crashed  = False
font = pygame.font.SysFont("Somic Sans MS", 30)
color = (255, 255, 255)

while not crashed:
    for event in pygame.event.get():
        if event.type == QUIT:
            crashed = True
    total_mins = time_left//60 # minutes left
    total_sec = time_left-(60*(total_mins)) #seconds left
    time_left -= 1
    if time_left > -1:
        text = font.render(("Time left: "+str(total_mins)+":"+str(total_sec)), True, color)
        screen.blit(text, (200, 200))
        pygame.display.flip()
        screen.fill((20,20,20))
        time.sleep(1)#making the time interval of the loop 1sec
    else:
        text = font.render("Time Over!!", True, color)
        screen.blit(text, (200, 200))
        pygame.display.flip()
        screen.fill((20,20,20))




pygame.quit()
sys.exit()

答案 6 :(得分:-1)

这实际上非常简单。感谢Pygame创建一个简单的库!

import pygame
x=0
while x < 10:
    x+=1
    pygame.time.delay(1000)

这就是它的全部!享受pygame的乐趣!