#ISM 6106
#Ruben Yanez
#Lab 2
#Color memory game
import time
import random
from Log import *
from Lights import *
from Button import *
from LightStrip import *
from Buzzer import *
from Displays import *
Log.level=NONE # Set log level to NONE for quiet operation
class GameHardware: #Class created
"""
A class to encapsulate the hardware setup of the game.
"""
def __init__(self, lightstrip_pin=2, buzzer_pin=17, display_sda=0, display_scl=1):
"""
Initializes the hardware components: LightStrip, Buzzer, and LCDDisplay.
"""
self.lightstrip = LightStrip(pin=lightstrip_pin, name="Light Strip", numleds=4, brightness=1)
self.buzzer = PassiveBuzzer(pin=buzzer_pin, name="Buzzer")
self.display = LCDDisplay(sda=display_sda, scl=display_scl)
class ColorSequenceGenerator: #Class created
"""
A class responsible for generating random color sequences for the game.
"""
def __init__(self, colors=[RED, GREEN, YELLOW, BLUE]):
"""
Initializes the colors to be used in the sequences.
"""
self.colors = colors
def generate_sequence(self, length):
"""
Generates a random sequence of colors with the given length.
"""
sequence = []
for _ in range(length):
sequence.append(random.choice(self.colors))
return sequence
class PlayerInput: #Class created
"""
A class to manage player input using buttons.
"""
def __init__(self, red_button_pin=22, green_button_pin=21,
yellow_button_pin=13, blue_button_pin=10, handler=None):
"""
Initializes the buttons and sets up their handlers.
"""
self.red_button = Button(red_button_pin, 'Red Button', handler=handler)
self.green_button = Button(green_button_pin, 'Green Button', handler=handler)
self.yellow_button = Button(yellow_button_pin, 'Yellow Button', handler=handler)
self.blue_button = Button(blue_button_pin, 'Blue Button', handler=handler)
self.input_sequence = []
def clear_input(self):
"""
Clears the current input sequence.
"""
self.input_sequence = []
def get_input(self):
"""
Returns the current input sequence.
"""
return self.input_sequence
class ColorMemoryGame: #Class created
"""
The main class for the Color Memory Game, managing game logic and flow.
"""
def __init__(self):
"""
Initializes the game components and sets up the starting level.
"""
self.hardware = GameHardware()
self.sequence_generator = ColorSequenceGenerator()
self.player_input = PlayerInput(handler=self)
self.current_level = 1
self.sequence = []
def buttonPressed(self, name):
"""
Handles button press events, appending the corresponding color to the player's input.
"""
color_map = {
'Red Button': RED,
'Green Button': GREEN,
'Yellow Button': YELLOW,
'Blue Button': BLUE
}
self.player_input.input_sequence.append(color_map[name])
self.check_input()
def buttonReleased(self, name):
"""
Not used, but required by the Button class.
"""
pass
def update_display(self):
"""
Updates the LCD display with the current level.
"""
self.hardware.display.clear()
self.hardware.display.showText(f"Level: {self.current_level}", row=0)
def generate_sequence(self):
"""
Generates a new color sequence for the current level.
"""
self.sequence = self.sequence_generator.generate_sequence(self.current_level)
def play_sequence(self):
"""
Plays the generated color sequence on the LightStrip with sound feedback.
"""
self.hardware.display.showText("Watch!", row=1)
time.sleep(1) # Brief pause before playing the sequence
self.hardware.display.clear()
self.update_display()
for color in self.sequence:
self.hardware.lightstrip.setColor(color)
self.hardware.buzzer.beep(tone=800, duration=300)
time.sleep(0.2)
self.hardware.lightstrip.setColor(BLACK)
time.sleep(0.5)
def check_input(self):
"""
Checks the player's input against the generated sequence and provides feedback.
"""
if self.player_input.get_input() == self.sequence:
self.hardware.buzzer.beep(tone=1000, duration=100) # Correct sequence
self.current_level += 1
self.player_input.clear_input()
self.update_display()
time.sleep(1)
self.generate_sequence()
self.play_sequence()
elif len(self.player_input.get_input()) == len(self.sequence):
self.hardware.buzzer.beep(tone=200, duration=500) # Wrong sequence
self.player_input.clear_input()
self.update_display()
self.hardware.display.showText("Try Again", row=1)
time.sleep(1)
self.play_sequence()
def game_loop(self):
"""
Starts and runs the main game loop.
"""
self.update_display()
self.generate_sequence()
self.play_sequence()
while True:
time.sleep(0.1)
# Create and start the game
game = ColorMemoryGame()
game.game_loop()