Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  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
  7. DEBUG = False
  8.  
  9. # initialize the pgame library
  10. pygame.init()
  11.  
  12. # set the GPIO pin numbers
  13. # the LED's from L to R
  14. LEDS = [6, 13, 19, 21]
  15. buttons = [20, 16, 12, 26]
  16.  
  17. # the sounds that map each LED (from L to R)
  18. sounds = [pygame.mixer.Sound("one.wav"),\
  19.           pygame.mixer.Sound("two.wav"), \
  20.           pygame.mixer.Sound("three.wav"),\
  21.           pygame.mixer.Sound("four.wav")]
  22.  
  23. # Use the Broadcom pin mode
  24. GPIO.setmode(GPIO.BCM)
  25.  
  26. # setup the input and output pins
  27. GPIO.setup(LEDS, GPIO.OUT)
  28. GPIO.setup(buttons, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
  29.  
  30. # this function turns the LED's on
  31. def all_on():
  32.     for i in LEDS:
  33.         GPIO.output(LEDS, True)
  34.  
  35. # this function turns the LED's off
  36. def all_off():
  37.     for i in LEDS:
  38.         GPIO.output(LEDS, False)
  39.  
  40. # this function flashes he LED's a couple times when \
  41. # the player loses the game.
  42. def lose():
  43.     for i in range(0, 4):
  44.         all_on()
  45.         sleep(0.5)
  46.         all_off()
  47.         sleep(0.5)
  48.  
  49.     if (seqs > 1):
  50.         print("You lost lol. You made it to a sequence of {}.").format(seqs)
  51.     else:
  52.         print("You didnt even make it to a sequence.")
  53.        
  54.  
  55. # the main part of the program
  56. # initialize the Simon sequence
  57. # each item in the sequence represents an LED (or switch),
  58. # indexed at 0 through 3
  59.  
  60. seq = []
  61. seqs = 0
  62.  
  63. # Randomly add the first two items to the sequence
  64. seq.append(randint(0, 3))
  65. seq.append(randint(0, 3))
  66.  
  67. print("Welcome to Simon")
  68. print("Try to play the sequence back by pressing the switches.")
  69. print("Press CNTRL+C to exit...")
  70.  
  71. # Detect when CTRL+C is pressed so that we can reset the GPIO pins
  72.  
  73. try:
  74.     # keep going until the user presses Ctrl+C
  75.    
  76.     while (True):
  77.         # randomly add one more item to the sequence
  78.         seqs += 1
  79.         seq.append(randint(0, 3))
  80.         if (DEBUG):
  81.             if (len(seq) > 3):
  82.                 print
  83.             print("seq={}").format(seq)
  84.  
  85.         if (len(seq) < 4):
  86.             for s in seq:
  87.                 # turn the appropriate LED on
  88.                 GPIO.output(LEDS[s], True)
  89.                 # play its corresponding sound
  90.                 sounds[s].play()
  91.                 # wait and turn the LED off again
  92.                 sleep(1)
  93.                 GPIO.output(LEDS[s], False)
  94.                 sleep(0.5)
  95.         elif (len(seq) >= 5 and len(seq) < 7):
  96.             for s in seq:
  97.                 # turn the appropriate LED on
  98.                 GPIO.output(LEDS[s], True)
  99.                 # play its corresponding sound
  100.                 sounds[s].play()
  101.                 # wait and turn the LED off again
  102.                 sleep(.9)
  103.                 GPIO.output(LEDS[s], False)
  104.                 sleep(0.4)
  105.         elif (len(seq) >= 7 and len(seq) < 10):
  106.             for s in seq:
  107.                 # turn the appropriate LED on
  108.                 GPIO.output(LEDS[s], True)
  109.                 # play its corresponding sound
  110.                 sounds[s].play()
  111.                 # wait and turn the LED off again
  112.                 sleep(.8)
  113.                 GPIO.output(LEDS[s], False)
  114.                 sleep(0.3)
  115.         elif (len(seq) >= 10 and len(seq) < 13):
  116.             for s in seq:
  117.                 # turn the appropriate LED on
  118.                 GPIO.output(LEDS[s], True)
  119.                 # play its corresponding sound
  120.                 sounds[s].play()
  121.                 # wait and turn the LED off again
  122.                 sleep(.7)
  123.                 GPIO.output(LEDS[s], False)
  124.                 sleep(0.25)
  125.         elif (len(seq) >= 13 and len(seq) < 15):
  126.             for s in seq:
  127.                 # turn the appropriate LED on
  128.                 GPIO.output(LEDS[s], True)
  129.                 # play its corresponding sound
  130.                 sounds[s].play()
  131.                 # wait 6nd turn the LED off again
  132.                 sleep(.6)
  133.                 GPIO.output(LEDS[s], False)
  134.                 sleep(0.15)
  135.         else:
  136.             if (len(seq) >= 15):
  137.                 for s in seq:
  138.                     # play its corresponding sound
  139.                     sounds[s].play()
  140.                     # wait and turn the LED off again
  141.                     sleep(.6)
  142.                     GPIO.output(LEDS[s], False)
  143.                     sleep(0.15)
  144.                
  145.  
  146.         # wait for player input (via the switches)
  147.         #initialize the count of switches pressed to 0
  148.         switch_count = 0
  149.         # keep accepting player input until the number of terms
  150.         # in the sequence is reached
  151.         while (switch_count < len(seq)):
  152.             # initially note that no switch is pressed
  153.             # this will help with switch debouncing
  154.             pressed = False
  155.             # so long as no switch is currently pressed...
  156.             while (not pressed):
  157.                 # ...we can check the status of each switch
  158.                 for i in range(len(buttons)):
  159.                     # if one switch is pressed
  160.                     while (GPIO.input(buttons[i]) == True):
  161.                         # note it index
  162.                         val = i
  163.                         # note that the switch has now been pressed
  164.                     # so that we dont detenct any more switch presses
  165.                         pressed = True
  166.             if (DEBUG):
  167.                 # display the index of the switch pressed
  168.                 print val,
  169.             # light the matching LED
  170.             GPIO.output(LEDS[val], True)
  171.             # play its correspionding sound
  172.             sounds[val].play()
  173.             # wait and turn the LED off again
  174.             sleep(1)
  175.             GPIO.output(LEDS[val], False)
  176.             sleep(0.25)
  177.  
  178.             # check to see if this LED is correct in the sequence
  179.             if (val != seq[switch_count]):
  180.                 # player is incorrect invoke loss function.
  181.                 lose()
  182.                 #reset the GPIO pins
  183.                 GPIO.cleanup()
  184.                 #exit the game
  185.                 exit(0)
  186.  
  187.             # if the player has this item correct, increment the count
  188.             switch_count += 1
  189. # detect CTRL+C
  190. except KeyboardInterrupt:
  191.     # reset the GPIO pins
  192.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement