Music for Each State
Part of the Sound and Game Feel section of Coddy's Pygame journey. Lesson 6 of 23.
Each state of a game can have its own music: a calm theme on the menu, a busy track while you play, a short tune when you win or lose. To switch, load the new track and play it. Loading a new track stops the old one. A small function keeps the two lines together:
def play_music(path, loops=-1):
pygame.mixer.music.load(path)
pygame.mixer.music.play(loops)Switch the music at the moment the state changes, on the line that sets state:
if player.colliderect(goal):
state = "win"
play_music("assets/music/win.ogg", 0)That line runs once, so the track starts once. Do not switch inside if state == "win": in the drawing part: that runs on every frame and restarts the track 60 times a second.
The win and lose tunes are short and end on their own, so they take loops 0. The theme and the level track loop with -1.
Challenge
MediumEnter starts the game; the blue square wins at the green goal and loses in the lava. Give each state its music:
- Write
play_music(path, loops=-1)that loads and plays a track. - Before the loop, play
assets/music/theme.ogg. - Where the state becomes
"play", playassets/music/level.ogg. - Where it becomes
"win", playassets/music/win.oggonce (loops 0); for"lose",assets/music/lose.oggonce. - Where Enter goes back to
"menu", play theme.ogg again.
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)
player = pygame.Rect(40, 160, 30, 30)
goal = pygame.Rect(420, 160, 30, 30)
lava = pygame.Rect(0, 320, 480, 40)
state = "menu"
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_RETURN:
if state == "menu":
state = "play"
player.topleft = (40, 160)
elif state in ("win", "lose"):
state = "menu"
if state == "play":
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"
elif player.colliderect(lava):
state = "lose"
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)))
pygame.display.flip()
clock.tick(60)
pygame.quit()
All lessons in Sound and Game Feel
Practice on your own: Pygame playground