In case anyone wants to try it, here is my code:
Code: Select all
"""
Simon Says for Adabox019 MACROPAD
Rebecca Wilson
July 29,2021
"""
from adafruit_macropad import MacroPad
import random
import time
macropad = MacroPad()
macropad.pixels.brightness = 0.3
# key colors
RED = (255,0,0)
ORANGE = (255,25,0)
YELLOW = (255, 200, 0)
SPRING = (0,255,50)
GREEN = (0,255,0)
CYAN = (0,255,125)
SKY = (0,255,255)
CLOUD = (0,125,255)
BLUE = (0,0,255)
INDIGO = (50,0,255)
MAGENTA = (125,0,125)
PINK = (255,0,50)
colors = [RED,ORANGE,YELLOW,SPRING,GREEN,CYAN,SKY,CLOUD,BLUE,INDIGO,MAGENTA,PINK]
# key sounds
sounds = [196,220,246,262,294,33,349,392,440,494,523,587]
# class to keep track of key position, color and sound
class Level_info:
def __init__(self,location):
self.key_location = location
self.neo_color = colors[location]
self.sound = sounds[location]
def show(self):
macropad.pixels[self.key_location] = self.neo_color
macropad.start_tone(self.sound)
time.sleep(0.5)
def hide(self):
macropad.pixels[self.key_location] = (0,0,0)
macropad.stop_tone()
time.sleep(0.5)
# shows game text
text_lines = macropad.display_text(title='Memory Game')
text_lines.show()
# main loop
while True:
text_lines[0].text = 'To start: '
text_lines[1].text = 'press encoder button.'
macropad.encoder_switch_debounced.update()
# initialize and reset game
if macropad.encoder_switch_debounced.pressed:
game_in_progress = True
levels = []
current_level = 1
text_lines[0].text = ' '
text_lines[1].text = 'Begin level: '.format(current_level)
text_lines[2].text = ' '
position_in_level = 0
time.sleep(2)
# game loop
while game_in_progress:
# each time through the loop - create and store a new Level_info object at the end of the levels list
levels.append(Level_info(random.randint(0,11)))
# shows player the order to press the buttons
for place in levels:
place.show()
place.hide()
text_lines[1].text = 'Begin level: {0}'.format(current_level)
# loop that checks whether player presses the correct buttons in the correct order
while position_in_level < len(levels):
key_event = macropad.keys.events.get()
if key_event:
if key_event.pressed:
if key_event.key_number is levels[position_in_level].key_location:
levels[position_in_level].show()
levels[position_in_level].hide()
position_in_level += 1
else:
text_lines[2].text = 'GAME OVER!'
game_in_progress = False
break
current_level += 1
position_in_level = 0
time.sleep(1)