Follow The LED with Raspberry Pi Pico and Pico Display Part 1(Python)

Python3 code to show random number selection

With a Raspberry Pi Pico and a Pimoroni Pico Display in hand, I have decided to try and make a game. The Pico Display has a colour LED, so for my first attempt I decided to utilise it for a game of “Follow The LED”. The game is similar to one most people play as a child – the Pico will flash the LED a certain colour, then the player needs to input that colour. After the player successfully mimics the Picos choice, the Pico adds a new LED colour to the sequence, and the player has to mimic the extended sequence. This repeats until the player fails to match the sequence.

For example;

Turn 1:

Pico flashes red LED

User presses button to flash red LED

Turn 2:

Pico flashes red LED, blue LED.

Users pressed button for red LED, then blue LED.

Turn XX:

Pico flashes ….

Users inputs incorrect sequence and game ends.

I looked at using the Pico Display LED in a previous blog post ( https://geektechstuff.com/2021/01/24/the-raspberry-pi-pico-pimoroni-pico-display/ ) so already have the Python to activate LEDs and set them via button input. I’m going to use Red, Green, Blue and have one of Pico Display’s buttons act as an exit for the game (incase the user wants to end the game early).

For my first attempt at writing the game I decided to look at the programming needed to just to get Python to choose a random number, add that number to a sequence, ask the player to repeat the sequence and for Python to confirm if the two sequences match. To do this I wrote some Python to run on my computer, with appropriate print commands (these would need removing after testing).


# geektechstuff "Follow The LED" game for Raspberry Pico and Pimoroni Pico Display

import random

def main():
    pico_selection = []
    player_selection = []
    player_score = 0
    while True:
        pico_selection.append(pico_rand())
        print(f"pico_selection is {pico_selection}")
        pico_length = len(pico_selection)
        user_length = 0
        # Takes user input to match number of numbers chosen by Pico
        while user_length < pico_length:
            user_guess = input()
            user_length += 1
            player_selection.append(int(user_guess))
        print(f"player_selection is {player_selection}")
        # Checks to see if the user selections match the Pico's selections
        if pico_selection != player_selection:
            print("Game Over")
            print(f"Score: {player_score}")
            break
        # If pico_selection and player_selection match then game continues
        player_score +=1
        player_selection = []
        print(f"Score: {player_score}")


def pico_rand():
    # picks a random integer between 0 and 3 (LED Colours on Pimoroni Pico Display)
    selection = random.randint(0,3)
    return(selection)

if __name__ == "__main__" :
    main()

With the basic Python figured out I will be looking to transfer it to the Raspberry Pi Pico and add the functionality of the Pico Display LED / buttons in part 2.

Python3 code to show random number selection
Using Python to choose a random number, then asking the player to match the number sequence