Page 1 of 1

Adabox019 macropad Simon says game

Posted: Thu Jul 29, 2021 7:03 pm
by wilsonRL
I made a Simon says game with the macropad from Adabox019. It's my first adabox and my first time writing a program that isn't a book question. I thoroughly enjoyed it, learned a lot and am ready to come up with a new game (it turns out that I don't really care for Simon says).

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)  

Re: Adabox019 macropad Simon says game

Posted: Fri Jul 30, 2021 11:13 am
by petronivs
Thanks for sharing this. I've been trying to think of an interesting use for this box that isn't just another smart keyboard wedge--it seems like that's what most of the examples focus around.

Re: Adabox019 macropad Simon says game

Posted: Mon Aug 16, 2021 9:15 pm
by kirksan
I like this example, this is my first Adabox and I’m learning how to program using it, I can follow your code and it makes sense, should learn a lot playing with it - thanks for sharing.

Re: Adabox019 macropad Simon says game

Posted: Tue Aug 17, 2021 11:11 pm
by adafruit_support_mike
Nice! Thanks for posting the code, and congratulations on learning from the project!

Re: Adabox019 macropad Simon says game

Posted: Wed Aug 18, 2021 10:27 am
by AnneBarela
Do you have a picture or video of the project? I'd like to put this on the Adafruit blog.

Re: Adabox019 macropad Simon says game

Posted: Sat Aug 21, 2021 9:20 pm
by wilsonRL
AnneBarela wrote:Do you have a picture or video of the project? I'd like to put this on the Adafruit blog.
Thank you for the positive feedback.

I just took a video of the project, but the file was too large to post.
I posted it to YouTube instead and you are welcome to use it: https://youtu.be/JwJJanlwwMc

Re: Adabox019 macropad Simon says game

Posted: Sat Aug 21, 2021 10:21 pm
by AnneBarela
perfect, thanks!

Re: Adabox019 macropad Simon says game

Posted: Sun Aug 22, 2021 11:23 am
by macewmi
Nice!

Re: Adabox019 macropad Simon says game

Posted: Tue Aug 24, 2021 7:06 pm
by jliu70
Saw this from Adafruit blog
https://blog.adafruit.com/2021/08/24/a- ... -adafruit/


Awesome job. This is really cool and inspiring.

Re: Adabox019 macropad Simon says game

Posted: Tue Aug 24, 2021 7:14 pm
by kirksan
I took the code you made and changed the names on the class and method definitions (to help me find where those items are used and understand how they work), once that was clear I added a function (method?) to blink the correct key after the player makes a mistake. I also made the game speed up as you play.
Thanks for sharing your code, this was very helpful, great work.

Code: Select all

"""
Modified from code for Simon Says for ADABOX 019 by Rebecca Wilson
"""

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)

#define a library of colors
colors = [RED,ORANGE,YELLOW,SPRING,GREEN,CYAN,SKY,CLOUD,BLUE,INDIGO,MAGENTA,PINK]

# define a library of sounds, these are individual notes in Hz
sounds = [196,220,246,262,294,330,349,392,440,494,523,587]

# create a class to keep track of key position, color and sound.  This class is 
# later used during gameplay to define the level, hence the name 'Level_info'
class Level_info:
    def __init__(self,position):
        self.key_position = position
        self.key_color = colors[position]
        self.key_sound = sounds[position]
    
    #define functions within the class to show which key to press, hide that key,
    #and when the player makes a mistake to blink the correct key
    def show(self):
        macropad.pixels[self.key_position] = self.key_color
        macropad.start_tone(self.key_sound)
        time.sleep(0.5/speed)
   
    def hide(self):
        macropad.pixels[self.key_position] = (0,0,0)
        macropad.stop_tone()
        time.sleep(0.5/speed)
    
    def blink(self):
        for x in range(0,4):
            macropad.pixels[self.key_position] = self.key_color
            macropad.start_tone(self.key_sound)
            time.sleep(0.2)
            macropad.pixels[self.key_position] = (0,0,0)
            macropad.stop_tone()
            time.sleep(0.2)
        time.sleep(0.5)
        
# shows game text
text_lines = macropad.display_text(title='Memory Game')
text_lines.show()

# main loop
while True:
    # define a starting speed, as level increases, so will speed
    speed = 1
    
    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:
        text_lines[0].text = ' '
        text_lines[1].text = ' '
        text_lines[2].text = ' '
        game_in_progress = True
        levels = []
        current_level = 1
        position_in_level = 0
        time.sleep(1.0)
   
        # 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)))
            
            text_lines[1].text = 'Level: {0}'.format(current_level)
            time.sleep(0.25)
            
            # shows player the order to press the buttons
            for place in levels:
                place.show()
                place.hide()
           
            # 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 and key_event.pressed:
                    if key_event.key_number is levels[position_in_level].key_position:
                        levels[position_in_level].show()
                        levels[position_in_level].hide()
                        position_in_level += 1
                    else:
                        text_lines[1].text = ' '
                        text_lines[2].text = 'GAME OVER!'
                        levels[position_in_level].blink()
                        text_lines[2].text = ' '
                        game_in_progress = False
                        speed = 1
                        break
           
            current_level += 1
            position_in_level = 0
            speed += .5
            time.sleep(1) 

Re: Adabox019 macropad Simon says game

Posted: Wed Aug 25, 2021 8:32 pm
by jliu70
Awesome update to the original.
I noticed that the code doesn't quite reset properly, so that after the game is over, when starting a new game, it doesn't quite work as any key press will result in game over.

I think it's related to the fact that while you reset the "speed" variable after "game over", the other variables also need to be reset.

Here's the updated code which seems to correct that tiny bug (although it seems to pop up now and again -- so I haven't quite fixed it):

Code: Select all

"""
Modified from code for Simon Says for ADABOX 019 by Rebecca Wilson

Required libraries:
        neopixel.mpy
        adafruit_debouncer.mpy
        adafruit_simple_text_display.mpy
        adafruit_macropad.mpy
        adafruit_midi
        adafruit_hid
        adafruit_display_text
"""

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)

#define a library of colors
colors = [RED,ORANGE,YELLOW,SPRING,GREEN,CYAN,SKY,CLOUD,BLUE,INDIGO,MAGENTA,PINK]

# define a library of sounds, these are individual notes in Hz
sounds = [196,220,246,262,294,330,349,392,440,494,523,587]

# create a class to keep track of key position, color and sound.  This class is 
# later used during gameplay to define the level, hence the name 'Level_info'
class Level_info:
    def __init__(self,position):
        self.key_position = position
        self.key_color = colors[position]
        self.key_sound = sounds[position]
    
    #define functions within the class to show which key to press, hide that key,
    #and when the player makes a mistake to blink the correct key
    def show(self):
        macropad.pixels[self.key_position] = self.key_color
        macropad.start_tone(self.key_sound)
        time.sleep(0.5/speed)
   
    def hide(self):
        macropad.pixels[self.key_position] = (0,0,0)
        macropad.stop_tone()
        time.sleep(0.5/speed)
    
    def blink(self):
        for x in range(0,4):
            macropad.pixels[self.key_position] = self.key_color
            macropad.start_tone(self.key_sound)
            time.sleep(0.2)
            macropad.pixels[self.key_position] = (0,0,0)
            macropad.stop_tone()
            time.sleep(0.2)
        time.sleep(0.5)
        
# shows game text
text_lines = macropad.display_text(title='Memory Game')
text_lines.show()

# main loop
while True:
    # define a starting speed, as level increases, so will speed
    speed = 1
    
    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:
        text_lines[0].text = ' '
        text_lines[1].text = ' '
        text_lines[2].text = ' '
        game_in_progress = True
        levels = []
        current_level = 1
        position_in_level = 0
        time.sleep(1.0)
   
        # 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)))
            
            text_lines[1].text = 'Level: {0}'.format(current_level)
            time.sleep(0.25)
            
            # shows player the order to press the buttons
            for place in levels:
                place.show()
                place.hide()
           
            # 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 and key_event.pressed:
                    if key_event.key_number is levels[position_in_level].key_position:
                        levels[position_in_level].show()
                        levels[position_in_level].hide()
                        position_in_level += 1
                    else:
                        text_lines[1].text = ' '
                        text_lines[2].text = 'GAME OVER!'
                        levels[position_in_level].blink()
                        text_lines[2].text = ' '
                        game_in_progress = False
                        speed = 1
                        levels = []
                        current_level = 1
                        position_in_level = 0
                        break
           
            current_level += 1
            position_in_level = 0
            speed += .5
            time.sleep(1) 
Thanks again!