-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_arcade_button_led_buzzer_game.py
74 lines (62 loc) · 1.7 KB
/
pi_arcade_button_led_buzzer_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import RPi.GPIO as GPIO
import random
import signal
import sys
import time
# Definitions
BUTTON_PIN = 19
LED_PIN = 26
BUZZER_PIN = 13
counter = 0
trigger_number = 0
# Setup pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUZZER_PIN, GPIO.OUT)
# Get a random number that the game ends at.
def get_random_number():
r = random.randint(1, 5)
print("Random: " + str(r))
return r
# Turn off the LED at shutdown and clean up.
def shutdown(signal, frame):
GPIO.output(LED_PIN, False)
GPIO.output(BUZZER_PIN, False)
GPIO.cleanup()
sys.exit(0)
# Handles button presses.
def button_press(channel):
global counter
global trigger_number
if (GPIO.input(channel) == False):
counter = counter + 1
print("Counter: " + str(counter))
if counter == trigger_number:
# Game over!
for n in range(0, 5):
time.sleep(0.1)
GPIO.output(LED_PIN, True)
GPIO.output(BUZZER_PIN, True)
time.sleep(0.1)
GPIO.output(LED_PIN, False)
GPIO.output(BUZZER_PIN, False)
# Get a new random number for next game, reset counter.
trigger_number = get_random_number()
counter = 0
else:
# Game continues, flash the LED.
for n in range(0, counter):
time.sleep(0.3)
GPIO.output(LED_PIN, True)
time.sleep(0.3)
GPIO.output(LED_PIN, False)
# Set the first random end point.
trigger_number = get_random_number()
# Add button press handler.
GPIO.add_event_detect(BUTTON_PIN, edge = GPIO.BOTH, callback = button_press, bouncetime = 300)
# Main entry point.
signal.signal(signal.SIGINT, shutdown)
while True:
time.sleep(1)