Advertisement
kreket

Raspberry Pi led game.

Dec 15th, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. #First led game as a means of learning python/raspberry.
  2.  
  3. import RPi.GPIO as GPIO
  4. from time import sleep
  5. from random import seed
  6. from random import random
  7. from random import randint
  8.  
  9.  
  10. #Pin setup
  11. redpin = 2
  12. greenpin = 3
  13. buttonpin = 4
  14.  
  15.  
  16. #GPIO Setup
  17. GPIO.setmode(GPIO.BCM) #BCM Pin numbering
  18. GPIO.setwarnings(False) #Disable warnings
  19. GPIO.setup(redpin, GPIO.OUT, initial=GPIO.LOW) #Default led off
  20. GPIO.setup(greenpin, GPIO.OUT, initial=GPIO.LOW) #Default led off
  21. GPIO.setup(buttonpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Default button off
  22.  
  23.  
  24. #Variable Settings
  25. ledontime = 0.5 #The time led stays on, less is more difficult
  26. buttonpressed = False #Used to check if button has already been pressed
  27. buttoncheck = False #Used to check if the button has been pressed.
  28. maxwait = 10 #Max wait from game start to led blinks
  29. counter = 0 #Used to flash green led the amount of times won
  30. gamestarted = False #Used to check that the game is started, so that you can't cheat the game by pressing on ready go signal.
  31.  
  32.  
  33. #Resets all variables, used on loose()
  34. def resetVariables():
  35.     global gamestarted
  36.     global ledontime
  37.     global buttonpressed
  38.     global maxwait
  39.     global counter
  40.     global buttoncheck
  41.     buttoncheck = False
  42.     maxwait = 10
  43.     ledontime = 0.5
  44.     buttonpressed = False
  45.     counter = 0
  46.     gamestarted = False
  47.  
  48.  
  49. #Button press logic
  50. def buttonPressed(channel):
  51.     global gamestarted
  52.     global buttonpressed
  53.     global buttoncheck
  54.     if buttoncheck == False and gamestarted == True:
  55.         channel_on = GPIO.input(greenpin)
  56.         if channel_on == 1:
  57.             buttonpressed = True
  58.             buttoncheck = True
  59.         else:
  60.             buttonpressed = False
  61.             buttoncheck = True
  62.     else:
  63.         buttonpressed = False
  64.  
  65. GPIO.add_event_detect(buttonpin, GPIO.FALLING, callback=buttonPressed, bouncetime=150) #Button interrupt
  66.  
  67.  
  68. #Starts the game, checks button press status after.
  69. def gameOn():
  70.     global gamestarted
  71.     global ledontime
  72.     sleep(0.1)
  73.     seed(1)
  74.     rnd = round((random() + randint(0, maxwait)), 1)
  75.     ledShow()
  76.     gamestarted = True
  77.     sleep(rnd)
  78.     GPIO.output(greenpin, GPIO.HIGH)
  79.     sleep(ledontime)
  80.     GPIO.output(greenpin, GPIO.LOW)
  81.     gamestarted = False
  82.     checkPressed()
  83.  
  84.  
  85. #Checking if button was pressed at the right time.
  86. def checkPressed():
  87.     global buttonpressed
  88.     if buttonpressed == True:
  89.         checkwait()
  90.         checktimeon()
  91.         winner()
  92.     else:
  93.         looser()
  94.  
  95.  
  96. #Used to check maxwait, and make it more diffucult faster down to 4, and slowly further down to 2.
  97. def checkwait():
  98.     global maxwait
  99.     if maxwait > 4:
  100.         maxwait = maxwait - 2
  101.         print("maxwait: " + str(maxwait))
  102.     elif maxwait > 2 and maxwait < 4:
  103.         maxwait = maxwait - 1
  104.         print("maxwait: " + str(maxwait))
  105.  
  106.  
  107. #CHecks ledtime, decreases to increase difficulty down to 0.1 second
  108. def checktimeon():
  109.     global ledontime
  110.     if ledontime > 0.1:
  111.         ledontime = ledontime - 0.05
  112.         print("ledtime: " + str(ledontime))
  113.  
  114.  
  115. #Shows green flashes rapid on win, resets button checks and start next round.
  116. def winner():
  117.     global counter
  118.     global buttonpressed
  119.     global buttoncheck
  120.     counter = counter + 1
  121.     for x in range(0, 5):
  122.         GPIO.output(greenpin, GPIO.HIGH)
  123.         sleep(0.1)
  124.         GPIO.output(greenpin, GPIO.LOW)
  125.         sleep(0.1)
  126.     print("Wins so far: " + str(counter))
  127.     buttoncheck = False
  128.     buttonpressed = False
  129.     gameOn()
  130.  
  131.  
  132. #Flashes redpin on loose, restarts the game.
  133. def looser():
  134.     for x in range(0, 5):
  135.         GPIO.output(redpin, GPIO.HIGH)
  136.         sleep(0.1)
  137.         GPIO.output(redpin, GPIO.LOW)
  138.         sleep(0.1)
  139.     showscore()
  140.     resetVariables()
  141.     gameOn()
  142.  
  143.  
  144. #Blows red led while green flashes amount of times won.
  145. def showscore():
  146.     GPIO.output(redpin, GPIO.HIGH)
  147.     sleep(1)
  148.     for x in range(0, counter):
  149.         GPIO.output(greenpin, GPIO.HIGH)
  150.         sleep(0.2)
  151.         GPIO.output(greenpin, GPIO.LOW)
  152.         sleep(0.3)
  153.     sleep(1)
  154.     GPIO.output(redpin, GPIO.LOW)
  155.  
  156.  
  157. #Starting ledshow, red red green (Ready set go)
  158. def ledShow():
  159.     GPIO.output(greenpin, GPIO.LOW)
  160.     GPIO.output(redpin, GPIO.LOW)
  161.     sleep(1)
  162.     for x in range(0, 2):
  163.         GPIO.output(redpin, GPIO.HIGH)
  164.         sleep(0.4)
  165.         GPIO.output(redpin, GPIO.LOW)
  166.         sleep(0.4)
  167.     GPIO.output(greenpin, GPIO.HIGH)
  168.     sleep(0.6)
  169.     GPIO.output(greenpin, GPIO.LOW)
  170.  
  171.  
  172. try:
  173.     while True:
  174.         gameOn()
  175.  
  176. finally:
  177.     #Turn off leds at exit/error.
  178.     GPIO.output(redpin, GPIO.LOW)
  179.     GPIO.output(greenpin, GPIO.LOW)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement