Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #First led game as a means of learning python/raspberry.
- import RPi.GPIO as GPIO
- from time import sleep
- from random import seed
- from random import random
- from random import randint
- #Pin setup
- redpin = 2
- greenpin = 3
- buttonpin = 4
- #GPIO Setup
- GPIO.setmode(GPIO.BCM) #BCM Pin numbering
- GPIO.setwarnings(False) #Disable warnings
- GPIO.setup(redpin, GPIO.OUT, initial=GPIO.LOW) #Default led off
- GPIO.setup(greenpin, GPIO.OUT, initial=GPIO.LOW) #Default led off
- GPIO.setup(buttonpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Default button off
- #Variable Settings
- ledontime = 0.5 #The time led stays on, less is more difficult
- buttonpressed = False #Used to check if button has already been pressed
- buttoncheck = False #Used to check if the button has been pressed.
- maxwait = 10 #Max wait from game start to led blinks
- counter = 0 #Used to flash green led the amount of times won
- gamestarted = False #Used to check that the game is started, so that you can't cheat the game by pressing on ready go signal.
- #Resets all variables, used on loose()
- def resetVariables():
- global gamestarted
- global ledontime
- global buttonpressed
- global maxwait
- global counter
- global buttoncheck
- buttoncheck = False
- maxwait = 10
- ledontime = 0.5
- buttonpressed = False
- counter = 0
- gamestarted = False
- #Button press logic
- def buttonPressed(channel):
- global gamestarted
- global buttonpressed
- global buttoncheck
- if buttoncheck == False and gamestarted == True:
- channel_on = GPIO.input(greenpin)
- if channel_on == 1:
- buttonpressed = True
- buttoncheck = True
- else:
- buttonpressed = False
- buttoncheck = True
- else:
- buttonpressed = False
- GPIO.add_event_detect(buttonpin, GPIO.FALLING, callback=buttonPressed, bouncetime=150) #Button interrupt
- #Starts the game, checks button press status after.
- def gameOn():
- global gamestarted
- global ledontime
- sleep(0.1)
- seed(1)
- rnd = round((random() + randint(0, maxwait)), 1)
- ledShow()
- gamestarted = True
- sleep(rnd)
- GPIO.output(greenpin, GPIO.HIGH)
- sleep(ledontime)
- GPIO.output(greenpin, GPIO.LOW)
- gamestarted = False
- checkPressed()
- #Checking if button was pressed at the right time.
- def checkPressed():
- global buttonpressed
- if buttonpressed == True:
- checkwait()
- checktimeon()
- winner()
- else:
- looser()
- #Used to check maxwait, and make it more diffucult faster down to 4, and slowly further down to 2.
- def checkwait():
- global maxwait
- if maxwait > 4:
- maxwait = maxwait - 2
- print("maxwait: " + str(maxwait))
- elif maxwait > 2 and maxwait < 4:
- maxwait = maxwait - 1
- print("maxwait: " + str(maxwait))
- #CHecks ledtime, decreases to increase difficulty down to 0.1 second
- def checktimeon():
- global ledontime
- if ledontime > 0.1:
- ledontime = ledontime - 0.05
- print("ledtime: " + str(ledontime))
- #Shows green flashes rapid on win, resets button checks and start next round.
- def winner():
- global counter
- global buttonpressed
- global buttoncheck
- counter = counter + 1
- for x in range(0, 5):
- GPIO.output(greenpin, GPIO.HIGH)
- sleep(0.1)
- GPIO.output(greenpin, GPIO.LOW)
- sleep(0.1)
- print("Wins so far: " + str(counter))
- buttoncheck = False
- buttonpressed = False
- gameOn()
- #Flashes redpin on loose, restarts the game.
- def looser():
- for x in range(0, 5):
- GPIO.output(redpin, GPIO.HIGH)
- sleep(0.1)
- GPIO.output(redpin, GPIO.LOW)
- sleep(0.1)
- showscore()
- resetVariables()
- gameOn()
- #Blows red led while green flashes amount of times won.
- def showscore():
- GPIO.output(redpin, GPIO.HIGH)
- sleep(1)
- for x in range(0, counter):
- GPIO.output(greenpin, GPIO.HIGH)
- sleep(0.2)
- GPIO.output(greenpin, GPIO.LOW)
- sleep(0.3)
- sleep(1)
- GPIO.output(redpin, GPIO.LOW)
- #Starting ledshow, red red green (Ready set go)
- def ledShow():
- GPIO.output(greenpin, GPIO.LOW)
- GPIO.output(redpin, GPIO.LOW)
- sleep(1)
- for x in range(0, 2):
- GPIO.output(redpin, GPIO.HIGH)
- sleep(0.4)
- GPIO.output(redpin, GPIO.LOW)
- sleep(0.4)
- GPIO.output(greenpin, GPIO.HIGH)
- sleep(0.6)
- GPIO.output(greenpin, GPIO.LOW)
- try:
- while True:
- gameOn()
- finally:
- #Turn off leds at exit/error.
- GPIO.output(redpin, GPIO.LOW)
- GPIO.output(greenpin, GPIO.LOW)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement