|
@@ -0,0 +1,767 @@
|
|
1
|
+import random
|
|
2
|
+import time
|
|
3
|
+import pygame
|
|
4
|
+import sys
|
|
5
|
+import pyaudio
|
|
6
|
+import wave
|
|
7
|
+import glob
|
|
8
|
+import os
|
|
9
|
+import ast
|
|
10
|
+from configobj import ConfigObj
|
|
11
|
+from itertools import cycle
|
|
12
|
+
|
|
13
|
+class Game:
|
|
14
|
+ def __init__(self, o):
|
|
15
|
+ self.o = o
|
|
16
|
+
|
|
17
|
+ self.last_frame_time = time.perf_counter()
|
|
18
|
+ self.bullets = []
|
|
19
|
+ self.bullet_speed = 80
|
|
20
|
+ self.delta = 0.00001
|
|
21
|
+ self.sidebar_width = 80
|
|
22
|
+ self.gw = o.width - self.sidebar_width
|
|
23
|
+ self.score = 0
|
|
24
|
+ self.time = 0
|
|
25
|
+ self.player = Player(o, self)
|
|
26
|
+ self.enemies = []
|
|
27
|
+ self.birth = pygame.time.get_ticks()
|
|
28
|
+ self.last_enemy_spawn = pygame.time.get_ticks()
|
|
29
|
+ self.enemy_spawn_delay = 2000
|
|
30
|
+ self.explodes = []
|
|
31
|
+ self.explode_speed = 80
|
|
32
|
+ self.hs_name = ""
|
|
33
|
+ self.speed_mult = 1.0
|
|
34
|
+ self.speed_mult_inc = .1
|
|
35
|
+ self.mult_state = 3
|
|
36
|
+ # self.hs_n1 = "billy"
|
|
37
|
+ # self.hs_n2 = "joe"
|
|
38
|
+ # self.hs_n3 = "robzilla"
|
|
39
|
+ # self.hs_s1 = 10
|
|
40
|
+ # self.hs_s2 = 40
|
|
41
|
+ # self.hs_s3 = 100
|
|
42
|
+ self.load_highscore()
|
|
43
|
+ #self.save_highscore()
|
|
44
|
+ self.game_over = False
|
|
45
|
+
|
|
46
|
+ print('aaay making a game')
|
|
47
|
+
|
|
48
|
+ def update(self):
|
|
49
|
+ self.delta = time.perf_counter() - self.last_frame_time
|
|
50
|
+ self.last_frame_time = time.perf_counter()
|
|
51
|
+ #print(self.delta)
|
|
52
|
+ self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
|
|
53
|
+ #print('playing game ', self.player.posy)
|
|
54
|
+ self.mstime = (pygame.time.get_ticks() - self.birth)
|
|
55
|
+ self.time = int(self.mstime * .001)
|
|
56
|
+ if self.time > self.mult_state:
|
|
57
|
+ self.mult_state += 3
|
|
58
|
+ if self.speed_mult < 2.5:
|
|
59
|
+ self.speed_mult += self.speed_mult_inc
|
|
60
|
+ if self.enemy_spawn_delay > 1000:
|
|
61
|
+ self.enemy_spawn_delay -= 50
|
|
62
|
+ print('....level up ', self.speed_mult)
|
|
63
|
+ #print(self.mstime % 100.0)
|
|
64
|
+ #if self.mstime % 100.0 == 0:
|
|
65
|
+ #print('up level')
|
|
66
|
+ #self.speed_mult += self.speed_mult_inc
|
|
67
|
+ self.enemy_spawner()
|
|
68
|
+ self.draw_bullets()
|
|
69
|
+ self.draw_enemies()
|
|
70
|
+ self.draw_explodes()
|
|
71
|
+ self.player.update(self.delta)
|
|
72
|
+ self.draw_sidebar()
|
|
73
|
+ self.o.update_display(self.o)
|
|
74
|
+
|
|
75
|
+ def draw_bullets(self):
|
|
76
|
+ for x in self.bullets:
|
|
77
|
+ x.check_collision()
|
|
78
|
+ x.update()
|
|
79
|
+ x.draw()
|
|
80
|
+
|
|
81
|
+ def draw_enemies(self):
|
|
82
|
+ for x in self.enemies:
|
|
83
|
+ x.check_collision()
|
|
84
|
+ x.update()
|
|
85
|
+ x.draw()
|
|
86
|
+
|
|
87
|
+ def draw_explodes(self):
|
|
88
|
+ for x in self.explodes:
|
|
89
|
+ #x.check_collision()
|
|
90
|
+ x.update()
|
|
91
|
+ x.draw()
|
|
92
|
+
|
|
93
|
+ def draw_sidebar(self):
|
|
94
|
+ w = self.o.width - self.sidebar_width
|
|
95
|
+ self.o.draw.rectangle((w, 0, self.o.width, self.o.height), outline=0, fill=self.o.light_grey)
|
|
96
|
+ inc = 40
|
|
97
|
+ h = 10
|
|
98
|
+ self.o.center_block("Score", self.o.h2, [w, h, self.o.width, h + 10], self.o.blue)
|
|
99
|
+ h += inc
|
|
100
|
+ self.o.center_block(str(self.score), self.o.h2, [w, 15, self.o.width, h + 10], self.o.blue)
|
|
101
|
+ h += (inc * 2)
|
|
102
|
+ self.o.center_block("Time", self.o.h2, [w, 30, self.o.width, h + 10], self.o.blue)
|
|
103
|
+ h += inc
|
|
104
|
+ self.o.center_block(str(self.time), self.o.h2, [w, 40, self.o.width, h + 10], self.o.blue)
|
|
105
|
+
|
|
106
|
+ def enemy_spawner(self):
|
|
107
|
+ sl = pygame.time.get_ticks() - self.last_enemy_spawn
|
|
108
|
+ if len(self.enemies) < 1 or sl > self.enemy_spawn_delay:
|
|
109
|
+ new_e = Enemy(self.o, self)
|
|
110
|
+ self.enemies.append(new_e)
|
|
111
|
+ self.last_enemy_spawn = pygame.time.get_ticks()
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+ def save_highscore(self):
|
|
115
|
+ base_dir = "/home/pi/zpc_ct/"
|
|
116
|
+ title = "defender_scores"
|
|
117
|
+ fname = base_dir + title + '.sco'
|
|
118
|
+ if os.path.exists(fname):
|
|
119
|
+ os.utime(fname, None)
|
|
120
|
+ print(fname, ' exists')
|
|
121
|
+ else:
|
|
122
|
+ print('make a damn file ', fname)
|
|
123
|
+ open(fname, 'a').close()
|
|
124
|
+
|
|
125
|
+ self.sconf = ConfigObj(fname)
|
|
126
|
+ self.sconf['name1'] = self.hs_n1
|
|
127
|
+ self.sconf['name2'] = self.hs_n2
|
|
128
|
+ self.sconf['name3'] = self.hs_n3
|
|
129
|
+ self.sconf['score1'] = self.hs_s1
|
|
130
|
+ self.sconf['score2'] = self.hs_s2
|
|
131
|
+ self.sconf['score3'] = self.hs_s3
|
|
132
|
+
|
|
133
|
+ self.sconf.write()
|
|
134
|
+
|
|
135
|
+ def load_highscore(self):
|
|
136
|
+ base_dir = "/home/pi/zpc_ct/"
|
|
137
|
+ self.sconf = ConfigObj(base_dir + 'defender_scores.sco')
|
|
138
|
+ self.hs_n1 = self.sconf['name1']
|
|
139
|
+ self.hs_n2 = self.sconf['name2']
|
|
140
|
+ self.hs_n3 = self.sconf['name3']
|
|
141
|
+ self.hs_s1 = self.sconf.as_int('score1')
|
|
142
|
+ self.hs_s2 = self.sconf.as_int('score2')
|
|
143
|
+ self.hs_s3 = self.sconf.as_int('score3')
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+class Player:
|
|
147
|
+ def __init__(self, o, game):
|
|
148
|
+ self.o = o
|
|
149
|
+ self.game = game
|
|
150
|
+ self.width = 28
|
|
151
|
+ self.height = 12
|
|
152
|
+ self.speed = 80
|
|
153
|
+ self.posx = game.gw / 2
|
|
154
|
+ self.posy = o.height - self.height
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+ def update(self, delta):
|
|
158
|
+ self.check_keys(delta)
|
|
159
|
+ self.draw_player()
|
|
160
|
+ #check position
|
|
161
|
+ #pass
|
|
162
|
+ def draw_player(self):
|
|
163
|
+ hw = self.width / 2
|
|
164
|
+ hh = self.height / 2
|
|
165
|
+ self.o.draw.rectangle(((self.posx - hw), (self.posy - hh), (self.posx + hw), (self.posy + hh)), outline=self.o.light_grey, fill=self.o.light_grey)
|
|
166
|
+ self.o.draw.rectangle(((self.posx - (hw/4)), (self.posy - self.height), (self.posx + (hw/4)), (self.posy + hh)), outline=self.o.light_grey, fill=self.o.light_grey)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+ def check_keys(self, delta):
|
|
170
|
+ if self.o.keyState[0] == 2:
|
|
171
|
+ if self.posx > 0:
|
|
172
|
+ self.posx -= self.speed * delta
|
|
173
|
+ elif self.o.keyState[3] == 2:
|
|
174
|
+ if self.posx < self.o.width:
|
|
175
|
+ self.posx += self.speed * delta
|
|
176
|
+
|
|
177
|
+ elif self.o.keyState[1] == 1 or self.o.keyState[2] == 1:
|
|
178
|
+ self.game.bullets.append(Bullet(self.o, self.game, self.posx))
|
|
179
|
+
|
|
180
|
+class Bullet:
|
|
181
|
+ def __init__(self, o, game, x):
|
|
182
|
+ self.o = o
|
|
183
|
+ self.game = game
|
|
184
|
+ self.posx = x
|
|
185
|
+ self.posy = 110
|
|
186
|
+ self.width = 3
|
|
187
|
+ self.height = 10
|
|
188
|
+
|
|
189
|
+ def draw(self):
|
|
190
|
+ hw = self.width / 2
|
|
191
|
+ hh = self.height / 2
|
|
192
|
+ self.o.draw.rectangle(((self.posx - hw), (self.posy - hh), (self.posx + hw), (self.posy + hh)), outline=0, fill=self.o.light_grey)
|
|
193
|
+
|
|
194
|
+ def update(self):
|
|
195
|
+ self.posy -= self.game.bullet_speed * self.game.delta
|
|
196
|
+ if self.posy < 0:
|
|
197
|
+ self.game.bullets.remove(self)
|
|
198
|
+ #print('killing bullet ', len(self.game.bullets))
|
|
199
|
+ self.draw()
|
|
200
|
+
|
|
201
|
+ def check_collision(self):
|
|
202
|
+ pass
|
|
203
|
+
|
|
204
|
+class Enemy:
|
|
205
|
+ def __init__(self, o, game):
|
|
206
|
+ self.o = o
|
|
207
|
+ self.game = game
|
|
208
|
+ self.posx = game.gw / 2
|
|
209
|
+ self.posy = -20
|
|
210
|
+ self.width = 20
|
|
211
|
+ self.height = 15
|
|
212
|
+ self.speed = random.randrange(15, 30) * game.speed_mult
|
|
213
|
+ self.xspeed = random.randrange(0, 30) * game.speed_mult
|
|
214
|
+ self.point_value = 50
|
|
215
|
+
|
|
216
|
+ self.ydir = random.choice([-1, 1])
|
|
217
|
+
|
|
218
|
+ def draw(self):
|
|
219
|
+ hw = self.width / 2
|
|
220
|
+ hh = self.height / 2
|
|
221
|
+ self.o.draw.rectangle(((self.posx - hw), (self.posy - hh), (self.posx + hw), (self.posy + hh)), outline=self.o.light_grey, fill=self.o.light_grey)
|
|
222
|
+ self.o.draw.rectangle(((self.posx - hw + 5), (self.posy - hh + 10), (self.posx + hw - 5), (self.posy + hh)), outline=self.o.blue, fill=self.o.blue)
|
|
223
|
+ #eyes
|
|
224
|
+ # self.o.draw.rectangle(((self.posx - 8), (self.posy - 5), (self.posx - 5), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
|
|
225
|
+ # self.o.draw.rectangle(((self.posx + 8), (self.posy - 5), (self.posx + 5), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
|
|
226
|
+
|
|
227
|
+ self.o.draw.rectangle(((self.posx - 8), (self.posy - 4), (self.posx - 2), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
|
|
228
|
+ self.o.draw.rectangle(((self.posx + 8), (self.posy - 4), (self.posx + 2), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
|
|
229
|
+
|
|
230
|
+ def update(self):
|
|
231
|
+ self.posy += self.speed * self.game.delta
|
|
232
|
+ self.posx += (self.xspeed * self.ydir * self.game.delta)
|
|
233
|
+ self.check_collision()
|
|
234
|
+
|
|
235
|
+ if self.posy > self.o.height:
|
|
236
|
+ self.game.enemies.remove(self)
|
|
237
|
+ #print('killing enemy ', len(self.game.enemies))
|
|
238
|
+ self.game.game_over = True
|
|
239
|
+
|
|
240
|
+ if self.posx > (self.game.gw - (self.width / 2)):
|
|
241
|
+ self.ydir = -1
|
|
242
|
+ elif self.posx < (0 + (self.width / 2)):
|
|
243
|
+ self.ydir = 1
|
|
244
|
+
|
|
245
|
+ self.draw()
|
|
246
|
+
|
|
247
|
+ def check_collision(self):
|
|
248
|
+
|
|
249
|
+ for b in self.game.bullets:
|
|
250
|
+ xdist = abs(b.posx - self.posx)
|
|
251
|
+
|
|
252
|
+ if xdist < self.width / 2:
|
|
253
|
+ ydist = abs(b.posy - self.posy)
|
|
254
|
+ if ydist < self.height / 2:
|
|
255
|
+ #print('hit ', xdist)
|
|
256
|
+ self.game.score += self.point_value
|
|
257
|
+ if b in self.game.bullets:
|
|
258
|
+ self.game.bullets.remove(b)
|
|
259
|
+ self.die()
|
|
260
|
+ #else:
|
|
261
|
+ #print('no y hit ', ydist)
|
|
262
|
+ #else:
|
|
263
|
+ #print('no x hit ', xdist)
|
|
264
|
+
|
|
265
|
+ def die(self):
|
|
266
|
+ if self in self.game.enemies:
|
|
267
|
+ self.game.enemies.remove(self)
|
|
268
|
+ #print('enemy shot ', len(self.game.enemies))
|
|
269
|
+ self.game.explodes.append(Explode(self.o, self.game, self.posx, self.posy))
|
|
270
|
+
|
|
271
|
+class Explode:
|
|
272
|
+ def __init__(self, o, game, x, y):
|
|
273
|
+ self.o = o
|
|
274
|
+ self.game = game
|
|
275
|
+ self.posx = x
|
|
276
|
+ self.posy = y
|
|
277
|
+ self.width = 16
|
|
278
|
+ self.height = 14
|
|
279
|
+ self.gap = 5
|
|
280
|
+ self.scale_speed = 80
|
|
281
|
+
|
|
282
|
+ def draw(self):
|
|
283
|
+ hw = self.width / 2
|
|
284
|
+ hh = self.height / 2
|
|
285
|
+ c1 = [self.posx + self.gap, self.posy - self.gap]
|
|
286
|
+ c2 = [self.posx - self.gap, self.posy - self.gap]
|
|
287
|
+ c3 = [self.posx + self.gap, self.posy + self.gap]
|
|
288
|
+ c4 = [self.posx - self.gap, self.posy + self.gap]
|
|
289
|
+
|
|
290
|
+ self.o.draw.rectangle(((c1[0] - hw), (c1[1] - hh), (c1[0] + hw), (c1[1] + hh)), outline=0, fill=self.o.light_grey)
|
|
291
|
+ self.o.draw.rectangle(((c2[0] - hw), (c2[1] - hh), (c2[0] + hw), (c2[1] + hh)), outline=0, fill=self.o.light_grey)
|
|
292
|
+ self.o.draw.rectangle(((c3[0] - hw), (c3[1] - hh), (c3[0] + hw), (c3[1] + hh)), outline=0, fill=self.o.light_grey)
|
|
293
|
+ self.o.draw.rectangle(((c4[0] - hw), (c4[1] - hh), (c4[0] + hw), (c4[1] + hh)), outline=0, fill=self.o.light_grey)
|
|
294
|
+
|
|
295
|
+ def update(self):
|
|
296
|
+ #self.posy -= self.game.bullet_speed * self.game.delta
|
|
297
|
+ self.gap += self.game.explode_speed * self.game.delta
|
|
298
|
+ self.width -= self.scale_speed * self.game.delta
|
|
299
|
+ self.height -= self.scale_speed * self.game.delta
|
|
300
|
+ if self.gap > 20:
|
|
301
|
+ self.game.explodes.remove(self)
|
|
302
|
+ print('killing explosion ', len(self.game.explodes))
|
|
303
|
+ self.draw()
|
|
304
|
+
|
|
305
|
+ def check_collision(self):
|
|
306
|
+ pass
|
|
307
|
+
|
|
308
|
+#====================================
|
|
309
|
+
|
|
310
|
+State = type("State", (object,), {})
|
|
311
|
+#====================================
|
|
312
|
+class State(object):
|
|
313
|
+ def __init__(self, FSM):
|
|
314
|
+ self.FSM = FSM
|
|
315
|
+ self.timer = 0
|
|
316
|
+ self.startTime = 0
|
|
317
|
+ def Enter(self):
|
|
318
|
+ self.timer = 0
|
|
319
|
+ self.startTime = 0
|
|
320
|
+ def Execute(self):
|
|
321
|
+ print('Executing')
|
|
322
|
+ def Exit(self):
|
|
323
|
+ print('Exiting')
|
|
324
|
+
|
|
325
|
+#====================================
|
|
326
|
+
|
|
327
|
+class Example(State):
|
|
328
|
+ def __init__(self,FSM):
|
|
329
|
+ super(Example, self).__init__(FSM)
|
|
330
|
+
|
|
331
|
+ def Enter(self):
|
|
332
|
+ self.FSM.stateLife = 1
|
|
333
|
+ super(Example, self).Enter()
|
|
334
|
+
|
|
335
|
+ def Execute(self):
|
|
336
|
+ self.FSM.stateLife += 1
|
|
337
|
+
|
|
338
|
+ def Exit(self):
|
|
339
|
+ pass
|
|
340
|
+
|
|
341
|
+#====================================
|
|
342
|
+
|
|
343
|
+class Startup(State):
|
|
344
|
+ def __init__(self,FSM):
|
|
345
|
+ super(Startup, self).__init__(FSM)
|
|
346
|
+
|
|
347
|
+ def Enter(self):
|
|
348
|
+ self.FSM.stateLife = 1
|
|
349
|
+ self.birth = time.perf_counter()
|
|
350
|
+ super(Startup, self).Enter()
|
|
351
|
+
|
|
352
|
+ def Execute(self):
|
|
353
|
+ print('defender startup state')
|
|
354
|
+ self.FSM.ToTransition('toIntro')
|
|
355
|
+
|
|
356
|
+ def Exit(self):
|
|
357
|
+ pass
|
|
358
|
+
|
|
359
|
+#====================================
|
|
360
|
+
|
|
361
|
+class Intro(State):
|
|
362
|
+ def __init__(self,FSM):
|
|
363
|
+ super(Intro, self).__init__(FSM)
|
|
364
|
+
|
|
365
|
+ def Enter(self):
|
|
366
|
+ self.birth = time.perf_counter()
|
|
367
|
+ o = self.FSM.owner
|
|
368
|
+ o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
|
|
369
|
+ o.center_block("Welcome To Defender", o.h2, [0, 0, o.width, o.height], o.light_grey)
|
|
370
|
+ o.update_display(0)
|
|
371
|
+ super(Intro, self).Enter()
|
|
372
|
+
|
|
373
|
+ def Execute(self):
|
|
374
|
+ #print('doing defender intro ', (time.perf_counter() - self.birth))
|
|
375
|
+ if (time.perf_counter() - self.birth) > 2:
|
|
376
|
+ print('time is up')
|
|
377
|
+ self.FSM.queued_state = "toMain"
|
|
378
|
+ self.FSM.ToTransition('toBlueFade')
|
|
379
|
+ #self.FSM.ToTransition('toMain')
|
|
380
|
+
|
|
381
|
+ def Exit(self):
|
|
382
|
+ pass
|
|
383
|
+
|
|
384
|
+#====================================
|
|
385
|
+
|
|
386
|
+#====================================
|
|
387
|
+
|
|
388
|
+class Main(State):
|
|
389
|
+ def __init__(self,FSM):
|
|
390
|
+ super(Main, self).__init__(FSM)
|
|
391
|
+
|
|
392
|
+ def Enter(self):
|
|
393
|
+ print('hello defender main')
|
|
394
|
+ o = self.FSM.owner
|
|
395
|
+ o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
|
|
396
|
+ o.update_display(0)
|
|
397
|
+ self.game = Game(o)
|
|
398
|
+ o.game = self.game
|
|
399
|
+
|
|
400
|
+ super(Main, self).Enter()
|
|
401
|
+
|
|
402
|
+ def Execute(self):
|
|
403
|
+ self.game.update()
|
|
404
|
+ if self.game.game_over:
|
|
405
|
+
|
|
406
|
+ self.FSM.queued_state = "toGameOver"
|
|
407
|
+ self.FSM.ToTransition('toBlueFade')
|
|
408
|
+
|
|
409
|
+ def Exit(self):
|
|
410
|
+ pass
|
|
411
|
+
|
|
412
|
+#====================================
|
|
413
|
+
|
|
414
|
+class BlueFade(State):
|
|
415
|
+ def __init__(self,FSM):
|
|
416
|
+ super(BlueFade, self).__init__(FSM)
|
|
417
|
+
|
|
418
|
+ def Enter(self):
|
|
419
|
+ self.FSM.stateLife = 1
|
|
420
|
+ self.birth = pygame.time.get_ticks()
|
|
421
|
+ self.last_frame_time = time.perf_counter()
|
|
422
|
+ self.o = self.FSM.owner
|
|
423
|
+ self.x = self.o.width / 2
|
|
424
|
+ self.y = self.o.height / 2
|
|
425
|
+ self.xsize = 0.0
|
|
426
|
+ self.ysize = 0.0
|
|
427
|
+
|
|
428
|
+ super(BlueFade, self).Enter()
|
|
429
|
+
|
|
430
|
+ def Execute(self):
|
|
431
|
+ self.FSM.stateLife += 1
|
|
432
|
+ self.delta = time.perf_counter() - self.last_frame_time
|
|
433
|
+ self.last_frame_time = time.perf_counter()
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+ self.o.draw.rectangle((self.x - self.xsize, self.y - self.ysize, self.x + self.xsize, self.y + self.ysize), outline=self.o.blue, fill=self.o.blue)
|
|
437
|
+ self.o.update_display(self.o)
|
|
438
|
+ self.xsize += self.delta * 150
|
|
439
|
+ self.ysize += self.delta * 125
|
|
440
|
+ if (pygame.time.get_ticks() - self.birth > 1000):
|
|
441
|
+
|
|
442
|
+ print('get outta fade')
|
|
443
|
+ if self.FSM.queued_state != None:
|
|
444
|
+ ns = self.FSM.queued_state
|
|
445
|
+ self.FSM.queued_state = None
|
|
446
|
+ self.FSM.ToTransition(ns)
|
|
447
|
+ def Exit(self):
|
|
448
|
+ pass
|
|
449
|
+
|
|
450
|
+#====================================
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+class GreyFade(State):
|
|
454
|
+ def __init__(self,FSM):
|
|
455
|
+ super(GreyFade, self).__init__(FSM)
|
|
456
|
+
|
|
457
|
+ def Enter(self):
|
|
458
|
+ self.FSM.stateLife = 1
|
|
459
|
+ self.birth = pygame.time.get_ticks()
|
|
460
|
+ self.last_frame_time = time.perf_counter()
|
|
461
|
+ self.o = self.FSM.owner
|
|
462
|
+ self.x = self.o.width / 2
|
|
463
|
+ self.y = self.o.height / 2
|
|
464
|
+ self.xsize = 0.0
|
|
465
|
+ self.ysize = 0.0
|
|
466
|
+
|
|
467
|
+ super(GreyFade, self).Enter()
|
|
468
|
+
|
|
469
|
+ def Execute(self):
|
|
470
|
+ self.FSM.stateLife += 1
|
|
471
|
+ self.delta = time.perf_counter() - self.last_frame_time
|
|
472
|
+ self.last_frame_time = time.perf_counter()
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+ self.o.draw.rectangle((self.x - self.xsize, self.y - self.ysize, self.x + self.xsize, self.y + self.ysize), outline=self.o.light_grey, fill=self.o.light_grey)
|
|
476
|
+ self.o.update_display(self.o)
|
|
477
|
+ self.xsize += self.delta * 150
|
|
478
|
+ self.ysize += self.delta * 125
|
|
479
|
+ if (pygame.time.get_ticks() - self.birth > 1000):
|
|
480
|
+
|
|
481
|
+ print('get outta fade')
|
|
482
|
+ if self.FSM.queued_state != None:
|
|
483
|
+ ns = self.FSM.queued_state
|
|
484
|
+ self.FSM.queued_state = "toMain"
|
|
485
|
+ self.FSM.ToTransition(ns)
|
|
486
|
+
|
|
487
|
+ def Exit(self):
|
|
488
|
+ pass
|
|
489
|
+
|
|
490
|
+#====================================
|
|
491
|
+
|
|
492
|
+class GameOver(State):
|
|
493
|
+ def __init__(self,FSM):
|
|
494
|
+ super(GameOver, self).__init__(FSM)
|
|
495
|
+
|
|
496
|
+ def Enter(self):
|
|
497
|
+ self.FSM.stateLife = 1
|
|
498
|
+ self.o = self.FSM.owner
|
|
499
|
+ self.birth = pygame.time.get_ticks()
|
|
500
|
+
|
|
501
|
+ self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
|
|
502
|
+ self.o.center_block("Game Over", self.o.h2, [0, 0, self.o.width, self.o.height], self.o.light_grey)
|
|
503
|
+ self.o.center_block("Score: " + str(self.o.game.score), self.o.h2, [0, 20, self.o.width, self.o.height + 20], self.o.light_grey)
|
|
504
|
+ self.o.update_display(0)
|
|
505
|
+
|
|
506
|
+ super(GameOver, self).Enter()
|
|
507
|
+
|
|
508
|
+ def Execute(self):
|
|
509
|
+ #self.FSM.stateLife += 1
|
|
510
|
+ if self.o.keyState[0] == 1:
|
|
511
|
+ #self.FSM.ToTransition("toMain")
|
|
512
|
+ self.FSM.queued_state = "toMain"
|
|
513
|
+ self.FSM.ToTransition('toBlueFade')
|
|
514
|
+
|
|
515
|
+ if (pygame.time.get_ticks() - self.birth) > 3000:
|
|
516
|
+ if self.o.game.score > self.o.game.hs_s1:
|
|
517
|
+ print('this is a new high score')
|
|
518
|
+ self.FSM.queued_state = "toNewHighScore"
|
|
519
|
+ else:
|
|
520
|
+ print('not a new high score')
|
|
521
|
+
|
|
522
|
+ self.FSM.queued_state = "toHighScore"
|
|
523
|
+ self.FSM.ToTransition('toBlueFade')
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+ def Exit(self):
|
|
527
|
+ pass
|
|
528
|
+
|
|
529
|
+#====================================
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+class HighScore(State):
|
|
533
|
+ def __init__(self,FSM):
|
|
534
|
+ super(HighScore, self).__init__(FSM)
|
|
535
|
+
|
|
536
|
+ def Enter(self):
|
|
537
|
+ #self.FSM.stateLife = 1
|
|
538
|
+ self.o = self.FSM.owner
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+ self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
|
|
542
|
+ self.o.center_text("HIGH SCORES", self.o.h1, self.o.width, 25, self.o.light_grey)
|
|
543
|
+ self.o.center_text("----------------------", self.o.h2, self.o.width, 55, self.o.light_grey)
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+ #self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
|
|
547
|
+ self.o.center_block(self.o.game.hs_n3 + ': ' + str(self.o.game.hs_s3), self.o.h2, [0, -20, self.o.width, self.o.height-20], self.o.light_grey)
|
|
548
|
+ self.o.center_block(self.o.game.hs_n2 + ': ' + str(self.o.game.hs_s2), self.o.h2, [0, 0, self.o.width, self.o.height], self.o.light_grey)
|
|
549
|
+ self.o.center_block(self.o.game.hs_n1 + ': ' + str(self.o.game.hs_s1), self.o.h2, [0, 20, self.o.width, self.o.height+20], self.o.light_grey)
|
|
550
|
+ self.o.center_block("restart", self.o.h2, [0, 50, self.o.width, self.o.height + 50], self.o.light_grey)
|
|
551
|
+ self.o.update_display(0)
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+ super(HighScore, self).Enter()
|
|
555
|
+
|
|
556
|
+ def Execute(self):
|
|
557
|
+ #self.FSM.stateLife += 1
|
|
558
|
+ if self.o.keyState[0] == 1:
|
|
559
|
+ #self.FSM.ToTransition("toMain")
|
|
560
|
+ self.FSM.queued_state = "toMain"
|
|
561
|
+ self.FSM.ToTransition('toBlueFade')
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+ def Exit(self):
|
|
565
|
+ pass
|
|
566
|
+
|
|
567
|
+#====================================
|
|
568
|
+
|
|
569
|
+#====================================
|
|
570
|
+
|
|
571
|
+class NewHighScore(State):
|
|
572
|
+ def __init__(self,FSM):
|
|
573
|
+ super(NewHighScore, self).__init__(FSM)
|
|
574
|
+
|
|
575
|
+ def Enter(self):
|
|
576
|
+ self.birth = time.perf_counter()
|
|
577
|
+ o = self.FSM.owner
|
|
578
|
+ o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
|
|
579
|
+ o.center_block("NEW HIGH SCORE!", o.h2, [0, 0, o.width, o.height], o.light_grey)
|
|
580
|
+ o.update_display(0)
|
|
581
|
+ super(NewHighScore, self).Enter()
|
|
582
|
+
|
|
583
|
+ def Execute(self):
|
|
584
|
+ #print('doing defender intro ', (time.perf_counter() - self.birth))
|
|
585
|
+ if (time.perf_counter() - self.birth) > 2:
|
|
586
|
+ print('time is up')
|
|
587
|
+ self.FSM.queued_state = "toEnterText"
|
|
588
|
+ self.FSM.ToTransition('toBlueFade')
|
|
589
|
+ #self.FSM.ToTransition('toMain')
|
|
590
|
+
|
|
591
|
+ def Exit(self):
|
|
592
|
+ pass
|
|
593
|
+
|
|
594
|
+#====================================
|
|
595
|
+class EnterText(State):
|
|
596
|
+ def __init__(self,FSM):
|
|
597
|
+ super(EnterText, self).__init__(FSM)
|
|
598
|
+
|
|
599
|
+ def Enter(self):
|
|
600
|
+ o = self.FSM.owner
|
|
601
|
+
|
|
602
|
+ self.input_string = ""
|
|
603
|
+
|
|
604
|
+ self.last_action = pygame.time.get_ticks()
|
|
605
|
+ self.caps = False
|
|
606
|
+ self.cur_but = 18
|
|
607
|
+ self.cur_letter = ""
|
|
608
|
+
|
|
609
|
+ self.skips = [7, 3, 11, 12, 13, 14, 15]
|
|
610
|
+
|
|
611
|
+ self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"],
|
|
612
|
+ ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"],
|
|
613
|
+ ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"],
|
|
614
|
+ [], [], [], []]
|
|
615
|
+
|
|
616
|
+ self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"],
|
|
617
|
+ ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"],
|
|
618
|
+ ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"],
|
|
619
|
+ [], [], [], []]
|
|
620
|
+
|
|
621
|
+ self.labels = self.lc_labels
|
|
622
|
+
|
|
623
|
+ #self.songCycle = cycle(self.song)
|
|
624
|
+ #self.curPattern = next(self.songCycle)
|
|
625
|
+
|
|
626
|
+ self.cycles = []
|
|
627
|
+ for l in self.labels:
|
|
628
|
+ self.cycles.append(cycle(l))
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+ self.draw(o)
|
|
633
|
+ o.update_display(0)
|
|
634
|
+ super(EnterText, self).Enter()
|
|
635
|
+
|
|
636
|
+ def Execute(self):
|
|
637
|
+ o = self.FSM.owner
|
|
638
|
+ y_size = o.height / 4
|
|
639
|
+ if o.keyState[11] == 1:
|
|
640
|
+ if self.input_string != 'blank':
|
|
641
|
+ o.game.hs_name = self.input_string
|
|
642
|
+ #o.bpm = o.sconf['bpm']
|
|
643
|
+ #o.sconf['title'] = self.input_string
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+ if o.game.score > o.game.hs_s3:
|
|
647
|
+ print('new first')
|
|
648
|
+ o.game.hs_s1 = o.game.hs_s2
|
|
649
|
+ o.game.hs_n1 = o.game.hs_n2
|
|
650
|
+ o.game.hs_s2 = o.game.hs_s3
|
|
651
|
+ o.game.hs_n2 = o.game.hs_n3
|
|
652
|
+ o.game.hs_s3 = o.game.score
|
|
653
|
+ o.game.hs_n3 = o.game.hs_name
|
|
654
|
+
|
|
655
|
+ elif o.game.score > o.game.hs_s2:
|
|
656
|
+ print('new second')
|
|
657
|
+ o.game.hs_s1 = o.game.hs_s2
|
|
658
|
+ o.game.hs_n1 = o.game.hs_n2
|
|
659
|
+ o.game.hs_s2 = o.game.score
|
|
660
|
+ o.game.hs_n2 = o.game.hs_name
|
|
661
|
+ else:
|
|
662
|
+ print('new third')
|
|
663
|
+ o.game.hs_s1 = o.game.score
|
|
664
|
+ o.game.hs_n1 = o.game.hs_name
|
|
665
|
+
|
|
666
|
+ print('new high score saving')
|
|
667
|
+ #o.save_song()
|
|
668
|
+ self.FSM.queued_state = 'toHighScore'
|
|
669
|
+ self.FSM.ToTransition('toBlueFade')
|
|
670
|
+ o.game.save_highscore()
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+ if o.keyState[16] > 0 and o.keyState[17] > 0:
|
|
675
|
+ pass
|
|
676
|
+ else:
|
|
677
|
+ _iter = 0
|
|
678
|
+ for k in o.keyState:
|
|
679
|
+ if k == 1:
|
|
680
|
+ cur_time = pygame.time.get_ticks()
|
|
681
|
+ print(cur_time - self.last_action)
|
|
682
|
+
|
|
683
|
+ #print('cur letter ', next(self.cycles[_iter]))
|
|
684
|
+ if _iter not in self.skips:
|
|
685
|
+ self.last_action = cur_time
|
|
686
|
+ self.cur_letter = next(self.cycles[_iter])
|
|
687
|
+ self.cur_but = _iter
|
|
688
|
+ self.draw(o)
|
|
689
|
+ o.center_block(self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey)
|
|
690
|
+
|
|
691
|
+ if _iter == 7:
|
|
692
|
+
|
|
693
|
+ self.input_string = self.input_string[:-1]
|
|
694
|
+ #self.input_string = self.input_string.rstrip(self.input_string[-1])
|
|
695
|
+ print('deleting')
|
|
696
|
+ self.last_action = 100000000
|
|
697
|
+ self.draw(o)
|
|
698
|
+ o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
|
|
699
|
+
|
|
700
|
+ elif _iter == 3:
|
|
701
|
+ print('caps')
|
|
702
|
+ if self.caps:
|
|
703
|
+ self.caps = False
|
|
704
|
+ self.labels = self.lc_labels
|
|
705
|
+
|
|
706
|
+ else:
|
|
707
|
+ self.caps = True
|
|
708
|
+ self.labels = self.uc_labels
|
|
709
|
+ self.draw(o)
|
|
710
|
+ o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
|
|
711
|
+ self.cycles = []
|
|
712
|
+ for l in self.labels:
|
|
713
|
+ self.cycles.append(cycle(l))
|
|
714
|
+
|
|
715
|
+ o.update_display(0)
|
|
716
|
+ _iter += 1
|
|
717
|
+
|
|
718
|
+ cur_time = pygame.time.get_ticks()
|
|
719
|
+ delay = cur_time - self.last_action
|
|
720
|
+ if delay > 1000 and delay < 100000:
|
|
721
|
+ self.input_string += self.cur_letter
|
|
722
|
+ self.last_action = 100000000
|
|
723
|
+ self.reset_cycles()
|
|
724
|
+ self.draw(o)
|
|
725
|
+
|
|
726
|
+ o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
|
|
727
|
+ o.update_display(0)
|
|
728
|
+
|
|
729
|
+ def draw(self, o):
|
|
730
|
+ x_size = o.width / 4
|
|
731
|
+ y_size = o.height / 4
|
|
732
|
+ og_x = 0
|
|
733
|
+ o_x = og_x
|
|
734
|
+ o_y = o.height
|
|
735
|
+ text_padding = 6
|
|
736
|
+
|
|
737
|
+ _id = 0
|
|
738
|
+ while _id < 16:
|
|
739
|
+ lab = ""
|
|
740
|
+ for i in self.labels[_id]:
|
|
741
|
+ lab += i
|
|
742
|
+
|
|
743
|
+ if _id == 8:
|
|
744
|
+ lab = "_12"
|
|
745
|
+
|
|
746
|
+ o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
|
|
747
|
+ o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
|
|
748
|
+
|
|
749
|
+ o_x = o_x + x_size
|
|
750
|
+ _id += 1
|
|
751
|
+ if _id % 4 == 0:
|
|
752
|
+ o_y -= y_size
|
|
753
|
+ o_x = og_x
|
|
754
|
+ o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue)
|
|
755
|
+
|
|
756
|
+ def reset_cycles(self):
|
|
757
|
+ self.cycles = []
|
|
758
|
+ for l in self.labels:
|
|
759
|
+ self.cycles.append(cycle(l))
|
|
760
|
+
|
|
761
|
+ def Exit(self):
|
|
762
|
+ pass
|
|
763
|
+
|
|
764
|
+#====================================
|
|
765
|
+
|
|
766
|
+def timer_event():
|
|
767
|
+ print('timer event level upsssssssssssssssssssssssssss')
|