Getting Started with 2D Games Using Arcade Library (Part x): Playing Sound Effects
In this article, we will play sound effects in our game.
1. Prepare Sound Files
Create a new sounds folder and place the following sound file inside it.
| Sound | File Name |
|---|---|
| Download se_coin.ogg | se_coin.ogg |
If you cannot download the file directly from GitHub, please use the Download button at the top right of the page.
The sound file is provided by
SEADENDEN 8bit Free BGM
(highly recommended by the author!!).
The folder structure should look like this:
# Folder structure
working_directory/
├ main.py
├ sprite.py
├ images/
└ sounds/ # Folder for sound files
└ se_coin.ogg
2. Create a Sound Object
By passing the path to a sound file into arcade.Sound(), you can create a sound object.
# main.py (excerpt)
# Sound object
self.se_coin = arcade.Sound("sounds/se_coin.ogg")
3. Play the Sound
You can play the created sound object by passing it to arcade.play_sound().
# main.py (excerpt)
# Play the sound
arcade.play_sound(self.se_coin)
If you cannot hear any sound, please check:
- Your PC volume or mute settings
- Whether the path to the sound file is correct
Complete Code
Below is the complete code with all features implemented so far.
You can copy and run it as-is.
# sprite.py (complete code)
import arcade
import math
class BaseSprite(arcade.Sprite):
def __init__(self, filename, x, y):
super().__init__(filename)
# Position
self.center_x = x
self.center_y = y
# Velocity
self.vx = 0
self.vy = 0
# Animation
self.anim_counter = 0
self.anim_interval = 4
self.anim_index = 0
self.anim = []
def update(self, delta_time):
""" Update """
self.center_x += self.vx * delta_time
self.center_y += self.vy * delta_time
# Animation
self.update_animation()
def move(self, spd, deg):
""" Move Sprite """
rad = deg * math.pi / 180
self.vx = spd * math.cos(rad)
self.vy = spd * math.sin(rad)
def stop(self):
""" Stop Sprite """
self.vx = 0
self.vy = 0
def update_animation(self):
""" Animation """
if not self.anim:
return
self.anim_counter += 1
if self.anim_counter < self.anim_interval:
return
self.anim_counter = 0
self.anim_index += 1
if len(self.anim) <= self.anim_index:
self.anim_index = 0
self.texture = self.anim[self.anim_index]
class Player(BaseSprite):
def __init__(self, filename, x, y):
super().__init__(filename, x, y)
self.anim.append(arcade.load_texture("images/ninja/front_01.png"))
self.anim.append(arcade.load_texture("images/ninja/front_02.png"))
self.anim.append(arcade.load_texture("images/ninja/front_03.png"))
self.anim.append(arcade.load_texture("images/ninja/front_04.png"))
self.anim.append(arcade.load_texture("images/ninja/front_05.png"))
class Coin(BaseSprite):
def __init__(self, filename, x, y):
super().__init__(filename, x, y)
self.anim.append(arcade.load_texture("images/coin/coin_01.png"))
self.anim.append(arcade.load_texture("images/coin/coin_02.png"))
self.anim.append(arcade.load_texture("images/coin/coin_03.png"))
self.anim.append(arcade.load_texture("images/coin/coin_04.png"))
self.anim.append(arcade.load_texture("images/coin/coin_05.png"))
# main.py (complete code)
import arcade
import sprite
import random
class GameView(arcade.View):
def __init__(self, window):
super().__init__()
self.window = window
self.w = self.window.width
self.h = self.window.height
# Background color
self.background_color = arcade.color.PAYNE_GREY
# Background sprites
self.backgrounds = arcade.SpriteList()
bkg = arcade.Sprite("images/bg_temple.png")
bkg.center_x = self.w / 2
bkg.center_y = self.h / 2
self.backgrounds.append(bkg)
# Player sprite
self.players = arcade.SpriteList()
self.player = sprite.Player(
"images/ninja/front_01.png",
x=self.w / 2, y=self.h / 2
)
self.players.append(self.player)
# Coin sprites
self.coins = arcade.SpriteList()
for i in range(10):
x = random.random() * self.w
y = random.random() * self.h
coin = sprite.Coin(
"images/coin/coin_01.png",
x=x, y=y
)
self.coins.append(coin)
# Score
self.score = 0
self.score_text = arcade.Text(
"SCORE: {}".format(self.score),
self.w / 2, self.h - 20,
arcade.color.BLACK, 16,
anchor_x="center", anchor_y="top"
)
# Sound object
self.se_coin = arcade.Sound("sounds/se_coin.ogg")
def on_key_press(self, key, key_modifiers):
# Move (WASD)
if key == arcade.key.W:
self.player.move(90, 90)
if key == arcade.key.A:
self.player.move(90, 180)
if key == arcade.key.S:
self.player.move(90, 270)
if key == arcade.key.D:
self.player.move(90, 0)
def on_key_release(self, key, key_modifiers):
self.player.stop()
def on_update(self, delta_time):
self.players.update(delta_time)
self.coins.update(delta_time)
# Player x coin list
hit_coins = arcade.check_for_collision_with_list(
self.player,
self.coins
)
for coin in hit_coins:
coin.remove_from_sprite_lists()
# Score
self.score += 1
self.score_text.text = "SCORE: {}".format(self.score)
# Play sound
arcade.play_sound(self.se_coin)
def on_draw(self):
self.clear() # Clear
self.backgrounds.draw()
self.players.draw()
self.coins.draw()
self.score_text.draw()
def main():
""" Main process """
window = arcade.Window(480, 320, "Hello, Arcade!!")
game = GameView(window)
window.show_view(game)
arcade.run()
if __name__ == "__main__":
main()
The result will look like this (the sound should be playing... hopefully!?):
Final Thoughts
Thank you very much for reading.
This concludes "Getting Started with 2D Games Using Arcade Library".
Great job finishing the series!!
The game we created is very simple, but it includes all the fundamental elements of game development:
- Sprite management
- Keyboard input
- Sound playback
If you understand everything up to this point, you should now be able to create simple games with Arcade on your own.
I highly recommend using what you’ve learned here as a base and trying to build your own small game.
I hope this series becomes your starting point for game development. ޱ(ఠ皿ఠ)ว
(If you enjoyed it, a 👍 would be greatly appreciated!!)

Top comments (0)