Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 pgame library
- pygame.init()
- # set the GPIO pin numbers
- # the LED's from L to R
- LEDS = [6, 13, 19, 21]
- buttons = [20, 16, 12, 26]
- # the sounds that map 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")]
- # Use the Broadcom pin mode
- GPIO.setmode(GPIO.BCM)
- # setup the input and output pins
- GPIO.setup(LEDS, GPIO.OUT)
- GPIO.setup(buttons, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
- # this function turns the LED's on
- def all_on():
- for i in LEDS:
- GPIO.output(LEDS, True)
- # this function turns the LED's off
- def all_off():
- for i in LEDS:
- GPIO.output(LEDS, False)
- # this function flashes he LED's a couple 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)
- if (seqs > 1):
- print("You lost lol. You made it to a sequence of {}.").format(seqs)
- else:
- print("You didnt even make it to a sequence.")
- # 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 = []
- seqs = 0
- # 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 the switches.")
- print("Press CNTRL+C to exit...")
- # 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
- seqs += 1
- seq.append(randint(0, 3))
- if (DEBUG):
- if (len(seq) > 3):
- print
- print("seq={}").format(seq)
- if (len(seq) < 4):
- for s in 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)
- elif (len(seq) >= 5 and len(seq) < 7):
- for s in 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(.9)
- GPIO.output(LEDS[s], False)
- sleep(0.4)
- elif (len(seq) >= 7 and len(seq) < 10):
- for s in 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(.8)
- GPIO.output(LEDS[s], False)
- sleep(0.3)
- elif (len(seq) >= 10 and len(seq) < 13):
- for s in 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(.7)
- GPIO.output(LEDS[s], False)
- sleep(0.25)
- elif (len(seq) >= 13 and len(seq) < 15):
- for s in seq:
- # turn the appropriate LED on
- GPIO.output(LEDS[s], True)
- # play its corresponding sound
- sounds[s].play()
- # wait 6nd turn the LED off again
- sleep(.6)
- GPIO.output(LEDS[s], False)
- sleep(0.15)
- else:
- if (len(seq) >= 15):
- for s in seq:
- # play its corresponding sound
- sounds[s].play()
- # wait and turn the LED off again
- sleep(.6)
- GPIO.output(LEDS[s], False)
- sleep(0.15)
- # 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 terms
- # in the sequence is reached
- while (switch_count < len(seq)):
- # initially note that no switch is pressed
- # this will help with switch debouncing
- pressed = False
- # so long as no switch is currently pressed...
- while (not pressed):
- # ...we can check the status of each switch
- for i in range(len(buttons)):
- # if one switch is pressed
- while (GPIO.input(buttons[i]) == True):
- # note it index
- val = i
- # note that the switch has now been pressed
- # so that we dont detenct any more switch presses
- pressed = True
- if (DEBUG):
- # display the index of the switch pressed
- print val,
- # light the matching LED
- GPIO.output(LEDS[val], True)
- # play its correspionding sound
- sounds[val].play()
- # 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 loss function.
- lose()
- #reset the GPIO pins
- GPIO.cleanup()
- #exit the game
- exit(0)
- # if the player has this item correct, increment the count
- switch_count += 1
- # detect CTRL+C
- except KeyboardInterrupt:
- # reset the GPIO pins
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement