-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.py
executable file
·554 lines (443 loc) · 19.3 KB
/
assets.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
'''@authors Simone Orsi (305461) and Martina Gualtieri (308783)'''
import random, math, constants
from actor import Actor, Arena
# -- Background -----
class Background(Actor):
def __init__(self, arena, pos, s, setting):
self._x, self._y = pos
self._dx = -s
self._setting = setting
self._arena = arena
arena.add(self)
self._img_x, self._img_y, self._w, self._h = self.symbol()
def collide(self, other):
if self._setting == constants.GROUND and isinstance(other, Bullet) and other.direction() == constants.BULLET_DOWN:
if random.randint(0, 4) == 0:
hole = Hole(self._arena, (other.position()[0], self._y), True, constants.SMALL_SIZE)
self._arena.remove(other)
return True
return False
def move(self):
self._x += self._dx
if self._x + self._dx < -constants.MAX_IMAGE_WIDTH:
self._x = constants.MAX_IMAGE_WIDTH
self._x += self._dx
def position(self):
return self._x, self._y, self._w, self._h
def arena(self):
arena_w, arena_h = self._arena.size()
return arena_w, arena_h
def symbol(self):
if self._setting == constants.MOUNTAINS:
return 0, 50, 512, 210
elif self._setting == constants.HILLS:
return 0, 256, 512, 126
elif self._setting == constants.CITY:
return 0, 384, 512, 70
elif self._setting == constants.GROUND:
return 0, 512, 512, 15
def setting(self):
return self._setting
def get_speed(self):
return self._dx
def set_speed(self):
self._dx -= 1
def priority(self):
return constants.LOW_PRIORITY
class Obstacle(Actor):
def collide(self, other):
raise NotImplementedError('Abstract method')
def move(self):
self._dx = self.speed()
arena_w, arena_h = self._arena.size()
self._x += self._dx
if self._x + self._w < 0:
self._x = random.randint(arena_w, arena_w*4)
''' x is a random number between canvas width and 4 times this size.
This way there is more space to move objects.
'''
self.check_position()
def reset_x(self):
self._x = constants.NEGATIVE_X
self.move()
def check_position(self):
arena_w, arena_h = self._arena.size()
move_element = True
while move_element:
move_element = False
for a in self._arena.actors():
if self is not a and isinstance(a, Obstacle):
a_x, a_y, a_w, a_h = a.position()
distance_0 = self._x - (a_x + a_w)
distance_1 = a_x - (self._x + self._w)
#Check whether the objects are sufficiently spaced
if abs(distance_0) <= constants.MIN_DISTANCE or abs(distance_1) <= constants.MIN_DISTANCE:
self._x = random.randint(arena_w, arena_w*4)
move_element = True
def position(self) -> (int, int, int, int):
return self._x, self._y, self._w, self._h
def symbol(self) -> (int, int, int, int):
raise NotImplementedError('Abstract method')
def speed(self):
ground = None
for a in self._arena.actors():
if isinstance(a, Background) and a.setting() == constants.GROUND:
ground = a
if ground != None:
return int(ground.get_speed())
else:
return constants.OBSTACLE_SPEED
def priority(self) -> int:
return constants.MEDIUM_LOW_PRIORITY
# -- Hole -----
class Hole(Obstacle):
def __init__(self, arena, pos, explode, size):
self._x, self._y = pos
self._explode = explode
self._size = size
self._arena = arena
arena.add(self)
self._img_x, self._img_y, self._w, self._h = self.symbol()
self._dx = self.speed()
self.check_position()
def collide(self, other):
pass
def move(self):
self._dx = self.speed()
arena_w, arena_h = self._arena.size()
self._x += self._dx
if self._x + self._w < 0:
if not self._explode:
self._x = random.randint(arena_w, arena_w*4)
''' x is a random number between canvas width and 4 times this size.
This way there is more space to move objects.
'''
self.check_position()
else:
self._arena.remove(self)
def symbol(self):
if self._size == constants.SMALL_SIZE:
return 138, 140, 13, 12
elif self._size == constants.BIG_SIZE:
return 154, 140, 25, 13
# -- Rock -----
class Rock(Obstacle):
def __init__(self, arena, pos, size):
self._x, self._y = pos
self._size = size
self._damaged = False
self._arena = arena
arena.add(self)
self._img_x, self._img_y, self._w, self._h = self.symbol()
def collide(self, other):
if isinstance(other, Rover) or (isinstance(other, Bullet) and self._size == constants.SMALL_SIZE):
self.reset_x()
return True
elif isinstance(other, Bullet) and self._size == constants.BIG_SIZE:
if self._damaged:
self.reset_x()
self._damaged = False
return True
else:
self._damaged = True
return False
def symbol(self):
if self._size == constants.SMALL_SIZE:
return 80, 203, 14, 12
elif self._size == constants.BIG_SIZE:
return 112, 199, 13, 16
# -- Cannon -----
class Cannon(Obstacle):
def __init__(self, arena, pos):
self._x, self._y = pos
self._arena = arena
arena.add(self)
self._img_x, self._img_y, self._w, self._h = self.symbol()
def collide(self, other):
if (isinstance(other, Rover) or
(isinstance(other, Bullet) and (other.direction() == constants.BULLET_UP or other.direction() == constants.BULLET_RIGHT))):
self.reset_x()
return True
return False
def symbol(self):
return 109, 246, 16, 16
# -- Bullet -----
class Bullet(Actor):
def __init__(self, arena, pos, direction):
self._initial_x, self._initial_y = pos
self._x, self._y = self._initial_x, self._initial_y
self._img_x, self._img_y, self._w, self._h = 0, 0, 0, 0
self._direction = direction
self._distance = 120
self._dy, self._dx = 6, 0
self._arena = arena
arena.add(self)
self._img_x, self._img_y, self._w, self._h = self.symbol()
def collide(self, other):
if isinstance(other, Rock) or (isinstance(other, Bullet) and other.direction() != self._direction):
self._arena.remove(self)
return True
return False
def move(self):
self._dx = self.speed()
arena_w, arena_h = self._arena.size()
if self._direction == constants.BULLET_UP:
self._y -= self._dy
if self._y < 0:
self._arena.remove(self)
elif self._direction == constants.BULLET_RIGHT:
self._x += self._dx
if self._x > self._initial_x + self._distance:
self._arena.remove(self)
elif self._direction == constants.BULLET_DOWN:
self._y += self._dy
if self._x < 0 or self._x > arena_w:
self._arena.remove(self)
elif self._direction == constants.BULLET_LEFT:
self._x -= self._dx
if self._x < 0 or self._x > arena_w:
self._arena.remove(self)
def position(self):
return self._x, self._y, self._w, self._h
def symbol(self):
if self._direction == constants.BULLET_UP:
self._img_x, self._img_y, self._w, self._h = 197, 231, 5, 5
elif self._direction == constants.BULLET_RIGHT:
if self._initial_x < self._x < self._initial_x + self._distance/2:
self._img_x, self._img_y, self._w, self._h = 193, 143, 10, 4
elif self._initial_x + self._distance/2 < self._x < self._initial_x + 3 * self._distance/4:
self._img_x, self._img_y, self._w, self._h = 225, 142, 6, 7
elif self._initial_x + 3 * self._distance/4 < self._x < self._initial_x + self._distance:
self._img_x, self._img_y, self._w, self._h = 239, 140, 8, 10
elif self._direction == constants.BULLET_LEFT:
self._img_x, self._img_y, self._w, self._h = 215, 143, 4, 4
elif self._direction == constants.BULLET_DOWN:
self._img_x, self._img_y, self._w, self._h = 213, 231, 5, 6
return self._img_x, self._img_y, self._w, self._h
def direction(self):
return self._direction
def priority(self):
return constants.MEDIUM_HIGH_PRIORITY
def speed(self):
ground = None
for a in self._arena.actors():
if isinstance(a, Background) and a.setting() == constants.GROUND:
ground = a
if ground != None:
speed = -int(ground.get_speed())
if self._direction == constants.BULLET_LEFT:
speed *= 2
return speed
else:
if self._direction == constants.BULLET_LEFT:
return constants.BULLET_LEFT_SPEED
else:
return constants.BULLET_SPEED
# -- Ufo -----
class Ufo(Actor):
def __init__(self, arena, pos, s, vehicle):
self._x, self._y = pos
self._y_backup = self._y
self._count, self._timer = 0, 0
self._img_x, self._img_y, self._w, self._h = 0, 0, 0, 0
self._left, self._up = True, False
self._rotate = False
self._vehicle = vehicle
self._dx = -s
if self._vehicle == constants.SHOOTER_UFO:
self._dy = -(s/3)
elif self._vehicle == constants.BONUS_UFO:
self._dy = -(s/2)
self._arena = arena
arena.add(self)
self._img_x, self._img_y, self._w, self._h = self.symbol()
def collide(self, other):
if isinstance(other, Bullet) and other.direction() != constants.BULLET_DOWN:
arena_w, arena_h = self._arena.size()
if self._vehicle == constants.BONUS_UFO:
for r in self._arena.actors():
if isinstance(r, Rover):
r.set_invincible()
self._timer = 1
self._x = arena_w + 300
self._left = True
return True
return False
def move(self):
if self._x % 30 == 0:
self._rotate = not self._rotate
arena_w, arena_h = self._arena.size()
if self._x + self._dx < constants.MARGIN:
self._left = False
elif self._x + self._dx > arena_w - constants.MARGIN:
self._left = True
if self._left:
self._x += self._dx
else:
self._x -= self._dx
if self._vehicle == constants.SHOOTER_UFO:
random_dy = random.choice([self._dy, -self._dy])
if self._y + random_dy < constants.MARGIN or self._y + random_dy > constants.TOP_MARGIN_SHOOTER_UFO:
self._y -= random_dy
else:
self._y += random_dy
elif self._vehicle == constants.BONUS_UFO:
if self._y + self._dy < constants.MARGIN:
self._up = False
elif self._y + self._dy > constants.TOP_MARGIN_BONUS_UFO:
self._up = True
if self._up:
self._y += self._dy
else:
self._y -= self._dy
if self._timer > 0:
self._timer += 1
if self._timer == constants.MAX_TIMER:
for r in self._arena.actors():
if isinstance(r, Rover):
r.set_invincible()
self._timer = 0
def get_rotate(self):
return self._rotate
def position(self):
return self._x, self._y, self._w, self._h
def arena(self):
arena_w, arena_h = self._arena.size()
return arena_w, arena_h
def symbol(self):
if self._vehicle == constants.SHOOTER_UFO:
self._img_x, self._img_y, self._w, self._h = 67, 230, 15, 8
elif self._vehicle == constants.BONUS_UFO:
if self._rotate:
self._img_x, self._img_y, self._w, self._h = 104, 226, 13, 16
else:
self._img_x, self._img_y, self._w, self._h = 88, 227, 14, 14
return self._img_x, self._img_y, self._w, self._h
def priority(self):
return constants.MEDIUM_LOW_PRIORITY
# -- Rover -----
class Rover(Actor):
def __init__(self, arena, pos, s, player):
self._x, self._y = pos
self._dx, self._dy, self._g, self._speed = 0, 0, 0.4, s
self._damaged, self._firstrun, self._invincible = False, True, False
self._img_x, self._img_y, self._w, self._h = 0, 0, 0, 0
self._destruction_timer = 0
self._player = player
self._sprite_type = ""
self._arena = arena
arena.add(self)
self._img_x, self._img_y, self._w, self._h = self.symbol()
def move(self):
arena_w, arena_h = self._arena.size()
if not self._damaged:
self._y += self._dy
self._dy += self._g
if (self._y > constants.GROUND_LEVEL - self._h):
self._y = constants.GROUND_LEVEL - self._h
self._dy = 0
self._x += self._dx
if self._x < 0:
self._x = 0
elif self._x > arena_w - self._w:
self._x = arena_w - self._w
elif self._damaged:
self._destruction_timer += 1
if self._damaged and self._destruction_timer > 30:
self._arena.remove(self)
def go_right(self):
self._dx, self._dy = +self._speed, 0
def go_left(self):
self._dx, self._dy = -self._speed, 0
def stay(self):
self._dx, self._dy = 0, 0
def jump(self):
arena_w, arena_h = self._arena.size() #Arena size getter
if self._y >= constants.GROUND_LEVEL - self._h:
self._dy = -6
def set_invincible(self):
self._invincible = not self._invincible
def damaged(self):
return self._damaged
def collide(self, other):
if not self._invincible: #Just for testing purposes, I swear I never cheated.. almost never
if (isinstance(other, Hole) or isinstance(other, Rock) or isinstance(other, Cannon) or
(isinstance(other, Bullet) and (other.direction() == constants.BULLET_DOWN or other.direction() == constants.BULLET_LEFT))):
self._damaged = True
return True
return False
def position(self):
return self._x, self._y, self._w, self._h
def player(self):
return self._player
def arena(self):
arena_w, arena_h = self._arena.size()
return arena_w, arena_h
def get_sprite_type(self):
return self._sprite_type
def symbol(self):
''' #(I) As long as rover is invincible
'''
''' #(A) As long as rover is alive and not invincible
'''
''' #(D) As long as rover is beeing destroyed
'''
if self._player == constants.PLAYER_1:
if self._invincible: # -- P1(I) ----
if self._destruction_timer <= 0:
if self._dy < 0:
self._sprite_type = constants.JUMPING_SPRITE
self._img_x, self._img_y, self._w, self._h = 8, 285, 27, 26 #Jumping sprite
else:
self._sprite_type = constants.DEFAULT_SPRITE
self._img_x, self._img_y, self._w, self._h = 6, 250, 32, 23 #Default sprite
else: # -- P1(A) -----
if self._destruction_timer <= 0:
if self._dy < 0:
self._sprite_type = constants.JUMPING_SPRITE
self._img_x, self._img_y, self._w, self._h = 47, 103, 27, 27 #Jumping sprite
else:
self._sprite_type = constants.DEFAULT_SPRITE
self._img_x, self._img_y, self._w, self._h = 212, 158, 32, 23 #Default sprite
# -- P1(D) ----
if 0 < self._destruction_timer < constants.ROVER_DESTRUCTION_ANIMATION_0:
if self._destruction_timer == 4:
self._y -= 4
self._img_x, self._img_y, self._w, self._h = 113, 101, 46, 32
elif constants.ROVER_DESTRUCTION_ANIMATION_0 < self._destruction_timer < constants.ROVER_DESTRUCTION_ANIMATION_1:
self._img_x, self._img_y, self._w, self._h = 165, 101, 42, 32
elif constants.ROVER_DESTRUCTION_ANIMATION_1 < self._destruction_timer < constants.ROVER_DESTRUCTION_ANIMATION_2:
if self._destruction_timer == 4:
self._y += 1
self._img_x, self._img_y, self._w, self._h = 214, 102, 41, 30
elif self._player == constants.PLAYER_2:
if self._invincible: # -- P2(I) ----
if self._destruction_timer <= 0:
if self._dy < 0:
self._sprite_type = constants.JUMPING_SPRITE
self._img_x, self._img_y, self._w, self._h = 50, 284, 27, 27 #Jumping sprite
else:
self._sprite_type = constants.DEFAULT_SPRITE
self._img_x, self._img_y, self._w, self._h = 47, 250, 32, 23 #Default sprite
else: # -- P2(A) -----
if self._destruction_timer <= 0:
if self._dy < 0:
self._sprite_type = constants.JUMPING_SPRITE
self._img_x, self._img_y, self._w, self._h = 49, 152, 27, 27 #Jumping sprite
else:
self._sprite_type = constants.DEFAULT_SPRITE
self._img_x, self._img_y, self._w, self._h = 248, 158, 32, 23 #Default sprite
# -- P2(D) ----
if 0 < self._destruction_timer < constants.ROVER_DESTRUCTION_ANIMATION_0:
if self._destruction_timer == 4:
self._y -= 4
self._img_x, self._img_y, self._w, self._h = 113, 101, 46, 32
elif constants.ROVER_DESTRUCTION_ANIMATION_0 < self._destruction_timer < constants.ROVER_DESTRUCTION_ANIMATION_1:
self._img_x, self._img_y, self._w, self._h = 165, 101, 42, 32
elif constants.ROVER_DESTRUCTION_ANIMATION_1 < self._destruction_timer < constants.ROVER_DESTRUCTION_ANIMATION_2:
if self._destruction_timer == 4:
self._y += 1
self._img_x, self._img_y, self._w, self._h = 214, 102, 41, 30
return self._img_x, self._img_y, self._w, self._h
def priority(self):
return constants.HIGH_PRIORITY