Skip to content

Commit 961c259

Browse files
Add some sounds
1 parent 0cb4c1b commit 961c259

5 files changed

Lines changed: 20 additions & 0 deletions

File tree

Macropad_Dragon_Drop/code.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424

2525
# UTILITY FUNCTIONS AND CLASSES --------
2626

27+
AUDIO = None
28+
29+
import audiocore
30+
import audiopwmio
31+
import board
32+
AUDIO = audiopwmio.PWMAudioOut(board.SPEAKER)
33+
34+
def background_sound(filename):
35+
AUDIO.stop()
36+
MACROPAD._speaker_enable.value = True
37+
AUDIO.play(audiocore.WaveFile(open(filename, 'rb')))
38+
39+
2740
# pylint: disable=too-few-public-methods
2841
class Sprite:
2942
""" Class for sprite (eggs, fireballs) state information """
@@ -148,13 +161,16 @@ def __init__(self, column, start_time):
148161
TILE[0] = 3 + int((NOW * 6) % 2.0)
149162
# Fire catch logic
150163
if y >= MACROPAD.display.height - 40 and COLUMN_PRESSED[COLUMN]:
164+
background_sound('dragondrop/sizzle.wav')
165+
151166
sprite.paused = True
152167
sprite.start_time = NOW
153168
TILE.y = MACROPAD.display.height - 20
154169
LIFE_BAR.value = max(0, LIFE_BAR.value - 4)
155170
else:
156171
if y >= MACROPAD.display.height - 22:
157172
# Egg hit ground
173+
background_sound('dragondrop/splat.wav')
158174
TILE.y = MACROPAD.display.height - 22
159175
TILE[0] = 1 # Broken egg
160176
sprite.paused = True
@@ -163,6 +179,7 @@ def __init__(self, column, start_time):
163179
elif y >= MACROPAD.display.height - 40:
164180
if COLUMN_PRESSED[COLUMN]:
165181
# Egg caught at right time
182+
background_sound('dragondrop/rawr.wav')
166183
TILE.y = MACROPAD.display.height - 22
167184
TILE[0] = 2 # Dragon hatchling
168185
SCORE += 10
@@ -172,6 +189,7 @@ def __init__(self, column, start_time):
172189
elif y >= MACROPAD.display.height - 58:
173190
if COLUMN_PRESSED[COLUMN]:
174191
# Egg caught too soon
192+
background_sound('dragondrop/splat.wav')
175193
TILE.y = MACROPAD.display.height - 40
176194
TILE[0] = 1 # Broken egg
177195
sprite.paused = True
@@ -203,4 +221,6 @@ def __init__(self, column, start_time):
203221
break
204222

205223
MACROPAD.display.refresh()
224+
if not AUDIO.playing:
225+
MACROPAD._speaker_enable.value = False
206226
gc.collect()
27.4 KB
Binary file not shown.
22.7 KB
Binary file not shown.
12.6 KB
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)