Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Created a Bullet class.

  • Loading branch information...
commit 98a3972916f9a464a708db072b9e31357b31ef98 1 parent 335209a
Al Sweigart authored August 08, 2012

Showing 1 changed file with 23 additions and 10 deletions. Show diff stats Hide diff stats

  1. 33  square-shooter/square-shooter_makeover.py
33  square-shooter/square-shooter_makeover.py
@@ -153,7 +153,7 @@ def spawn(self):
153 153
             elif self.kind == "big":
154 154
                 new_kind = "medium"
155 155
 
156  
-            for i in range(2): # creates two new Bubbles
  156
+            for i in range(20): # creates two new Bubbles
157 157
                 spawned_bubbles.append(Bubble(new_kind))
158 158
                 spawned_bubbles[-1].pos.copy(self.pos)
159 159
 
@@ -172,6 +172,7 @@ class Ship(ObjectOnMap):
172 172
     def __init__(self):
173 173
         super(Ship, self).__init__(0.04) # all Ships are the same size.
174 174
         self._shield_timer = 0
  175
+        self._super_bullet_timer = 0
175 176
         self.accel_x = 0 # acceleration rate of the ship
176 177
         self.accel_y = 0
177 178
 
@@ -196,9 +197,11 @@ def update(self, delta_t):
196 197
         self.speed.x *= DECELERATION
197 198
         self.speed.y *= DECELERATION
198 199
 
199  
-        # shield degrades over time until it reaches 0.
200  
-        if self._shield_timer > 0:
  200
+        # shield and super bullets degrade over time until it reaches 0.
  201
+        if self.has_shield():
201 202
             self._shield_timer -= delta_t
  203
+        if self.has_super_bullets():
  204
+            self._super_bullet_timer -= delta_t
202 205
 
203 206
         super(Ship, self).update(delta_t)
204 207
 
@@ -210,6 +213,19 @@ def has_shield(self):
210 213
         """Returns True if this ship currently has a shield, False if it does not have a shield."""
211 214
         return self._shield_timer > 0
212 215
 
  216
+    def add_super_bullets(self, secs=6):
  217
+        """Extends the time on the ship's shield by secs seconds."""
  218
+        self._super_bullet_timer += secs
  219
+
  220
+    def has_super_bullets(self):
  221
+        """Returns True if this ship currently has a shield, False if it does not have a shield."""
  222
+        return self._super_bullet_timer > 0
  223
+
  224
+class Bullet(ObjectOnMap):
  225
+    def __init__(self):
  226
+        super(Bullet, self).__init__(0.01) # all Bullet objects are the same size
  227
+        self.shield = False
  228
+
213 229
 
214 230
 class GameWorld:
215 231
     bubbles = []
@@ -223,7 +239,6 @@ class GameWorld:
223 239
 
224 240
     death_timer = 0
225 241
     finish_timer = 0
226  
-    bullet_shield_timer = 0
227 242
     freeze_timer = 0
228 243
 
229 244
     level = 0
@@ -242,7 +257,6 @@ def init_level(self, level):
242 257
         self.death_timer = 0
243 258
         self.finish_timer = 0
244 259
 
245  
-        self.bullet_shield_timer = 0;
246 260
         self.freeze_timer = 0;
247 261
 
248 262
         del self.bubbles[:]
@@ -265,8 +279,7 @@ def update(self, delta_t):
265 279
                 self.powerups.pop(0)
266 280
         for i in self.powerups:
267 281
             i.age += delta_t
268  
-        if self.bullet_shield_timer > 0:
269  
-            self.bullet_shield_timer -= delta_t
  282
+
270 283
         if self.freeze_timer > 0:
271 284
             self.freeze_timer -= delta_t
272 285
 
@@ -303,7 +316,7 @@ def handle_collisions(self, delta_t):
303 316
         for b in self.bubbles:
304 317
             if self.bullet != None and b.collides_with(self.bullet):
305 318
                 self.bubbles.remove(b)
306  
-                if self.bullet_shield_timer <= 0:
  319
+                if not self.ship.has_super_bullets():
307 320
                     self.bullet = None
308 321
                 else:
309 322
                     # Push it along or it will just
@@ -355,7 +368,7 @@ def apply_powerup(self, powerup):
355 368
         if powerup.kind == "shield":
356 369
             self.ship.add_shield()
357 370
         elif powerup.kind == "bullet":
358  
-            self.bullet_shield_timer += 6
  371
+            self.ship.add_super_bullets()
359 372
         elif powerup.kind == "freeze":
360 373
             self.freeze_timer += 6
361 374
         else:
@@ -523,7 +536,7 @@ def render_bullet(self):
523 536
             RED,
524 537
             scale_and_round(pos.x, pos.y),
525 538
             int(round(bullet.radius * MAP_SIZE)))
526  
-        if self.model.bullet_shield_timer > 0:
  539
+        if self.model.ship.has_super_bullets():
527 540
             pygame.draw.rect(self.screen, RED, bbox, 1)
528 541
 
529 542
     def render_powerup(self, powerup):

0 notes on commit 98a3972

Please sign in to comment.
Something went wrong with that request. Please try again.