Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Metro/Metro_RP2350_Chips_Challenge/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#
# SPDX-License-Identifier: MIT
import audiocore
import audiobusio
from definitions import PLAY_SOUNDS

class Audio:
def __init__(self, *, bit_clock, word_select, data):
self._audio = audiobusio.I2SOut(bit_clock, word_select, data)
def __init__(self, audio_bus, sounds):
self._audio = audio_bus
self._wav_files = {}

def add_sound(self, sound_name, file):
self._wav_files[sound_name] = file
for sound_name, file in sounds.items():
self._add_sound(sound_name, file)
# Play the first sound in the list to initialize the audio system
self.play(tuple(self._wav_files.keys())[0], wait=True)

def play(self, sound_name, wait=False):
if not PLAY_SOUNDS:
Expand All @@ -23,3 +23,6 @@ def play(self, sound_name, wait=False):
if wait:
while self._audio.playing:
pass

def _add_sound(self, sound_name, file):
self._wav_files[sound_name] = file
35 changes: 29 additions & 6 deletions Metro/Metro_RP2350_Chips_Challenge/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import picodvi
import framebufferio
import displayio
import adafruit_tlv320
import audiobusio
from audio import Audio
from game import Game
from definitions import SECOND_LENGTH, TICKS_PER_SECOND

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

SOUND_EFFECTS = {
"BUTTON_PUSHED": "/sounds/pop2.wav",
"DOOR_OPENED": "/sounds/door.wav",
"ITEM_COLLECTED": "/sounds/blip2.wav",
"BOOTS_STOLEN": "/sounds/strike.wav",
"WATER_SPLASH": "/sounds/water2.wav",
"TELEPORT": "/sounds/teleport.wav",
"CANT_MOVE": "/sounds/oof3.wav",
"CHIP_LOSES": "/sounds/bummer.wav",
"LEVEL_COMPLETE": "/sounds/ditty1.wav",
"IC_COLLECTED": "/sounds/click3.wav",
"BOMB_EXPLOSION": "/sounds/hit3.wav",
"SOCKET_SOUND": "/sounds/chimes.wav",
"TIME_LOW_TICK": "/sounds/click1.wav",
"TIME_UP": "/sounds/bell.wav"
}

displayio.release_displays()

audio_settings = {
'bit_clock': board.D9,
'word_select': board.D10,
'data': board.D11
}
i2c = board.I2C()
dac = adafruit_tlv320.TLV320DAC3100(i2c)
dac.configure_clocks(sample_rate=44100, bit_depth=16)
dac.headphone_output = True
dac.headphone_volume = -15 # dB

audio_bus = audiobusio.I2SOut(board.D9, board.D10, board.D11)
audio = Audio(audio_bus, SOUND_EFFECTS)

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

game = Game(display, DATA_FILE, **audio_settings)
game = Game(display, DATA_FILE, audio)
tick_length = SECOND_LENGTH / 1000 / TICKS_PER_SECOND
while True:
start = time.monotonic()
Expand Down
2 changes: 1 addition & 1 deletion Metro/Metro_RP2350_Chips_Challenge/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from micropython import const

# Settings
PLAY_SOUNDS = False
PLAY_SOUNDS = True

# Timing Constants
TICKS_PER_SECOND = const(20)
Expand Down
3 changes: 3 additions & 0 deletions Metro/Metro_RP2350_Chips_Challenge/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ def draw_field(self, field, first_draw=False):
# The box should be on the right of the coordinates
# The width and height should be the size of the box
# The font should be the font to use for the label and the text box
if isinstance(field["padding"], int):
field["padding"] = convert_padding(field["padding"])

if first_draw:
# draw the label
label = TextBox(
Expand Down
6 changes: 3 additions & 3 deletions Metro/Metro_RP2350_Chips_Challenge/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ def get_victory_message(deaths):
return None

class Game:
def __init__(self, display, data_file, **kwargs):
def __init__(self, display, data_file, audio):
self._display = display
self._images = {}
self._buffers = {}
self._message_group = displayio.Group()
self._loading_group = displayio.Group()
self._tile_size = 24 # Default tile size (length and width)
self._digit_dims = (0, 0)
self._gamelogic = GameLogic(data_file, **kwargs)
self._gamelogic = GameLogic(data_file, audio)
self._databuffer = DataBuffer()
self._color_index = {}
self._init_display()
Expand Down Expand Up @@ -850,5 +850,5 @@ def _draw_frame(self):
y_pos * self._tile_size + VIEWPORT_OFFSET[1], top_tile, bottom_tile
)

self._draw_hint()
self._draw_title_dialog()
self._draw_hint()
24 changes: 2 additions & 22 deletions Metro/Metro_RP2350_Chips_Challenge/gamelogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@
from level import Level, Tile
from point import Point
from slip import Slip
from audio import Audio

SOUND_EFFECTS = {
"BUTTON_PUSHED": "/sounds/pop2.wav",
"DOOR_OPENED": "/sounds/door.wav",
"ITEM_COLLECTED": "/sounds/blip2.wav",
"BOOTS_STOLEN": "/sounds/strike.wav",
"WATER_SPLASH": "/sounds/water2.wav",
"TELEPORT": "/sounds/teleport.wav",
"CANT_MOVE": "/sounds/oof3.wav",
"CHIP_LOSES": "/sounds/bummer.wav",
"LEVEL_COMPLETE": "/sounds/ditty1.wav",
"IC_COLLECTED": "/sounds/click3.wav",
"BOMB_EXPLOSION": "/sounds/hit3.wav",
"SOCKET_SOUND": "/sounds/chimes.wav",
"TIME_LOW_TICK": "/sounds/click1.wav",
"TIME_UP": "/sounds/bell.wav"
}

def is_ice(tile):
return tile == TYPE_ICE or TYPE_ICEWALL_SOUTHEAST <= tile <= TYPE_ICEWALL_NORTHEAST
Expand Down Expand Up @@ -73,7 +55,8 @@ class GameLogic:
A class to represent the state of the game as well as
control all the game movements and actions.
"""
def __init__(self, data_file, **kwargs):
def __init__(self, data_file, audio):
self._audio = audio
self._tileset = [Element() for _ in range(0x70)]
self._chip = Creature()
self._create_tileset()
Expand All @@ -96,10 +79,7 @@ def __init__(self, data_file, **kwargs):
self._current_time = 0
self._last_slip_dir = NONE
self._controller_dir = NONE
self._audio = Audio(**kwargs)
self._time_limit = 0
for sound_name, file in SOUND_EFFECTS.items():
self._audio.add_sound(sound_name, file)

def dec_level(self):
if self.current_level_number > 1:
Expand Down
Binary file not shown.