Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here's one way to layout this circuit: GND 5V. E Raspberry Pi GPIO Extension Board D. 1... GND. 3V3. Simon The parts of the activity
Here's one way to layout this circuit: GND 5V. E Raspberry Pi GPIO Extension Board D. 1... GND. 3V3. Simon The parts of the activity that you have already implemented provide almost all that is needed to lay the base for the game. The only thing left to add is a way to generate a random sequence of colors, allow the player to push buttons that correspond the to sequence, check if the player's sequence matches the one in the game, and either grow the sequence or end the game! First, here's the code: 1 import RPi.GPIO as GPIO 2 from time import sleep 3 from random import randint 4 import pygame 5 6 # set to True to enable debugging output DEBUG = False 7 8 # initialize the pygame library pygame.init() 9 10 11 12 13 14 15 # set the GPIO pin numbers # the switches (from L to R) switches = [ 20, 16, 12, 26 ] # the LEDs (from L to R) leds = [6, 13, 19, 21 ] # the sounds that map to each LED (from L to R) sounds = [ pygame.mixer.Sound ("one.wav"), pygame.mixer.Sound ("two.wav"), pygame.mixer. Sound ("three.wav") pygame.mixer.Sound ("four.wav") ] 16 17 # use the Broadcom pin mode GPIO.setmode (GPIO.BCM) 18 19 20 # setup the input and output pins GPIO.setup (switches, GPIO.IN, pull_up_down=GPIO. PUD_DOWN) GPIO.setup (leds, GPIO.OUT) 21 22 23 24 # this function turns the LEDs on def all on (): for i in leds: GPIO.output (leds, True) 25 26 27 28 # this function turns the LEDs off def all off(): for i in leds: GPIO.output (leds, False) 29 30 31 32 33 34 35 # this functions flashes the LEDs a few times when the player loses the game def lose(): for i in range (0, 4): all on() sleep (0.5) all off () sleep (0.5) 36 37 38 # the main part of the program # initialize the simon sequence # each item in the sequence represents an LED (or switch), indexed at o through 3 seq = [] # randomly add the first two items to the sequence seq.append (randint(0, 3)) 39 40 41 42 seq.append (randint (0, 3)) 43 44 print ("Welcome to Simon!") print ("Try to play the sequence back by pressing the switches.") print("Press Ctrl+C to exit...") 45 8 46 47 48 49 50 51 52 53 54 55 56 57 # we'll discuss this later, but this allows us to detect # when Ctrl+C is pressed so that we can reset the GPIO pins try: # keep going until the user presses Ctrl+C while (True) : # randomly add one more item to the sequence seq.append (randint (0, 3)) if (DEBUG) : # display the sequence to the console if (len (seq) > 3): print() print("seq={}".format (seq)) 58 59 60 61 62 63 64 65 66 67 # display the sequence using the LEDs for sin seq: # turn the appropriate LED on GPIO.output (leds [s], True) # play its corresponding sound sounds [S] .play ( ) # wait and turn the LED off again sleep (1) GPIO.output (leds [s], False) sleep (0.5) 68 69 70 71 72 73 74 75 76 # wait for player input (via the switches) # initialize the count of switches pressed to 0 switch count = 0 # keep accepting player input until the number of items in the sequence is reached while (switch_count 3): print() print("seq={}".format (seq)) 58 59 60 61 62 63 64 65 66 67 # display the sequence using the LEDs for sin seq: # turn the appropriate LED on GPIO.output (leds [s], True) # play its corresponding sound sounds [S] .play ( ) # wait and turn the LED off again sleep (1) GPIO.output (leds [s], False) sleep (0.5) 68 69 70 71 72 73 74 75 76 # wait for player input (via the switches) # initialize the count of switches pressed to 0 switch count = 0 # keep accepting player input until the number of items in the sequence is reached while (switch_count
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started