Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Formatting changes and comments to init_level().

  • Loading branch information...
commit 3acad0029d66c17707ab31da7d88e24ad7577c84 1 parent d0e100c
Al Sweigart authored August 08, 2012

Showing 1 changed file with 49 additions and 44 deletions. Show diff stats Hide diff stats

  1. 93  square-shooter/square-shooter_makeover.py
93  square-shooter/square-shooter_makeover.py
@@ -198,13 +198,10 @@ def update(self, delta_t):
198 198
         self.speed.x *= DECELERATION
199 199
         self.speed.y *= DECELERATION
200 200
 
201  
-        # shield and super bullets degrade over time until it reaches 0.
202  
-        if self.has_shield():
203  
-            self._shield_timer -= delta_t
204  
-        if self.has_super_bullets():
205  
-            self._super_bullet_timer -= delta_t
206  
-        if self.has_freeze():
207  
-            self._freeze_timer -= delta_t
  201
+        # powerups degrade over time until it reaches 0.
  202
+        if self.has_shield():        self._shield_timer       -= delta_t
  203
+        if self.has_super_bullets(): self._super_bullet_timer -= delta_t
  204
+        if self.has_freeze():        self._freeze_timer       -= delta_t
208 205
 
209 206
         super(Ship, self).update(delta_t)
210 207
 
@@ -265,8 +262,8 @@ class GameWorld:
265 262
     bullet = None
266 263
     ship = None
267 264
 
268  
-    death_timer = 0
269  
-    finish_timer = 0
  265
+    afterdeath_timer = 0
  266
+    afterfinish_timer = 0
270 267
 
271 268
     level = 0
272 269
     score = 0
@@ -276,17 +273,25 @@ class GameWorld:
276 273
 
277 274
     def init_level(self, level):
278 275
         self.level = level
279  
-        if (level > self.max_level): self.max_level = level
  276
+
  277
+        # update the "max level" top score if needed
  278
+        if (level > self.max_level):
  279
+            self.max_level = level
  280
+
280 281
         if self.ship == None:
281 282
             self.ship = Ship()
282 283
         self.ship.add_shield() # add shield at the start of a level
283 284
         self.bullet = None;
284  
-        self.death_timer = 0
285  
-        self.finish_timer = 0
286 285
 
  286
+        self.afterdeath_timer = 0
  287
+        self.afterfinish_timer = 0
  288
+
  289
+        # clear out the bubbles, explosions, and power ups from the last level.
287 290
         del self.bubbles[:]
288 291
         del self.explosions[:]
289 292
         del self.powerups[:]
  293
+
  294
+        # create a number of starting big bubbles as the level number.
290 295
         for i in range(level):
291 296
             self.bubbles.append(Bubble("big"))
292 297
 
@@ -306,8 +311,8 @@ def update(self, delta_t):
306 311
             i.age += delta_t
307 312
 
308 313
         if len(self.bubbles) == 0:
309  
-            if self.finish_timer > 0:
310  
-                self.finish_timer -= delta_t;
  314
+            if self.afterfinish_timer > 0:
  315
+                self.afterfinish_timer -= delta_t;
311 316
             else:
312 317
                 self.level += 1
313 318
                 self.lives += 1
@@ -323,8 +328,8 @@ def update(self, delta_t):
323 328
                 self.bullet = None
324 329
 
325 330
         if self.ship == None:
326  
-            if self.death_timer > 0:
327  
-                self.death_timer -= delta_t
  331
+            if self.afterdeath_timer > 0:
  332
+                self.afterdeath_timer -= delta_t
328 333
             elif self.lives > 0:
329 334
                 self.ship = Ship()
330 335
                 self.ship.add_shield() # add shields at the start of a life
@@ -350,7 +355,7 @@ def handle_collisions(self, delta_t):
350 355
                 self.spawn_explosion(b)
351 356
                 self.mark_score(b)
352 357
                 if len(self.bubbles) == 0:
353  
-                    self.finish_timer = 3
  358
+                    self.afterfinish_timer = 3
354 359
                 break
355 360
             elif self.ship != None:
356 361
                 if not b.collides_with(self.ship):
@@ -360,7 +365,7 @@ def handle_collisions(self, delta_t):
360 365
                 self.spawn_explosion(self.ship)
361 366
                 self.ship = None
362 367
                 self.lives -= 1
363  
-                self.death_timer = 3;
  368
+                self.afterdeath_timer = 3;
364 369
                 break
365 370
 
366 371
         if self.ship == None: return
@@ -402,8 +407,8 @@ def apply_powerup(self, powerup):
402 407
 
403 408
 
404 409
 class GameScreen:
405  
-    def __init__(self, model, screen):
406  
-        self.model = model
  410
+    def __init__(self, world, screen):
  411
+        self.world = world
407 412
 
408 413
         self.screen = screen
409 414
         self.width, self.height = screen.get_size()
@@ -422,7 +427,7 @@ def __init__(self, model, screen):
422 427
         self.render_backround()
423 428
 
424 429
     def render(self):
425  
-        m = self.model
  430
+        m = self.world
426 431
 
427 432
         self.screen.blit(self.bglayer, (0, 0))
428 433
 
@@ -473,16 +478,16 @@ def render_title_screen(self):
473 478
         text = self.msg_font.render("presents", False, GREEN)
474 479
         self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_QUARTER_HEIGHT)))
475 480
 
476  
-        high_score = "High score: " + str(self.model.high_score)
  481
+        high_score = "High score: " + str(self.world.high_score)
477 482
         text = self.msg_font.render(high_score, False, GREEN)
478 483
         self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
479 484
 
480  
-        max_level = "Max level: " + str(self.model.max_level)
  485
+        max_level = "Max level: " + str(self.world.max_level)
481 486
         text = self.msg_font.render(max_level, False, GREEN)
482 487
         self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
483 488
 
484 489
     def render_game_world(self):
485  
-        m = self.model
  490
+        m = self.world
486 491
 
487 492
         self.screen.set_clip((0, 0, MAP_WIDTH, MAP_HEIGHT))
488 493
 
@@ -511,7 +516,7 @@ def render_game_world(self):
511 516
         self.screen.set_clip(None)
512 517
 
513 518
     def render_ship(self):
514  
-        ship = self.model.ship
  519
+        ship = self.world.ship
515 520
         pos = ship.pos
516 521
         bbox = pygame.draw.circle(
517 522
             self.screen,
@@ -524,18 +529,18 @@ def render_ship(self):
524 529
             scale_and_round(pos.x, pos.y),
525 530
             int(round(ship.radius * 0.5 * MAP_SIZE)),
526 531
             1)
527  
-        if self.model.ship.has_shield():
  532
+        if self.world.ship.has_shield():
528 533
             pygame.draw.rect(self.screen, SILVER, bbox, 1)
529 534
 
530 535
     def render_bullet(self):
531  
-        bullet = self.model.bullet
  536
+        bullet = self.world.bullet
532 537
         pos = bullet.pos
533 538
         bbox = pygame.draw.circle(
534 539
             self.screen,
535 540
             RED,
536 541
             scale_and_round(pos.x, pos.y),
537 542
             int(round(bullet.radius * MAP_SIZE)))
538  
-        if self.model.ship.has_super_bullets():
  543
+        if self.world.ship.has_super_bullets():
539 544
             pygame.draw.rect(self.screen, RED, bbox, 1)
540 545
 
541 546
     def render_powerup(self, powerup):
@@ -580,8 +585,8 @@ def render_pause_text(self):
580 585
 
581 586
 screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
582 587
 clock = pygame.time.Clock()
583  
-model = GameWorld()
584  
-renderer = GameScreen(model, screen)
  588
+world = GameWorld()
  589
+renderer = GameScreen(world, screen)
585 590
 
586 591
 pygame.display.set_caption("Square Shooter Desktop Edition")
587 592
 pygame.event.set_blocked(pygame.MOUSEMOTION)
@@ -598,30 +603,30 @@ def render_pause_text(self):
598 603
         if ev.key == pygame.K_ESCAPE:
599 604
             running = False
600 605
         elif ev.key == pygame.K_q:
601  
-            if model.level > 0:
602  
-                model.level = 0
  606
+            if world.level > 0:
  607
+                world.level = 0
603 608
             else:
604 609
                 running = False
605 610
         elif ev.key == pygame.K_p:
606  
-            if model.level == 0:
607  
-                model.score = 0
608  
-                model.lives = 1
609  
-                model.init_level(1)
  611
+            if world.level == 0:
  612
+                world.score = 0
  613
+                world.lives = 1
  614
+                world.init_level(1)
610 615
             else:
611 616
                 renderer.game_paused = not renderer.game_paused
612 617
     elif ev.type == pygame.MOUSEBUTTONDOWN:
613 618
         # on mouse down, fire a bullet and start the thruster of the ship
614  
-        if (model.level > 0) and (model.ship != None) and (not renderer.game_paused):
  619
+        if (world.level > 0) and (world.ship != None) and (not renderer.game_paused):
615 620
             x, y = ev.pos
616  
-            if model.bullet == None:
617  
-                model.bullet = model.ship.shoot_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))[0]
618  
-            model.ship.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
  621
+            if world.bullet == None:
  622
+                world.bullet = world.ship.shoot_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))[0]
  623
+            world.ship.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
619 624
     elif ev.type == pygame.MOUSEBUTTONUP:
620 625
         # on mouse up, stop accelerating the ship
621  
-        if (model.level > 0) and (model.ship != None):
622  
-            model.ship.stop_thrust()
  626
+        if (world.level > 0) and (world.ship != None):
  627
+            world.ship.stop_thrust()
623 628
 
624 629
     # Simulations need the time in seconds, dammit!
625  
-    if model.level > 0 and not renderer.game_paused:
626  
-        model.update(delta_t * 0.001)
  630
+    if world.level > 0 and not renderer.game_paused:
  631
+        world.update(delta_t * 0.001)
627 632
     renderer.render()

0 notes on commit 3acad00

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