Skip to content

Commit

Permalink
Created a Bullet class.
Browse files Browse the repository at this point in the history
  • Loading branch information
asweigart committed Aug 8, 2012
1 parent 335209a commit 98a3972
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions square-shooter/square-shooter_makeover.py
Expand Up @@ -153,7 +153,7 @@ def spawn(self):
elif self.kind == "big":
new_kind = "medium"

for i in range(2): # creates two new Bubbles
for i in range(20): # creates two new Bubbles
spawned_bubbles.append(Bubble(new_kind))
spawned_bubbles[-1].pos.copy(self.pos)

Expand All @@ -172,6 +172,7 @@ class Ship(ObjectOnMap):
def __init__(self):
super(Ship, self).__init__(0.04) # all Ships are the same size.
self._shield_timer = 0
self._super_bullet_timer = 0
self.accel_x = 0 # acceleration rate of the ship
self.accel_y = 0

Expand All @@ -196,9 +197,11 @@ def update(self, delta_t):
self.speed.x *= DECELERATION
self.speed.y *= DECELERATION

# shield degrades over time until it reaches 0.
if self._shield_timer > 0:
# shield and super bullets degrade over time until it reaches 0.
if self.has_shield():
self._shield_timer -= delta_t
if self.has_super_bullets():
self._super_bullet_timer -= delta_t

super(Ship, self).update(delta_t)

Expand All @@ -210,6 +213,19 @@ def has_shield(self):
"""Returns True if this ship currently has a shield, False if it does not have a shield."""
return self._shield_timer > 0

def add_super_bullets(self, secs=6):
"""Extends the time on the ship's shield by secs seconds."""
self._super_bullet_timer += secs

def has_super_bullets(self):
"""Returns True if this ship currently has a shield, False if it does not have a shield."""
return self._super_bullet_timer > 0

class Bullet(ObjectOnMap):
def __init__(self):
super(Bullet, self).__init__(0.01) # all Bullet objects are the same size
self.shield = False


class GameWorld:
bubbles = []
Expand All @@ -223,7 +239,6 @@ class GameWorld:

death_timer = 0
finish_timer = 0
bullet_shield_timer = 0
freeze_timer = 0

level = 0
Expand All @@ -242,7 +257,6 @@ def init_level(self, level):
self.death_timer = 0
self.finish_timer = 0

self.bullet_shield_timer = 0;
self.freeze_timer = 0;

del self.bubbles[:]
Expand All @@ -265,8 +279,7 @@ def update(self, delta_t):
self.powerups.pop(0)
for i in self.powerups:
i.age += delta_t
if self.bullet_shield_timer > 0:
self.bullet_shield_timer -= delta_t

if self.freeze_timer > 0:
self.freeze_timer -= delta_t

Expand Down Expand Up @@ -303,7 +316,7 @@ def handle_collisions(self, delta_t):
for b in self.bubbles:
if self.bullet != None and b.collides_with(self.bullet):
self.bubbles.remove(b)
if self.bullet_shield_timer <= 0:
if not self.ship.has_super_bullets():
self.bullet = None
else:
# Push it along or it will just
Expand Down Expand Up @@ -355,7 +368,7 @@ def apply_powerup(self, powerup):
if powerup.kind == "shield":
self.ship.add_shield()
elif powerup.kind == "bullet":
self.bullet_shield_timer += 6
self.ship.add_super_bullets()
elif powerup.kind == "freeze":
self.freeze_timer += 6
else:
Expand Down Expand Up @@ -523,7 +536,7 @@ def render_bullet(self):
RED,
scale_and_round(pos.x, pos.y),
int(round(bullet.radius * MAP_SIZE)))
if self.model.bullet_shield_timer > 0:
if self.model.ship.has_super_bullets():
pygame.draw.rect(self.screen, RED, bbox, 1)

def render_powerup(self, powerup):
Expand Down

0 comments on commit 98a3972

Please sign in to comment.