-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
295 lines (226 loc) · 11 KB
/
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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""
--------------------------------------------------------------------------
Morse Code Decode Game
--------------------------------------------------------------------------
License:
Copyright 2024 Mina Schepmann
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------
Use the following hardware components to make a game involving morse code
deciphering:
- HT16K33 Display
- Red LED
- Green LED
- Potentiometer (analog input)
- Joystick
- SPI Screen
Requirements:
- Hardware:
- When the device powers on, SPI screen will display an introduction screen
with instructions and then difficulty level settings. Once the game is
started, the buzzer will sound morse code and the 7-hex display will be a
countdown timer. If the user deciphers the code correctly, a green LED will
turn on and the buzzer and timer will stop; if the user is incorrect, a red
LED will turn on, the buzzer will play a tone, and then the game will
continue. At the end of each round, the user will have an option, using a
joystick, to power off or keep playing.
- User interaction:
- The first point of user interaction will be selection of the difficulty
level. There will be two difficulty aspects: The first will account the
time intervals it takes for the morse code to be sent to the buzzer; the
second will vary the time the player has to solve the puzzle. The user
will depend heavily on the joystick to change position of the cursor on
the SPI screen and choose numbers for input. They will use the button
function in the joysitck to act as an "enter" button.
Uses:
- HT16K33 display library developed in class
-
"""
import time
import ht16k33 as HT16K33
import led as LED
import buzzer as BUZZER
import potentiometer as POT
import word_to_morse as MORSE
import spi_screen as SPI
import threading
import random as rand
import Adafruit_BBIO.ADC as ADC
import button as BUTTON
# ------------------------------------------------------------------------
# Constants
# ------------------------------------------------------------------------
easy_words = ["CAFE", "FACE", "HAIR", "JADE", "NAAN", "UBER", "ZAPS", "IBEX",
"GAWK", "EDGE"]
medium_words = ["HEART", "FIFTY", "EIGHT", "MOUNT", "ROUTE", "PRIZE", "UNITY",
"WHICH", "YOUTH", "VITAL"]
hard_words = ["FABLED", "CASUAL", "EIGHTH", "EMERGE", "ABACUS", "IAMBIC",
"VACATE", "WOBBLE", "EAGLET", "DABBED"]
code = {'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
'9': '----.', '0': '-----', }
# list of alphanumeric for check
alpha_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ']
highxval = 700
lowxval = 315
highyval = 700
lowyval = 315
frequency = #SET THIS HAHAHHA
# ------------------------------------------------------------------------
# Global variables
# ------------------------------------------------------------------------
# None
# ------------------------------------------------------------------------
# Functions / Classes
# ------------------------------------------------------------------------
class GameCode(threading.Thread):
red_led = None
green_led = None
spi = None
buzzer = None
display = None
def __init__(self, red_led = "P2_4", green_led = "P2_6"
buzzer = "P2_1", spi = "", xjoy = " ", yjoy = " ",
butjoy = " ", i2c_bus=1, i2c_address=0x70):
self._setup()
self.red_led = LED.LED(red_led)
self.green_led = LED.LED(green_led)
self.buzzer = BUZZER.PWM.stop
self.spi = SPI.blank()
self.display = HT16K33.HT16K33(i2c_bus, i2c_address)
self.xjoy = xjoy()
self.yjoy = yjoy()
self.butjoy = BUTTON.Button(butjoy)
# self.unpressed_value = HIGH
# self.pressed_value = LOW
def _setup(self):
""" Initialize the hardware components."""
ADC.setup()
spi.blank()
time.sleep(5)
#Turn on the SPI
def initial_spi(self):
""" Starts the interface between the player and the game, describing
what the objective is for the player and prompting a difficulty
choice"""
self.spi.text("welcome to morse code decode", fontsize=24,
fontcolor=(255,255,255), backgroundcolor=(0,0,0), justify=CENTER,
align=TOP, rotation=90)
time.sleep(3)
self.spi.blank()
time.sleep(1)
self.spi.text("this device is a bomb", fontsize=24,
fontcolor=(255,255,255), backgroundcolor=(0,0,0), justify=CENTER,
align=TOP, rotation=90)
self.spi.text("your job is to defuse it", fontsize=24,
fontcolor=(255,255,255), backgroundcolor=(0,0,0), justify=CENTER,
align=CENTER, rotation=90)
self.spi.text("choose your difficulty level", fontsize=24,
fontcolor=(255,255,255), backgroundcolor=(0,0,0), justify=CENTER,
align=BOTTOM, rotation=90)
time.sleep(5)
self.spi.blank()
# IMPLEMENT LEVEL SELECTION AS VARIABLE NAME "LEVEL"
def spi_level_select(self):
""" This function displays the options for the level selection screen"""
###
level = [easy,medium,hard]
easy = self.spi.text("easy", fontsize=24, fontcolor=(255,255,255),
backgroundcolor=(0,0,0), justify=CENTER, align=TOP, rotation=90)
medium = self.spi.text("medium", fontsize=24, fontcolor=(255,255,255),
backgroundcolor=(0,0,0), justify=CENTER, align=CENTER, rotation=90)
hard = self.spi.text("hard", fontsize=24, fontcolor=(255,255,255),
backgroundcolor=(0,0,0), justify=CENTER, align=BOTTOM, rotation=90)
def level_select_word_choice(self):
""" This function takes an input from the user about the desired
difficulty and then randomly picks a word from the potential word
arrays"""
if level == easy:
word = rand.choice(easy_words)
elif level == medium:
word = rand.choice(medium_words)
elif level == hard:
word = rand.choice(hard_words)
def morseword(self):
""" This function converts the chosen word from the random generator
from the array of given words from the list"""
alpha_to_morse(word)
def buzzer_play_morse(self):
""" This function will play the morse code on the buzzer as dot and
dash sounds. It will also check continuously if the correct answer has
been submitted; if it has, the buzzer will stop making sounds, if the
incorrect answer is given, then it will play a custom sound and then
resume the dot and dash sounds."""
for i in morse:
if i == '-':
self.buzzer.play(self, frequency, length=0.5, stop=False)
if i == '.':
self.buzzer.play(self, frequency, length=1.5, stop=False)
if i == ' ':
self.buzzer.(self, frequency, length=2.5, stop=False)
def buzzer_play_wrong(self):
""" This function sets the buzzer to play a drone sound"""
frequency = ##
self.buzzer.play(self, frequency, length=3, stop=False)
self.buzzer.stop()
def LEDs(self):
""" Turn on the correct LED given when an answer is submitted, then
turn the LED off after a time interval of 1.5 seconds
"""
if self.correct_answer:
# Checks if the correct answer has been given, and turns on the
# green LED if it is
self.green_led.on() # Is this a time that the LED will be on
time.sleep(1.5)
self.green_led.off()
if self.incorrect_answer:
# Checks if the incorrect answer has been given, and turns on the
# red LED if it is
self.red_led.on()
time.sleep(1.5)
self.red_led.off()
# End def
def stickjoy(self):
""" Set the up/down, left/right, and push button thresholds on the
joystick"""
xval=int(ADC.read(self.xjoy)) # Defines the x-resistance as an integer
yval=int(ADC.read(self.yjoy)) # Defines the y-resistance as an integer
# if xval > xvalhigh:
# spi_screen_right()
def segment_display(self):
"""This is the function for the seven-segment display which will have a
countdown function"""
def run(self):
""" Run the actual program and have the game genuinely work and run
properly"""
self.setup()
self.initial_spi()
time.sleep(1)
while(1):
if self.stickjoy.xval > xvalhigh:
level=level(i+1)