Docs › game library

game.sound()

Play a short arcade sound effect: a coin, a jump, an explosion. There are 10 built in, made right in the browser, so you never need a sound file.

How to write it

game.sound(n)
n
Which sound, as a number from 0 to 9 or as its name in quotes, like game.sound(2) or game.sound("coin"). Leave it out for 0, the beep.

It starts the sound and carries straight on, so your game never waits for it to finish.

All the sounds

0  "beep"
a menu click, a countdown tick
1  "buzz"
a wrong answer, "you can't do that"
2  "coin"
picking something up, scoring a point
3  "powerup"
a power-up, a level up, an upgrade
4  "jump"
jumping, bouncing
5  "laser"
shooting
6  "hit"
getting hurt, losing a life
7  "explosion"
a crash, a blown-up asteroid
8  "win"
winning, clearing a level
9  "lose"
game over

A few extra names work too: "shoot" and "zap" are the laser, "boom" is the explosion, "hurt" is the hit, "point" and "score" are the coin, "wrong" and "error" are the buzz, "blip" is the beep, "won" is win and "gameover" is lose. Capital letters don't matter.

A full example

A sound board: press a number key from 0 to 9 to hear that sound and see its name.

import game

game.window(480, 360, background="#14213d")

names = ["beep", "buzz", "coin", "powerup", "jump",
         "laser", "hit", "explosion", "win", "lose"]

game.label("Click here, then press 0 to 9", 240, 40, color="#ffffff")
shown = game.label("", 240, 180, size=40, color="#ffd166")

was_down = False          # was a number key down last frame?

while game.playing():
    key = None
    for n in range(10):
        if game.pressed(str(n)):
            key = n

    if key is not None and not was_down:     # only the moment it goes down
        game.sound(key)
        shown.content = str(key) + "  " + names[key]

    was_down = key is not None
    game.frame()

Browsers only allow sound after you click or tap the page. A game is clicked to play anyway, so this almost never matters, but if the very first sound is silent, that's why.

When to play a sound

Play a sound once, when something happens: the moment a coin is caught, the moment a jump starts, the moment the game ends. Put it right next to the code that makes the thing happen:

if basket.touches(coin):
    score = score + 1
    game.sound("coin")        # once, because the coin moves away straight after
    coin.y = 0

Never play it on every frame. The loop runs 30 times a second, so this plays 30 laser sounds a second for as long as the key is held, and they pile into one ugly buzz:

while game.playing():
    if game.pressed("space"):
        game.sound("laser")   # 30 lasers a second!
    game.frame()

Fix it by playing the sound only when the key goes down, like the sound board does with was_down, or by adding a short wait (a cool-down) between shots.

Need a sound that keeps going, like a car engine or a siren that rises and falls? That's game.tone(), which starts a sound that keeps playing until you stop it, and you can change its pitch every frame.

Why isn't my sound working?

  • I hear a beep instead of my sound. The name is misspelled, like "coins" or "explode", or the number is bigger than 9. Anything it doesn't know plays the beep. Check the list above.
  • It sounds like a horrible buzz. It's being played every frame. See When to play a sound.
  • I hear nothing at all. Check your volume and that the tab isn't muted, then click the game once. The browser won't play sound until you have clicked the page.
  • I want my own sound file. That isn't possible: game.sound() only plays the built-in sounds. Use game.tone() to make your own beeps and hums.