Skip to content

Commit a5dbc39

Browse files
committed
Bug fixes for Chips Challenge
1 parent 608253e commit a5dbc39

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

Metro/Metro_RP2350_Chips_Challenge/audio.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
#
33
# SPDX-License-Identifier: MIT
44
import audiocore
5-
import audiobusio
65
from definitions import PLAY_SOUNDS
76

87
class Audio:
9-
def __init__(self, *, bit_clock, word_select, data):
10-
self._audio = audiobusio.I2SOut(bit_clock, word_select, data)
8+
def __init__(self, audio_bus, sounds):
9+
self._audio = audio_bus
1110
self._wav_files = {}
12-
13-
def add_sound(self, sound_name, file):
14-
self._wav_files[sound_name] = file
11+
for sound_name, file in sounds.items():
12+
self._add_sound(sound_name, file)
13+
# Play the first sound in the list to initialize the audio system
14+
self.play(tuple(self._wav_files.keys())[0], wait=True)
1515

1616
def play(self, sound_name, wait=False):
1717
if not PLAY_SOUNDS:
@@ -23,3 +23,6 @@ def play(self, sound_name, wait=False):
2323
if wait:
2424
while self._audio.playing:
2525
pass
26+
27+
def _add_sound(self, sound_name, file):
28+
self._wav_files[sound_name] = file

Metro/Metro_RP2350_Chips_Challenge/code.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import picodvi
88
import framebufferio
99
import displayio
10+
import adafruit_tlv320
11+
import audiobusio
12+
from audio import Audio
1013
from game import Game
1114
from definitions import SECOND_LENGTH, TICKS_PER_SECOND
1215

@@ -17,13 +20,33 @@
1720
# Change this to use a different data file
1821
DATA_FILE = "CHIPS.DAT"
1922

23+
SOUND_EFFECTS = {
24+
"BUTTON_PUSHED": "/sounds/pop2.wav",
25+
"DOOR_OPENED": "/sounds/door.wav",
26+
"ITEM_COLLECTED": "/sounds/blip2.wav",
27+
"BOOTS_STOLEN": "/sounds/strike.wav",
28+
"WATER_SPLASH": "/sounds/water2.wav",
29+
"TELEPORT": "/sounds/teleport.wav",
30+
"CANT_MOVE": "/sounds/oof3.wav",
31+
"CHIP_LOSES": "/sounds/bummer.wav",
32+
"LEVEL_COMPLETE": "/sounds/ditty1.wav",
33+
"IC_COLLECTED": "/sounds/click3.wav",
34+
"BOMB_EXPLOSION": "/sounds/hit3.wav",
35+
"SOCKET_SOUND": "/sounds/chimes.wav",
36+
"TIME_LOW_TICK": "/sounds/click1.wav",
37+
"TIME_UP": "/sounds/bell.wav"
38+
}
39+
2040
displayio.release_displays()
2141

22-
audio_settings = {
23-
'bit_clock': board.D9,
24-
'word_select': board.D10,
25-
'data': board.D11
26-
}
42+
i2c = board.I2C()
43+
dac = adafruit_tlv320.TLV320DAC3100(i2c)
44+
dac.configure_clocks(sample_rate=44100, bit_depth=16)
45+
dac.headphone_output = True
46+
dac.headphone_volume = -15 # dB
47+
48+
audio_bus = audiobusio.I2SOut(board.D9, board.D10, board.D11)
49+
audio = Audio(audio_bus, SOUND_EFFECTS)
2750

2851
fb = picodvi.Framebuffer(320, 240, clk_dp=board.CKP, clk_dn=board.CKN,
2952
red_dp=board.D0P, red_dn=board.D0N,
@@ -32,7 +55,7 @@
3255
color_depth=8)
3356
display = framebufferio.FramebufferDisplay(fb)
3457

35-
game = Game(display, DATA_FILE, **audio_settings)
58+
game = Game(display, DATA_FILE, audio)
3659
tick_length = SECOND_LENGTH / 1000 / TICKS_PER_SECOND
3760
while True:
3861
start = time.monotonic()

Metro/Metro_RP2350_Chips_Challenge/definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from micropython import const
55

66
# Settings
7-
PLAY_SOUNDS = False
7+
PLAY_SOUNDS = True
88

99
# Timing Constants
1010
TICKS_PER_SECOND = const(20)

Metro/Metro_RP2350_Chips_Challenge/game.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def get_victory_message(deaths):
7070
return None
7171

7272
class Game:
73-
def __init__(self, display, data_file, **kwargs):
73+
def __init__(self, display, data_file, audio):
7474
self._display = display
7575
self._images = {}
7676
self._buffers = {}
7777
self._message_group = displayio.Group()
7878
self._loading_group = displayio.Group()
7979
self._tile_size = 24 # Default tile size (length and width)
8080
self._digit_dims = (0, 0)
81-
self._gamelogic = GameLogic(data_file, **kwargs)
81+
self._gamelogic = GameLogic(data_file, audio)
8282
self._databuffer = DataBuffer()
8383
self._color_index = {}
8484
self._init_display()
@@ -850,5 +850,5 @@ def _draw_frame(self):
850850
y_pos * self._tile_size + VIEWPORT_OFFSET[1], top_tile, bottom_tile
851851
)
852852

853-
self._draw_hint()
854853
self._draw_title_dialog()
854+
self._draw_hint()

Metro/Metro_RP2350_Chips_Challenge/gamelogic.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@
1313
from level import Level, Tile
1414
from point import Point
1515
from slip import Slip
16-
from audio import Audio
17-
18-
SOUND_EFFECTS = {
19-
"BUTTON_PUSHED": "/sounds/pop2.wav",
20-
"DOOR_OPENED": "/sounds/door.wav",
21-
"ITEM_COLLECTED": "/sounds/blip2.wav",
22-
"BOOTS_STOLEN": "/sounds/strike.wav",
23-
"WATER_SPLASH": "/sounds/water2.wav",
24-
"TELEPORT": "/sounds/teleport.wav",
25-
"CANT_MOVE": "/sounds/oof3.wav",
26-
"CHIP_LOSES": "/sounds/bummer.wav",
27-
"LEVEL_COMPLETE": "/sounds/ditty1.wav",
28-
"IC_COLLECTED": "/sounds/click3.wav",
29-
"BOMB_EXPLOSION": "/sounds/hit3.wav",
30-
"SOCKET_SOUND": "/sounds/chimes.wav",
31-
"TIME_LOW_TICK": "/sounds/click1.wav",
32-
"TIME_UP": "/sounds/bell.wav"
33-
}
3416

3517
def is_ice(tile):
3618
return tile == TYPE_ICE or TYPE_ICEWALL_SOUTHEAST <= tile <= TYPE_ICEWALL_NORTHEAST
@@ -73,7 +55,8 @@ class GameLogic:
7355
A class to represent the state of the game as well as
7456
control all the game movements and actions.
7557
"""
76-
def __init__(self, data_file, **kwargs):
58+
def __init__(self, data_file, audio):
59+
self._audio = audio
7760
self._tileset = [Element() for _ in range(0x70)]
7861
self._chip = Creature()
7962
self._create_tileset()
@@ -96,10 +79,7 @@ def __init__(self, data_file, **kwargs):
9679
self._current_time = 0
9780
self._last_slip_dir = NONE
9881
self._controller_dir = NONE
99-
self._audio = Audio(**kwargs)
10082
self._time_limit = 0
101-
for sound_name, file in SOUND_EFFECTS.items():
102-
self._audio.add_sound(sound_name, file)
10383

10484
def dec_level(self):
10585
if self.current_level_number > 1:

0 commit comments

Comments
 (0)