Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored the GameScreen.render() method, and got rid of the green b…

…ox drawing code.
  • Loading branch information...
commit 2657bd7ec349b982d4c07c84de1471a35cef83aa 1 parent 577b420
Al Sweigart authored August 09, 2012

Showing 1 changed file with 24 additions and 26 deletions. Show diff stats Hide diff stats

  1. 50  square-shooter/square-shooter_makeover.py
50  square-shooter/square-shooter_makeover.py
@@ -428,36 +428,31 @@ def __init__(self, world, screen):
428 428
 
429 429
         self.game_paused = False
430 430
 
431  
-        self.render_background()
432 431
 
433 432
     def render(self):
434  
-        m = self.world
435  
-
436 433
         self.screen.blit(self.bglayer, (0, 0))
  434
+        self.render_background(self.world.level)
437 435
 
438  
-        if m.level == 0:
  436
+        if self.world.level == 0:
439 437
             self.render_title_screen()
440  
-            # Hide the [P]ause text.
441  
-            self.screen.fill(GREEN, (MAP_WIDTH + 20, 424, 140, 24))
442 438
         else:
443 439
             self.render_game_world()
444  
-            # Hide the [P]lay text.
445  
-            self.screen.fill(GREEN, (MAP_WIDTH + 20, 400, 140, 24))
446  
-            if self.game_paused: self.render_pause_text()
  440
+            if self.game_paused:
  441
+                self.render_pause_text()
447 442
 
448  
-        text = self.hud_font.render(str(m.level), False, BLACK)
  443
+        text = self.hud_font.render(str(self.world.level), False, BLACK)
449 444
         self.screen.blit(text, (MAP_WIDTH + 20, 48))
450  
-        text = self.hud_font.render(str(m.lives), False, BLACK)
  445
+        text = self.hud_font.render(str(self.world.lives), False, BLACK)
451 446
         self.screen.blit(text, (MAP_WIDTH + 20, 48 * 3))
452  
-        text = self.hud_font.render(str(m.score), False, BLACK)
  447
+        text = self.hud_font.render(str(self.world.score), False, BLACK)
453 448
         self.screen.blit(text, (MAP_WIDTH + 20, 48 * 5))
454 449
 
455  
-        #fps_text = self.msg_font.render(str(self.fps), False, GREEN)
456  
-        #self.screen.blit(fps_text, (0, 0))
  450
+        fps_text = self.msg_font.render(str(self.fps), False, GREEN)
  451
+        self.screen.blit(fps_text, (0, 0))
457 452
 
458 453
         pygame.display.flip()
459 454
 
460  
-    def render_background(self):
  455
+    def render_background(self, level):
461 456
         self.bglayer.fill(BLACK)
462 457
         self.bglayer.fill(GREEN, (MAP_WIDTH, 0, WINDOW_WIDTH - MAP_WIDTH, MAP_HEIGHT))
463 458
 
@@ -466,10 +461,15 @@ def render_background(self):
466 461
             text = self.hud_font.render(msg[i], False, BLACK)
467 462
             self.bglayer.blit(text, (MAP_WIDTH + 20, i * 96))
468 463
 
469  
-        msg = ["[Q]uit", "[P]ause", "[P]lay"]
470  
-        for i in range(3):
471  
-            text = self.msg_font.render(msg[i], False, WHITE)
472  
-            self.bglayer.blit(text, (MAP_WIDTH + 20, 448 - i * 24))
  464
+        text = self.msg_font.render('[Q]uit', False, WHITE)
  465
+        self.bglayer.blit(text, (MAP_WIDTH + 20, 424))
  466
+
  467
+        if level == 0:
  468
+            msg = '[P]lay'
  469
+        else:
  470
+            msg = '[P]ause'
  471
+        text = self.msg_font.render(msg, False, WHITE)
  472
+        self.bglayer.blit(text, (MAP_WIDTH + 20, 400))
473 473
 
474 474
     def render_title_screen(self):
475 475
         text = self.hud_font.render("SQUARE", False, GREEN)
@@ -491,14 +491,12 @@ def render_title_screen(self):
491 491
         self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
492 492
 
493 493
     def render_game_world(self):
494  
-        m = self.world
495  
-
496 494
         self.screen.set_clip((0, 0, MAP_WIDTH, MAP_HEIGHT))
497 495
 
498  
-        if m.ship != None: self.render_ship()
499  
-        if m.bullet != None: self.render_bullet()
  496
+        if self.world.ship != None: self.render_ship()
  497
+        if self.world.bullet != None: self.render_bullet()
500 498
 
501  
-        for bubble in m.bubbles:
  499
+        for bubble in self.world.bubbles:
502 500
             pos = bubble.pos
503 501
             pygame.draw.circle(
504 502
                 self.screen,
@@ -506,7 +504,7 @@ def render_game_world(self):
506 504
                 scale_and_round(pos.x, pos.y),
507 505
                 int(round(bubble.radius * MAP_SIZE)),
508 506
                 1)
509  
-        for explosion in m.explosions:
  507
+        for explosion in self.world.explosions:
510 508
             pos = explosion.pos
511 509
             pygame.draw.circle(
512 510
                 self.screen,
@@ -514,7 +512,7 @@ def render_game_world(self):
514 512
                 scale_and_round(pos.x, pos.y),
515 513
                 int(round(explosion.radius * MAP_SIZE)),
516 514
                 1)
517  
-        for i in m.powerups:
  515
+        for i in self.world.powerups:
518 516
             self.render_powerup(i)
519 517
 
520 518
         self.screen.set_clip(None)

0 notes on commit 2657bd7

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