Menu
CoddyTech

Pause the Music

Part of the Sound and Game Feel section of Coddy's Pygame journey. Lesson 7 of 23.

When the game pauses, the music should pause with it, and go on from the same place when play resumes. The music player has two calls for that:

pygame.mixer.music.pause()
pygame.mixer.music.unpause()

pause() holds the track where it is. unpause() goes on from there. Do not use stop() and play() for this: play() starts the track again from its beginning, and the player hears the first bars every time they pause.

Call them at the moment the pause changes, where P flips paused:

paused = not paused
if paused:
    pygame.mixer.music.pause()
else:
    pygame.mixer.music.unpause()

Sound effects that are playing when you pause finish on their own; they are short. A paused game starts no new ones, because the code that plays them does not run.

challenge icon

Challenge

Medium

P pauses the game and shows Paused. Make the music follow:

  1. When P pauses, call pygame.mixer.music.pause().
  2. When P resumes, call pygame.mixer.music.unpause().

Try it yourself

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Lava Run")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)


def play_music(path, loops=-1):
    pygame.mixer.music.load(path)
    pygame.mixer.music.play(loops)


play_music("assets/music/theme.ogg")

player = pygame.Rect(40, 160, 30, 30)
goal = pygame.Rect(420, 160, 30, 30)
lava = pygame.Rect(0, 320, 480, 40)
state = "menu"
paused = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_p:
            if state == "play":
                paused = not paused
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
            if state == "menu":
                state = "play"
                play_music("assets/music/level.ogg")
                player.topleft = (40, 160)
            elif state in ("win", "lose"):
                state = "menu"
                play_music("assets/music/theme.ogg")

    if state == "play" and not paused:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            player.x -= 4
        if keys[pygame.K_RIGHT]:
            player.x += 4
        if keys[pygame.K_UP]:
            player.y -= 4
        if keys[pygame.K_DOWN]:
            player.y += 4
        player.clamp_ip(screen.get_rect())
        if player.colliderect(goal):
            state = "win"
            play_music("assets/music/win.ogg", 0)
        elif player.colliderect(lava):
            state = "lose"
            play_music("assets/music/lose.ogg", 0)

    screen.fill((30, 30, 46))
    if state == "menu":
        title = font.render("Press Enter to start", True, (240, 240, 240))
        screen.blit(title, title.get_rect(center=(240, 180)))
    else:
        pygame.draw.rect(screen, (230, 90, 40), lava)
        pygame.draw.rect(screen, (90, 200, 120), goal)
        pygame.draw.rect(screen, (80, 160, 255), player)
        if state == "win":
            message = font.render("You win!", True, (240, 240, 240))
            screen.blit(message, message.get_rect(center=(240, 100)))
        elif state == "lose":
            message = font.render("Game over", True, (240, 240, 240))
            screen.blit(message, message.get_rect(center=(240, 100)))
        elif paused:
            message = font.render("Paused", True, (240, 240, 240))
            screen.blit(message, message.get_rect(center=(240, 100)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Sound and Game Feel

Practice on your own: Pygame playground