Advertisement
JXSeaton

Game - Simon for Raspberry Pi

Oct 31st, 2020
2,694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. import RPi.GPIO as GPIO
  5. GPIO.setmode (GPIO.BOARD)
  6. GPIO.setwarnings(False)
  7.  
  8. red = 18
  9. yellow = 22
  10. green = 24
  11. blue = 26
  12.  
  13. GPIO.setup(red, GPIO.OUT)
  14. GPIO.setup(yellow, GPIO.OUT)
  15. GPIO.setup(green, GPIO.OUT)
  16. GPIO.setup(blue, GPIO.OUT)
  17.  
  18. GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  19. GPIO.setup(36, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  20. GPIO.setup(38, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  21. GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  22.  
  23. game = True
  24.  
  25. pattern = []
  26. lights = [red, yellow, green, blue]
  27.  
  28. while game:
  29.     pattern.append(random.randint(0,3))
  30.    
  31.     for x in pattern:
  32.         GPIO.output(lights[x], GPIO.HIGH)
  33.         time.sleep(1)
  34.         GPIO.output(lights[x], GPIO.LOW)
  35.         time.sleep(0.5)
  36.    
  37.     for x in pattern:
  38.        
  39.         waitingForInput = True
  40.        
  41.         while waitingForInput:
  42.             redButtonState = GPIO.input(32)
  43.             yellowButtonState = GPIO.input(36)
  44.             greenButtonState = GPIO.input(38)
  45.             blueButtonState = GPIO.input(40)
  46.            
  47.             if redButtonState == 0:
  48.                 GPIO.output(red, GPIO.HIGH)
  49.                 waitingForInput = False
  50.                 if x != 0:
  51.                     game = False
  52.                 time.sleep(1)
  53.                 GPIO.output(red, GPIO.LOW)
  54.                
  55.             if yellowButtonState == 0:
  56.                 GPIO.output(yellow, GPIO.HIGH)
  57.                 waitingForInput = False
  58.                 if x != 1:
  59.                     game = False
  60.                 time.sleep(1)
  61.                 GPIO.output(yellow, GPIO.LOW)
  62.            
  63.             if greenButtonState == 0:
  64.                 GPIO.output(green, GPIO.HIGH)
  65.                 waitingForInput = False
  66.                 if x != 2:
  67.                     game = False
  68.                 time.sleep(1)
  69.                 GPIO.output(green, GPIO.LOW)
  70.            
  71.             if blueButtonState == 0:
  72.                 GPIO.output(blue, GPIO.HIGH)
  73.                 waitingForInput = False
  74.                 if x != 3:
  75.                     game = False
  76.                 time.sleep(1)
  77.                 GPIO.output(blue, GPIO.LOW)
  78.        
  79.     time.sleep(1)
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement