Question
Here is the entire python code import RPi.GPIO as GPIO from time import sleep from random import randint import pygame # set to True to
Here is the entire python code
import RPi.GPIO as GPIO from time import sleep from random import randint import pygame
# set to True to enable debugging output DEBUG = False # initialize the pygame library pygame.init()
# 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 ]
# use the Broadcom pin mode GPIO.setmode(GPIO.BCM)
# setup the input and output pins GPIO.setup(switches, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(leds, GPIO.OUT)
# this function turns the LEDs on def all_on(): for i in leds: GPIO.output(leds, True) # this function turns the LEDs off def all_off(): for i in leds: GPIO.output(leds, False)
# 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)
# the main part of the program # initialize the Simon sequence # each item in the sequence represents an LED (or switch), indexed at 0 through 3 seq = [] # randomly add the first two items to the sequence seq.append(randint(0, 3)) seq.append(randint(0, 3)) print("Welcome to Simon!") print("Try to play the sequence back by pressing theswitches.") print("Press Ctrl+C to exit...")
# 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))
# display the sequence using the LEDs for s in seq: # turn the appropriate LED on GPIO.output(leds[s], True) # wait and turn the LED off again sleep(1) GPIO.output(leds[s], False) sleep(0.5)
# 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
if (DEBUG): # display the index of the switch pressed print(val),
# light the matching LED GPIO.output(leds[val], True) # wait and turn the LED off again sleep(1) GPIO.output(leds[val], False) sleep(0.25)
# check to see if this LED is correct in the sequence if (val != seq[switch_count]): # player is incorrect; invoke the lose function lose() # reset the GPIO pins GPIO.cleanup() # exit the game exit(0)
# if the player has this item in the sequence correct, increment the count switch_count += 1 # detect Ctrl+C except KeyboardInterrupt: # reset the GPIO pins GPIO.cleanup()
I have continuously tried to do these two questions but I have no idea where to place the counter for the sequence string in number 1 and I also do not understand how to implement 2
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! Here's one way to layout this circuit: GND 5V. E Raspberry Pi GPIO Extension Board D. 1... GND. 3V3. Homework: Simon For the homework portion of this activity, you may have the option to work in groups (pending prof approval). It is suggested that groups contain at least one confident Python coder. The first part of this activity is to implement the Simon program in this activity. Please work to understand the algorithm and source code instead of merely typing it in (or, worse, using a copy/paste process). Once your Simon game is working properly, you can move on the the second part of this activity. For the second part of this activity, you must implement several improvements: (1) A scoring mechanism. When the player makes a mistake and the game ends, output a message similar to, You made it to a sequence of 9! Remember that failing at the start should output something like, You made it to a sequence of O! or, You didn't even make it to a sequence! (2) Over time, increase the speed of the playing sequence. As the player is more and more successful, increase the speed of the sequence as it is played back to the player. The time spent playing each note in a sequence is currently 1s. Furthermore, the delay in between playing the notes is currently 0.5s. Modify this as follows: Once the sequence gets to five notes, the time spent playing each each note should be decreased to 0.9s; the delay in between playing the notes should be decreased to 0.4s. Once the sequence gets to seven notes, the time spent playing each each note should be decreased to 0.8s; the delay in between playing the notes should be decreased to 0.3s. Once the sequence gets to ten notes, the time spent playing each each note should be decreased to 0.7s; the delay in between playing the notes should be decreased to 0.25s. Once the sequence gets to thirteen notes, the time spent playing each each note should be decreased to 0.6s; the delay in between playing the notes should be decreased to 0.15s. 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! Here's one way to layout this circuit: GND 5V. E Raspberry Pi GPIO Extension Board D. 1... GND. 3V3. Homework: Simon For the homework portion of this activity, you may have the option to work in groups (pending prof approval). It is suggested that groups contain at least one confident Python coder. The first part of this activity is to implement the Simon program in this activity. Please work to understand the algorithm and source code instead of merely typing it in (or, worse, using a copy/paste process). Once your Simon game is working properly, you can move on the the second part of this activity. For the second part of this activity, you must implement several improvements: (1) A scoring mechanism. When the player makes a mistake and the game ends, output a message similar to, You made it to a sequence of 9! Remember that failing at the start should output something like, You made it to a sequence of O! or, You didn't even make it to a sequence! (2) Over time, increase the speed of the playing sequence. As the player is more and more successful, increase the speed of the sequence as it is played back to the player. The time spent playing each note in a sequence is currently 1s. Furthermore, the delay in between playing the notes is currently 0.5s. Modify this as follows: Once the sequence gets to five notes, the time spent playing each each note should be decreased to 0.9s; the delay in between playing the notes should be decreased to 0.4s. Once the sequence gets to seven notes, the time spent playing each each note should be decreased to 0.8s; the delay in between playing the notes should be decreased to 0.3s. Once the sequence gets to ten notes, the time spent playing each each note should be decreased to 0.7s; the delay in between playing the notes should be decreased to 0.25s. Once the sequence gets to thirteen notes, the time spent playing each each note should be decreased to 0.6s; the delay in between playing the notes should be decreased to 0.15sStep 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