From 798ece1b88b5735e8c43d849150a6b084da5d8f5 Mon Sep 17 00:00:00 2001 From: Al Sweigart Date: Thu, 9 Aug 2012 23:47:53 -0700 Subject: [PATCH] Added an Explosion class, and created Bubble.render() --- square-shooter/square-shooter_makeover.py | 46 ++++++++++++++--------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/square-shooter/square-shooter_makeover.py b/square-shooter/square-shooter_makeover.py index 0a6f6d0..c1a544c 100644 --- a/square-shooter/square-shooter_makeover.py +++ b/square-shooter/square-shooter_makeover.py @@ -161,6 +161,13 @@ def spawn(self): return (spawned_bubbles, spawned_powerups) + def render(self, surface): + pygame.draw.circle( + surface, + self.color, + scale_and_round(self.pos.x, self.pos.y), + int(round(self.radius * MAP_SIZE)), + 1) class Powerup(ObjectOnMap): def __init__(self, pos): @@ -280,6 +287,21 @@ def render(self, surface): if self.shield: pygame.draw.rect(surface, RED, bbox, 1) +class Explosion(ObjectOnMap): + def __init__(self): + super(Explosion, self).__init__(0) # explosions start at size 0 + + def update(self, delta_t): + self.radius += delta_t + + def render(self, surface): + pygame.draw.circle( + surface, + RED, + scale_and_round(self.pos.x, self.pos.y), + int(round(self.radius * MAP_SIZE)), + 1) + class GameWorld: bubbles = [] explosions = [] @@ -323,8 +345,8 @@ def update(self, delta_t): self.handle_collisions(delta_t) # expand the explosions and delete them once they get too big - for i in self.explosions: - i.radius += delta_t + for explosion in self.explosions: + explosion.update(delta_t) for i in range(len(self.explosions) - 1, -1, -1): if self.explosions[i].radius > MAX_EXPLOSION_SIZE: self.explosions.pop(i) @@ -409,7 +431,7 @@ def handle_collisions(self, delta_t): self.powerups.remove(p) def spawn_explosion(self, bubble): - explosion = ObjectOnMap(0) + explosion = Explosion() explosion.pos.copy(bubble.pos) self.explosions.append(explosion) @@ -519,21 +541,11 @@ def render_game_world(self): self.world.bullet.render(self.screen) for bubble in self.world.bubbles: - pos = bubble.pos - pygame.draw.circle( - self.screen, - bubble.color, - scale_and_round(pos.x, pos.y), - int(round(bubble.radius * MAP_SIZE)), - 1) + bubble.render(self.screen) + for explosion in self.world.explosions: - pos = explosion.pos - pygame.draw.circle( - self.screen, - RED, - scale_and_round(pos.x, pos.y), - int(round(explosion.radius * MAP_SIZE)), - 1) + explosion.render(self.screen) + for i in self.world.powerups: self.render_powerup(i)