-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10_game2.py
121 lines (87 loc) · 3.42 KB
/
day10_game2.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Imports
from machine import Pin, PWM
import time, sys
# Set up LED pins
red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)
# Set up the Break Beam pin
beam = Pin(26, Pin.IN, Pin.PULL_DOWN)
# Set up the Buzzer pin as PWM
buzzer = PWM(Pin(13))
# Set buzzer PWM frequency to 1000
buzzer.freq(1000)
# Start with the buzzer volume off (duty 0)
buzzer.duty_u16(0)
# Create game variables
starttime = 0
timecheck = 0
scorecounter = 0
state = 0
targetscore = 100
print("Game starts after the beep!")
#Long beep to signal game start
buzzer.duty_u16(10000)
time.sleep(2)
buzzer.duty_u16(0)
# D:14, P:12, J:19, 2P:8
print("GO!")
print("-------------------------------")
# Store our start time (seconds)
starttime = time.time()
while True: # Run this block until code stopped
time.sleep(0.0001) # Very short delay
# Take the current time and minus the original start time
# This gives us the number of seconds since we started the game
timecheck = time.time() - starttime
if timecheck >= 30: # If 30 or more seconds have passed
# LEDs off
red.value(0)
amber.value(0)
green.value(0)
# Beep to signal game end
buzzer.duty_u16(10000)
time.sleep(0.2)
buzzer.duty_u16(0)
# Print the target and player's score
print("-------------------------------")
print("GAME OVER! YOU LOSE :(")
print("The target was",targetscore,", you scored",scorecounter)
print("-------------------------------")
# Exit the program
sys.exit()
elif scorecounter >= targetscore: # If player's score has hit the target
# LEDs off
red.value(0)
amber.value(0)
green.value(0)
# Beep to signal game end
buzzer.duty_u16(10000)
time.sleep(0.2)
buzzer.duty_u16(0)
# Print time taken to win
print("-------------------------------")
print("YOU WIN!")
print("You took",timecheck,"seconds!")
print("-------------------------------")
# Exit the program
sys.exit()
elif state == 0 and beam.value() == 0: # If state is 0 AND our pin is LOW
scorecounter = scorecounter + 1 # Add +1 to our score counter
state = 1 # Change state to 1
print("SCORE =",scorecounter,"/",targetscore) # Print the score and target
print("Time remaining:", (30 - timecheck)) # take our timecheck variable away from 30 - gives the remaining time
if scorecounter < (targetscore / 100 * 33): # If our score is less than 33% of the target
red.value(1) # Red LED on
amber.value(0)
green.value(0)
elif (targetscore/ 100 * 33) < scorecounter < (targetscore / 100 * 66): # If our score is between 33% and 66% of the target
red.value(1) # Red LED on
amber.value(1) # Amber LED on
green.value(0)
elif scorecounter > (targetscore / 100 * 66): # If our score is over 66% of the target
red.value(1) # Red LED on
amber.value(1) # Amber LED on
green.value(1) # Green LED on
elif state == 1 and beam.value() == 1: # If state is 1 AND our pin is HIGH
state = 0 # Change the state to 0