-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestructive_wall.py
71 lines (53 loc) · 2 KB
/
destructive_wall.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
from random import choice, randint
import pygame
from tile import Tile
from powerup import PowerUp
from settings import EXPLOSION_TIME, POWERUPS_ARRAY
class DestructiveWall(Tile):
def __init__(self, pos, groups, name, explosion_sprites, poweup_sprites):
super().__init__(pos, groups, name)
self.explosion_sprites = explosion_sprites
self.powerup_sprites = poweup_sprites
self.wall_sprites = groups[0]
self.powerup_array = POWERUPS_ARRAY
self.is_dead = False
self.run_timer = True
self.timer = EXPLOSION_TIME
def collision_explosion(self):
"""Checks collision with explosions."""
if self.is_dead:
return
explosions_hit = pygame.sprite.spritecollide(
self, self.explosion_sprites, False
)
if explosions_hit:
self.is_dead = True
self.wall_sprites.remove(self)
def destroy(self):
"""Destroys the wall and spawns a powerup."""
# make this powerup spawn randomly (sometimes) based on some proportion (ronaldinho and wifi should be less common).
if randint(0, 100) < 50:
PowerUp(
(self.rect.x, self.rect.y),
[self.powerup_sprites],
choice(self.powerup_array),
self.explosion_sprites,
)
self.run_timer = False
self.kill()
def spawn_powerup(self):
"""Spawns a powerup after a certain amount of time."""
if not self.is_dead:
return
if not self.run_timer:
return
# this <= 0 prevents the destructive_wall to appear for 1 frame before the powerup.
# Since the logic in explosion.py is self.timer > 0.
if self.timer <= 0:
self.destroy()
else:
self.timer -= 1
def update(self):
"""Checks for collision with explosions every frame and spawns powerup if needed."""
self.collision_explosion()
self.spawn_powerup()