Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame中的倒计时_Python_Pygame - Fatal编程技术网

Python Pygame中的倒计时

Python Pygame中的倒计时,python,pygame,Python,Pygame,我开始使用pygame,我想做一个简单的游戏。我需要的元素之一是倒计时。 如何在PyGame中进行倒计时(如10秒)?在此页面上,您将找到您要查找的内容 你在开始倒计时之前下载一次滴答声(这可能是游戏中的一个触发器——关键事件,等等)。 例如: 有几种方法可以做到这一点——这里有一种。据我所知,Python没有中断机制 import time, datetime timer_stop = datetime.datetime.utcnow() +datetime.timedelta(seco

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

在此页面上,您将找到您要查找的内容
你在开始倒计时之前下载一次滴答声(这可能是游戏中的一个触发器——关键事件,等等)。 例如:


有几种方法可以做到这一点——这里有一种。据我所知,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

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

这其实很简单。感谢Pygame创建了一个简单的库

import pygame
x=0
while x < 10:
    x+=1
    pygame.time.delay(1000)
导入pygame
x=0
当x<10时:
x+=1
pygame.时间延迟(1000)
就这些!祝你玩得开心

返回自上次
时钟以来的时间(毫秒)。勾选
调用(,
dt
),因此您可以使用它增加或减少计时器变量

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()
将pygame导入为pg
def main():
第init页()
屏幕=pg.display.set_模式((640480))
font=pg.font.font(无,40)
灰色=pg.Color('gray19')
蓝色=pg.Color('dodgerblue')
#时钟用于限制帧速率
#并返回自上次滴答声以来的时间。
时钟=pg.time.clock()
定时器=10#减小此值以倒计时。
dt=0#增量时间(自上次勾选以来的时间)。
完成=错误
虽然没有这样做:
对于pg.event.get()中的事件:
如果event.type==pg.QUIT:
完成=正确
定时器-=dt

如果定时器有很多方法可以做到这一点,这是其中之一

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

pygame中存在一个计时器事件。用于重复创建一个。e、 g:

timer_interval=500#0.5秒
timer\u event=pygame.USEREVENT+1
pygame.time.set_计时器(计时器事件、计时器间隔)
注意,在pygame中可以定义客户事件。每个事件都需要一个唯一的id。用户事件的id必须介于
pygame.USEREVENT
(24)和
pygame.NUMEVENTS
(32)之间。在本例中,
pygame.USEREVENT+1
是计时器事件的事件id。
要禁用事件的计时器,请将毫秒参数设置为0

在事件循环中接收事件:

running=True
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
elif event.type==计时器事件:
# [...]
通过将0传递给time参数,可以停止计时器事件


请参见示例:

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)

导入pygame
pygame.init()
window=pygame.display.set_模式((200200))
clock=pygame.time.clock()
font=pygame.font.SysFont(无,100)
计数器=10
text=font.render(str(计数器),True,(0,128,0))
timer\u event=pygame.USEREVENT+1
pygame.time.set_计时器(计时器事件,1000)
运行=真
运行时:
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
elif event.type==计时器事件:
计数器-=1
text=font.render(str(计数器),True,(0,128,0))
如果计数器==0:
pygame.time.set_计时器(计时器事件,0)
窗口填充((255,255,255))
text_-rect=text.get_-rect(中心=window.get_-rect().center)
blit(text,text_rect)
pygame.display.flip()

delay对某些场景不起作用,因为它不会延迟特定对象,而是整个游戏。Hmmmmm。我想知道是否有可能把它放在一个函数中。