abc1234 abc1234 - 6 months ago 14
Python Question

Slowing time, but not the program

I'm making a game, where when a player steps outside the screen, the level starts. I wish to show an image of "LEVEL 1" before the game starts, yet the program shows the image too quickly. My framerate is at 60.

I am wondering if there is a way to delay time for about 5 seconds during the screen blitting but after it resumes to it's normal pace. The problem for me with the

pygame.time.delay()
and wait stuff is that is slows the entire program down.

Is there a easier way?

EDIT______ CODE

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


The first image is not displayed and goes directly to the second image

Answer

You could set up a timer and keep track of the time. when the timer reaches the delay you want you can do something.

In the example below I set up the timer and I set the level1yay to true only once the timer has reached the delay value.

I added a condition canexit so the game doesn't terminate on the first loop. I assumed that the condition for "if level1: " has been set somewhere else.

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

EDIT TO INCLUDE ACTIONS PRIOR TO ENTERING 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



#START OF LEVEL 1
    if level1:
    #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()