piano_game.py#

piano_game.py#

This file contains the implementation of the PianoGameUI class, which is responsible for handling the user interface and game logic for the Walking Piano game. The class integrates with Pyglet for rendering the UI and Mido for MIDI input/output handling.

Classes:

PianoGameUI: Manages the piano game UI, including drawing the piano, handling MIDI input, and game mechanics. ClockPauseManager: Manages the clock and scheduled functions, allowing pausing and resuming of the game.

Functions:

None (all functionality is encapsulated within classes).

Author: Devin Martin and Wesley Jake Anding

class piano_game.ClockPauseManager(update_rectangles)#

Bases: object

ClockPauseManager is responsible for managing the clock and scheduled functions, allowing pausing and resuming of the game. Mostly used for the Piano game in practice mode. The implementation is based on the Pyglet clock and scheduling functions.

This method is buggy and needs to be improved in future development!

Attributes#

scheduled_functionslist

List of scheduled functions.

start_timedatetime.datetime

The start time of the game.

update_rectanglescallable

Method to update rectangles.

pausedbool

Flag indicating if the game is paused.

pause_timedatetime.datetime

The time when the game was paused.

unpause_timedatetime.datetime

The time when the game was unpaused.

clear()#

Clear all scheduled functions and reset the pause state.

pause()#

Pause the clock and all scheduled functions.

resume()#

Resume the clock and all scheduled functions with adjusted intervals.

schedule_function(func, delay)#

Schedule a function with a delay.

Parameters#

funccallable

The function to be scheduled.

delayfloat

The delay in seconds.

class piano_game.PianoGameUI(window, midi_file_path, game_mode, inport_name, outport_name, controller_size, player_count=1, auto_play=0)#

Bases: EventDispatcher

create_active_notes_line()#

Create ‘imaginary’ line where notes will fall from. This lines up with the piano keys and utilizes very similar logic to the create_piano function.

create_black_key(x, y, width, height, color)#

Create a piano black key.

Parameters#

xint

The x-coordinate of the key.

yint

The y-coordinate of the key.

widthint

The width of the key.

heightint

The height of the key.

colortuple

The color of the key.

Returns#

pyglet.shapes.Rectangle

The created black key.

create_piano()#

Utilize create_white_key and create_black_key to create the entire piano with all keys. There are two modes for the piano, 49 key and 88 key. A parameter passed into the class will determine which mode to use. See attribute ‘controller_size’ for more details.

create_white_key(x, y, width, height, color)#

Create a piano white key.

Parameters#

xint

The x-coordinate of the key.

yint

The y-coordinate of the key.

widthint

The width of the key.

heightint

The height of the key.

colortuple

The color of the key.

Returns#

pyglet.shapes.Rectangle

The created white key.

end_of_song(dt)#

Acknowledge the end of the song. Once the song is over, on_draw will display the game over message.

Parameters#

dtfloat

The delta time.

exit_game()#

Exit the game and clean up resources.

falling_rectangles_list#

Todo: Get this note list working with ‘extract_track_messages()’ from MIDIprocessor class

flag_note(note_number, bool)#

Flag a note as being played or not.

Parameters#

note_numberint

The number of the note.

boolbool

Flag indicating if the note is played.

get_note_label_color(note_name)#

Get the color for the note label. Default to black if not found.

Parameters#

note_namestr

The name of the note.

Returns#

tuple

The color of the note label.

highlight_key(key_number)#

Highlight a specific key based on the key number.

Parameters#

key_numberint

The number of the key to highlight.

incoming_notes#

#Temp for testing visuals

jukebox_mode(midi_file_path)#

Play the piano automatically in jukebox mode.

Parameters#

midi_file_pathstr

The path to the MIDI file to be played.

load_midi_file(midi_file_path)#

Load a MIDI file into the game. Utilizes the MIDIProcessor class from midi_processor.py

Parameters#

midi_file_pathstr

The path to the MIDI file.

on_draw()#

Draw all aspects of the game. This method is called automatically by Pyglet every frame.

on_key_press(symbol, modifiers)#

Handle custom keybinds for development.

Parameters#

symbolint

The key symbol pressed.

modifiersint

Any modifier keys pressed.

on_mouse_press(x, y, button, modifiers)#

Handle mouse press to quit the game.

Parameters#

xint

The x-coordinate of the mouse press.

yint

The y-coordinate of the mouse press.

buttonint

The mouse button pressed.

modifiersint

Any modifier keys pressed.

play_piano_user()#

Allow user to play the piano using the MIDI keyboard. Starts a seperate thread to avoid blocking the main thread. Allows user to play the piano in real time with no interference with/from the game.

prepare_falling_rectangle(note_number, player)#

Prepare a falling rectangle for a specific note number.

Parameters#

note_numberint

The number of the note.

playerint

The player number.

Returns#

pyglet.shapes.BorderedRectangle

The created falling rectangle.

schedule_flag_note_off(dt, note, velocity)#

Schedule a note to be flagged as not being played.

Parameters#

dtfloat

The delta time.

noteint

The note number.

velocityint

The velocity of the note.

schedule_flag_note_on(dt, note, velocity, player)#

Schedule a note to be flagged as being played.

Parameters#

dtfloat

The delta time.

noteint

The note number.

velocityint

The velocity of the note.

playerint

The player number.

start_rectangle_game(dt, track_messages, player)#

Function to schedule notes for our Piano game. This function is called during initialization by the MIDIProcessor class.

Parameters#

dtfloat

The delta time.

track_messageslist

The list of track messages.

playerint

The player number.

start_rectangle_game_thread(dt, track_messages, player)#

Wrapper function for threading. Just calls start_rectangle_game with the given parameters and starts the seperate game thread.

Parameters#

dtfloat

The delta time.

track_messageslist

The list of track messages.

playerint

The player number.

unhighlight_key(key_number)#

Unhighlight a specific key based on the key number.

Parameters#

key_numberint

The number of the key to unhighlight.

update_rectangles(dt)#

Update the falling rectangles. This function is called every frame to update the falling rectangles. Most of the logic for the game is handled here. A deep understanding of the game logic is recommended before making changes here.

Parameters#

dtfloat

The delta time.

update_score(dt)#

Update the score based on currently playing notes.

Parameters#

dtfloat

The delta time.