减速时间,但不是程序

时间:2016-05-23 22:09:57

标签: python pygame

我正在制作游戏,当玩家走出屏幕时,等级开始。我希望在游戏开始前显示“LEVEL 1”的图像,但程序显示的图像太快了。我的帧率为60.

我想知道是否有办法在屏幕blitting期间延迟约5秒的时间,但是在它恢复到正常速度之后。使用pygame.time.delay()和等待内容的问题是,这会降低整个程序的速度。

有更简单的方法吗?

编辑______代码

 #START OF LEVEL 1
    if level1:

        screen.blit(level1_image,background_position)
        pygame.time.delay(500)
        level1yay = True
    if level1yay:

        screen.blit(background,background_position)

#Flip the Display
    pygame.display.flip()
    clock.tick(time)

#Quit
pygame.quit()

不显示第一张图像,直接转到第二张图像

3 个答案:

答案 0 :(得分:1)

您可以设置计时器并跟踪时间。当计时器达到你想要的延迟时,你可以做点什么。

在下面的示例中,我设置了计时器,并且只有在计时器达到延迟值后才将level1yay设置为true。

我添加了条件canexit,因此游戏不会在第一个循环中终止。我假设条件为"如果level1:"已被设置在其他地方。

mytimer = pygame.time.Clock() #creates timer
time_count = 0 #will be used to keep track of the time
mydelay = 5000 # will delay the main game by 5 seconds
canexit = False

#updates  timer
    mytimer.tick()
    time_count += mytimer.get_time()
#check if mydelay has been reached
    if  time_count >= mydelay:
        level1yay = True
        canexit = True

#START OF LEVEL 1
    if level1:
        screen.blit(level1_image,background_position)

    if level1yay:
        screen.blit(background,background_position)

#Flip the Display
    pygame.display.flip()
    clock.tick(time)

 #Quit
 if canexit ==  True:
    pygame.quit()

编辑包括进入第1级之前的行动:

mytimer = pygame.time.Clock() #creates timer
time_count = 0 #will be used to keep track of the time
mydelay = 5000 # will delay the main game by 5 seconds
canexit = False
start_menu = True # to start with the start menu rather than level1

#Do something before level 1
    if start_menu == True:
        ... do stuff
        if start_menu end:
             level1 = True
             time_count = 0

#START OF LEVEL 1
    if level1 == True:
    #updates  timer
        mytimer.tick() # start's ticking the timer
        time_count += mytimer.get_time() # add's the time since the last tick to time_count
    #check if mydelay has been reached
        if  time_count >= mydelay:
            level1 = False # so that you do not enter level1 again (even though this is redundant here since you will exit the game at the end of the loop... see canexit comment)
            level1yay = True # so that you can enter level1yay
            canexit = True # so that the game terminates at the end of the game loop 
            screen.blit(level1_image,background_position)

    if level1yay:
        screen.blit(background,background_position)

#Flip the Display
    pygame.display.flip()
    clock.tick(time)

 #Quit
 if canexit ==  True:
    pygame.quit()

答案 1 :(得分:0)

我不知道这是否真的有用,但是您可以尝试连续多次显示“LEVEL 1”的相同图像,因此它似乎会保持更长时间,但程序本身实际上从未被延迟过。

答案 2 :(得分:0)

pygame.time.wait(5000)延迟5秒,500秒半秒。您确实在此代码片段之前将level1变量设置为True,对吗?