J.R. BEATS
J.R. BEATS

Reputation: 93

I ran into a little timer bug

I ran into a bug with my timer. When I pause my game I noticed that my timer doesn't pause a long with the game. Example: if I pause the game when the timer is at 26 seconds remaining, and unpause the game about ten seconds later, the timer will say 16 seconds left instead of counting down from 26 like I want it to. If I pause the game for 30 seconds the timer still runs down to 0 instead of the timer pausing along with the game. How can I fix this?

code where my game runs:

#game runs here
run = True
paused = False
while run:

    for event in pygame.event.get():
        if event.type == pygame.USEREVENT:
            counter -= 1
            text = str(counter).rjust(3) if counter > 0 else 'GAME OVER!'
        if event.type == pygame.QUIT:
            run = False

        #pause game
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                paused = not paused

        #check if key is down
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
            if event.key == pygame.K_a:
                player.movingLeft = True
            if event.key == pygame.K_d:
                player.movingRight = True
            if event.key == pygame.K_SPACE:
                player.shoot()
                shoot = True

        #check if key is up
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                player.movingLeft = False
            if event.key == pygame.K_d:
                player.movingRight = False

    if paused:
        continue

    #draw street
    screen.blit(bg, [0, 0])

    #draw timer
    screen.blit(timer_font.render(text, True, (0,0,0)), (800, 10))

    #update groups
    bullet_group.update()
    bullet_group.draw(screen)

    debris_group.update()
    debris_group.draw(screen)

    #draw car
    player.draw()
    player.move()
    player.collision(debris_group)
    player.stats()

    #update all sprites
    all_sprites.update()
    all_sprites.draw(screen)

    #update the display
    pygame.display.update()
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()

Upvotes: 1

Views: 67

Answers (1)

Rabbid76
Rabbid76

Reputation: 211277

The timer event can be stopped by passing 0 to the time argument of pygame.time.set_timer.

Stop and restart the timer event when you toggle the pause state:

run = True
paused = False
while run:

    for event in pygame.event.get():
        if event.type == pygame.USEREVENT:
            counter -= 1
            text = str(counter).rjust(3) if counter > 0 else 'GAME OVER!'
        if event.type == pygame.QUIT:
            run = False

        #pause game
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                paused = not paused
                if pause:
                    pygame.time.set_timer(pygame.USEREVENT, 0)    # stop timer
                else:
                    pygame.time.set_timer(pygame.USEREVENT, 1000) # 1 second interval


        # [...]

Upvotes: 1

Related Questions