-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickdraw.py
347 lines (280 loc) · 8.62 KB
/
quickdraw.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import pygame, sys
from pygame.locals import *
import random
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
###################################
# CLASS DEFINITIONS #
###################################
class Drawable:
def __init__(self, name, image, pos_x, pos_y, draw):
self.name = name
self.image = pygame.image.load(image)
self.pos_x = pos_x
self.pos_y = pos_y
self.drawable = draw
def draw(self, screen):
if (self.drawable):
screen.blit(self.image, (self.pos_x, self.pos_y))
#pygame.display.update()
class TextField:
def __init__(self, name, text, pos_x, pos_y, draw):
self.name = name
self.text = text
self.pos_x = pos_x
self.pos_y = pos_y
self.drawable = draw
def draw(self, screen):
if (self.drawable):
texta = basicfont.render(self.text,True, (0, 0, 0))
textrecta = texta.get_rect()
textrecta.centerx = screen.get_rect().centerx + self.pos_x
textrecta.centery = screen.get_rect().centery + self.pos_y
screen.blit(texta,textrecta)
#pygame.display.update()
SPI_PORT = 0
SPI_DEVICE = 0
backgrounds = []
foregrounds = [None, Drawable("background_wait", "clint.png", 0,150, True ), None, None, None, None, None]
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
pygame.init()
pygame.display.set_caption('Quickdraw by HackManhattan')
size = [1600, 900]
#size = [1280, 768]
#screen = pygame.display.set_mode(size)
screen = pygame.display.set_mode(size, FULLSCREEN)
signal_reads = 16
clock = pygame.time.Clock()
screen.fill((255, 255, 255))
pygame.mouse.set_visible(False)
basicfont = pygame.font.SysFont(None, 72)
color_font = (0, 0, 0)
color_bg = (255,255,255)
piezo_min = [0,0,47,20,0,0,0,0,0,0]
# music definition
pygame.mixer.music.load("../audio/bg.mp3")
# effects definition
effect = pygame.mixer.Sound('../audio/Zen_Buddhist_Temple_Bell-SoundBible.com-331362457.wav')
round_start_effect = pygame.mixer.Sound('../audio/goodbadugly-whistle-long.wav')
draw_sound = pygame.mixer.Sound('../audio/draw.wav')
timeout_sound = pygame.mixer.Sound('../audio/youdone.wav')
reset_idx = 5
round_min = 4
round_max = 11
clint_start = 250
round_start = False
round_start_time = 0
round_timeout = 30
music_fadeout_time = 1500
#screen images
titlescreen = pygame.image.load('quickdraw_title_b.jpg')
wait_screen = pygame.image.load('quickdraw_wait.jpg')
draw_screen = pygame.image.load('quickdraw_draw.jpg')
#Setup the backgrounds
# 0 game initialized
backgrounds.append(Drawable("background_title", "quickdraw_title_b.jpg", 0,0, True ))
# 1 round started
backgrounds.append(Drawable("background_wait", "screen_bg.jpg", 0,0, True ))
# 2 ready for fire
backgrounds.append(Drawable("background_draw", "quickdraw_draw.jpg", 0,0, True ))
# 3 winner
backgrounds.append(Drawable("background_p2win", "quickdraw_p2win.jpg", 0,0, True ))
# 4 penalty
backgrounds.append(Drawable("background_penalty", "quickdraw_penalty.jpg", 0,0, True ))
# 5 timeout
backgrounds.append(Drawable("background_timeout", "quickdraw_timeout.jpg", 0,0, True ))
# 6 player1 winner
backgrounds.append(Drawable("background_p1win", "quickdraw_p1win.jpg", 0,0, True ))
musicpos = 0
def pause_music():
musicpos = pygame.mixer.music.get_pos()
pygame.mixer.music.fadeout(music_fadeout_time)
def unpause_music():
pygame.mixer.music.play(musicpos)
def readadc():
values = [0]*8
for j in range(0,signal_reads):
for i in range(8):
values[i] += mcp.read_adc(i)
for i in range(0,8):
values[i] = values[i] / (signal_reads *1.0)
return values
def get_winner(values):
# TODO: should read for targets
for i in range(2,4):
if values[i] > piezo_min[i]:
return i-2
return -1
def space_pressed(values):
#print("yo",values)
if values[reset_idx] >= 1022:
print("space_pressed")
time.sleep(0.25)
return True
return False
def gamestart():
return True
def render_fire(time_elapsed):
return True
def render_penalty(tgtplayer,count,game_state,texts):
texts[game_state].text = "Player " + str(tgtplayer+1)
def quit_game():
pygame.quit()
sys.exit()
def render_round_start():
return True
def player_win(tgtplayer,tgttime,game_state,texts):
result = "Time elapsed " + str(tgttime) + " seconds"
texts[game_state].text = result
def gun_picked_up(values):
for i in range(2):
if values[i] < 999:
return i
return -1
# random start time
clock.tick(120)
def changeState(state):
if (game_state != state):
backgrounds[game_state].drawable = False
game_state = state
backgrounds[game_state].drawable = True
def main():
round_time = 0
round_start_time = 0
menu_timeout = 0
game_state = 0 #flag for game state
# 0 game initialized
# 1 round started
# 2 ready for fire
# 3 player 2 winner
# 4 penalty
# 5 timeout
# 6 player 1 winner
game_win = {}
penalty = {}
# start music
music_is_on = False
texts = [None, None, None, TextField("p2win", "test", 200, 400, True), TextField("penalty", "test", 350, -60, True), None, TextField("p1win", "test", 200, 400, True)]
while True:
#Render all the drawables on a new screen
screen.fill((255,255,255))
#for drawable in backgrounds:
# drawable.draw(screen)
backgrounds[game_state].draw(screen)
if foregrounds[game_state]:
foregrounds[game_state].draw(screen)
if texts[game_state]:
texts[game_state].draw(screen)
pygame.display.update()
cur_input = readadc()
#print(cur_input)
if space_pressed(cur_input):
if game_state is 0:
#pygame.mixer.music.fadeout(music_fadeout_time)
pygame.mixer.music.pause()
#pause_music()
round_start_effect.play()
game_state = 1
#reset clint for his slow travel
foregrounds[1].pos_x = clint_start
round_time = random.randint(round_min, round_max)
round_start_time = time.time()
elif game_state in [3,4,5,6]:
game_state = 0
penalty = {}
winner = {}
music_is_on = True
pygame.mixer.music.unpause()
#unpause_music()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
quit_game()
elif event.key == K_SPACE and game_state is 0:
pygame.mixer.music.pause()
#pygame.mixer.music.fadeout(music_fadeout_time)
round_start_effect.play()
game_state = 1
#reset clint for his slow travel
foregrounds[1].pos_x = clint_start
round_time = random.randint(round_min,round_max)
round_start_time = time.time()
elif event.key == K_SPACE and game_state in [3,4,5,6]:
# winner to new game
game_state = 0
# reset penalty and winner
penalty = {}
winner = {}
music_is_on = True
pygame.mixer.music.unpause()
if game_state is 0:
gamestart()
if not music_is_on:
music_is_on = True
pygame.mixer.music.play()
elif game_state is 1:
render_round_start()
cur_elapsed = time.time() - round_start_time
#move clint across the screen slowly
foregrounds[1].pos_x += 1
if gun_picked_up(cur_input) != -1:
penalty["player"] = gun_picked_up(cur_input)
if "count" in penalty:
penalty["count"] += 1
game_state = 4
menu_timeout = time.time()
else:
penalty["count"] = 1
print(cur_elapsed,"time passed")
if cur_elapsed > round_time:
round_start_effect.stop()
#effect.play()
game_state = 2
draw_sound.play()
round_start_time = time.time()
music_is_on = False
elif game_state is 2:
cur_elapsed = time.time() - round_start_time
if cur_elapsed > round_timeout:
menu_timeout = time.time()
game_state = 5
timeout_sound.play()
render_fire(time.time() - round_start_time)
if get_winner(cur_input) != -1:
if get_winner(cur_input) == 0:
game_state = 6
else:
game_state = 3
menu_timeout = time.time()
game_win["player"] = get_winner(cur_input)
game_win["time"] = time.time() - round_start_time
elif game_state in [3,6]:
player_win(game_win["player"],game_win["time"],game_state,texts)
#start the timer to go back to the start screen
cur_elapsed = time.time() - menu_timeout
if cur_elapsed > round_timeout:
#go back to the start screen
game_state = 0
#unpause_music()
pygame.mixer.music.pause()
elif game_state is 4:
render_penalty(penalty["player"],penalty["count"],game_state,texts)
#start the timer to go back to the start screen
cur_elapsed = time.time() - menu_timeout
if cur_elapsed > round_timeout:
#go back to the start screen
game_state = 0
#unpause_music()
pygame.mixer.music.pause()
elif game_state is 5:
#start the timer to go back to the start screen
cur_elapsed = time.time() - menu_timeout
if cur_elapsed > round_timeout:
#go back to the start screen
game_state = 0
#unpause_music()
pygame.mixer.music.pause()
main()