Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved the GameWorld.update() code.

  • Loading branch information...
commit 17b99c9cbeca7f38e0b084aea3eeb8b7278a3bc0 1 parent da250f0
Al Sweigart authored August 08, 2012

Showing 1 changed file with 14 additions and 12 deletions. Show diff stats Hide diff stats

  1. 26  square-shooter/square-shooter_makeover.py
26  square-shooter/square-shooter_makeover.py
@@ -48,7 +48,9 @@
48 48
 MAP_HALF_HEIGHT          = int(MAP_HEIGHT / 2.0)
49 49
 MAP_THREE_QUARTER_HEIGHT = int(3 * MAP_HEIGHT / 4.0)
50 50
 
51  
-DECELERATION = 0.99
  51
+DECELERATION = 0.99 # set between 0.0 and 1.0
  52
+MAX_EXPLOSION_SIZE = 0.5 # set between 0.0 and 1.0
  53
+MAX_POWERUP_AGE = 9 # in seconds
52 54
 
53 55
 def scale_and_round(x, y):
54 56
     """Returns x and y coordinates from 0.0 to 1.0 scaled to 0 to MAP_WIDTH or MAP_HEIGHT."""
@@ -299,21 +301,21 @@ def update(self, delta_t):
299 301
         self.handle_collisions(delta_t)
300 302
 
301 303
         # expand the explosions and delete them once they get too big
302  
-        if len(self.explosions) > 0:
303  
-            if self.explosions[0].radius > 0.5:
304  
-                self.explosions.pop(0)
305 304
         for i in self.explosions:
306 305
             i.radius += delta_t
  306
+        for i in range(len(self.explosions) - 1, -1, -1):
  307
+            if self.explosions[i].radius > MAX_EXPLOSION_SIZE:
  308
+                self.explosions.pop(i)
307 309
 
308 310
         # "age" the powerups on the map, and delete them if they get too old
309  
-        if len(self.powerups) > 0:
310  
-            if self.powerups[0].age > 9:
311  
-                self.powerups.pop(0)
312 311
         for i in self.powerups:
313 312
             i.age += delta_t
  313
+        for i in range(len(self.powerups) - 1, -1, -1):
  314
+            if self.powerups[i].age > MAX_POWERUP_AGE:
  315
+                self.powerups.pop(i)
314 316
 
315 317
         # check if all the bubbles have been destroyed
316  
-        if len(self.bubbles) == 0:
  318
+        if not len(self.bubbles):
317 319
             if self.afterfinish_timer > 0:
318 320
                 # the afterfinish timer is still counting down
319 321
                 self.afterfinish_timer -= delta_t;
@@ -325,8 +327,8 @@ def update(self, delta_t):
325 327
                 return
326 328
         elif not self.ship.has_freeze():
327 329
             # update all the bubbles
328  
-            for i in self.bubbles:
329  
-                i.update(delta_t)
  330
+            for bubble in self.bubbles:
  331
+                bubble.update(delta_t)
330 332
 
331 333
         # update the bullet
332 334
         if self.bullet != None:
@@ -348,8 +350,8 @@ def update(self, delta_t):
348 350
                 # player has run out of lives, level 0 will make the start screen display
349 351
                 self.level = 0 # Game over
350 352
             return
351  
-
352  
-        self.ship.update(delta_t)
  353
+        else:
  354
+            self.ship.update(delta_t)
353 355
 
354 356
     def handle_collisions(self, delta_t):
355 357
         for b in self.bubbles:

0 notes on commit 17b99c9

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