Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Converted shield_timer member to a bunch of methods.

  • Loading branch information...
commit 335209a898f6bfea494c04427bbfc17f98010e9b 1 parent 7170b5a
Al Sweigart authored August 08, 2012

Showing 1 changed file with 20 additions and 8 deletions. Show diff stats Hide diff stats

  1. 28  square-shooter/square-shooter_makeover.py
28  square-shooter/square-shooter_makeover.py
@@ -171,7 +171,7 @@ def __init__(self, pos):
171 171
 class Ship(ObjectOnMap):
172 172
     def __init__(self):
173 173
         super(Ship, self).__init__(0.04) # all Ships are the same size.
174  
-        self.shield_timer = 0
  174
+        self._shield_timer = 0
175 175
         self.accel_x = 0 # acceleration rate of the ship
176 176
         self.accel_y = 0
177 177
 
@@ -195,8 +195,22 @@ def update(self, delta_t):
195 195
         self.speed.y += self.accel_y
196 196
         self.speed.x *= DECELERATION
197 197
         self.speed.y *= DECELERATION
  198
+
  199
+        # shield degrades over time until it reaches 0.
  200
+        if self._shield_timer > 0:
  201
+            self._shield_timer -= delta_t
  202
+
198 203
         super(Ship, self).update(delta_t)
199 204
 
  205
+    def add_shield(self, secs=6):
  206
+        """Extends the time on the ship's shield by secs seconds."""
  207
+        self._shield_timer += secs
  208
+
  209
+    def has_shield(self):
  210
+        """Returns True if this ship currently has a shield, False if it does not have a shield."""
  211
+        return self._shield_timer > 0
  212
+
  213
+
200 214
 class GameWorld:
201 215
     bubbles = []
202 216
     explosions = []
@@ -223,7 +237,7 @@ def init_level(self, level):
223 237
         if (level > self.max_level): self.max_level = level
224 238
         if self.ship == None:
225 239
             self.ship = Ship()
226  
-        self.ship.shield_timer += 6 # add 6 seconds of shield at the start of a level
  240
+        self.ship.add_shield() # add shield at the start of a level
227 241
         self.bullet = None;
228 242
         self.death_timer = 0
229 243
         self.finish_timer = 0
@@ -251,8 +265,6 @@ def update(self, delta_t):
251 265
                 self.powerups.pop(0)
252 266
         for i in self.powerups:
253 267
             i.age += delta_t
254  
-        if (self.ship != None) and (self.ship.shield_timer > 0):
255  
-            self.ship.shield_timer -= delta_t
256 268
         if self.bullet_shield_timer > 0:
257 269
             self.bullet_shield_timer -= delta_t
258 270
         if self.freeze_timer > 0:
@@ -280,7 +292,7 @@ def update(self, delta_t):
280 292
                 self.death_timer -= delta_t
281 293
             elif self.lives > 0:
282 294
                 self.ship = Ship()
283  
-                self.ship.shield_timer += 6 # add 6 seconds of shield at the start of a life
  295
+                self.ship.add_shield() # add shields at the start of a life
284 296
             else:
285 297
                 self.level = 0 # Game over
286 298
             return
@@ -308,7 +320,7 @@ def handle_collisions(self, delta_t):
308 320
             elif self.ship != None:
309 321
                 if not b.collides_with(self.ship):
310 322
                     continue
311  
-                if self.ship.shield_timer > 0:
  323
+                if self.ship.has_shield():
312 324
                     continue
313 325
                 self.spawn_explosion(self.ship)
314 326
                 self.ship = None
@@ -341,7 +353,7 @@ def mark_score(self, bubble):
341 353
 
342 354
     def apply_powerup(self, powerup):
343 355
         if powerup.kind == "shield":
344  
-            self.ship.shield_timer += 6
  356
+            self.ship.add_shield()
345 357
         elif powerup.kind == "bullet":
346 358
             self.bullet_shield_timer += 6
347 359
         elif powerup.kind == "freeze":
@@ -500,7 +512,7 @@ def render_ship(self):
500 512
             scale_and_round(pos.x, pos.y),
501 513
             int(round(ship.radius * 0.5 * MAP_SIZE)),
502 514
             1)
503  
-        if self.model.ship.shield_timer > 0:
  515
+        if self.model.ship.has_shield():
504 516
             pygame.draw.rect(self.screen, SILVER, bbox, 1)
505 517
 
506 518
     def render_bullet(self):

0 notes on commit 335209a

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