Skip to main content
 首页 » 编程设计

python之放慢时间,但不是程序

2025年04月02日15powertoolsteam

我正在制作一款游戏,当玩家走出屏幕时,关卡就会开始。我想在游戏开始前显示“LEVEL 1”的图像,但程序显示图像的速度太快了。我的帧率为 60。

我想知道是否有一种方法可以在屏幕闪烁期间将时间延迟大约 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() 

第一张图不显示,直接进入第二张图

请您参考如下方法:

您可以设置一个计时器并跟踪时间。当计时器达到您想要的延迟时,您可以做一些事情。

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

我添加了一个 canexit 条件,这样游戏就不会在第一个循环中终止。我假设“if 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()