Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Created a render() method for Powerup.

  • Loading branch information...
commit deeff180566bfededaa8db445bf3c485e2a7f157 1 parent 798ece1
Al Sweigart authored August 10, 2012

Showing 1 changed file with 35 additions and 34 deletions. Show diff stats Hide diff stats

  1. 69  square-shooter/square-shooter_makeover.py
69  square-shooter/square-shooter_makeover.py
@@ -146,7 +146,7 @@ def spawn(self):
146 146
 
147 147
         if self.kind == "small":
148 148
             # Small Bubbles do not create new Bubbles, but might create Powerups.
149  
-            if random.random() < 0.25:
  149
+            if random.random() < 10.25:
150 150
                 spawned_powerups.append(Powerup(self.pos))
151 151
         else:
152 152
             # Medium and Big Bubbles create new Bubble objects of the next smaller size. They don't create Powerups.
@@ -176,6 +176,38 @@ def __init__(self, pos):
176 176
         self.kind = random.choice(("shield", "bullet", "freeze"))
177 177
         self.age = 0
178 178
 
  179
+    def render(self, surface):
  180
+        scaled_x, scaled_y = scale_and_round(self.pos.x, self.pos.y)
  181
+        scaled_r = int(round(self.radius * MAP_SIZE))
  182
+        if self.kind == "shield":
  183
+            bbox = pygame.draw.circle(
  184
+                surface,
  185
+                WHITE,
  186
+                (scaled_x, scaled_y),
  187
+                scaled_r,
  188
+                1)
  189
+            pygame.draw.rect(surface, WHITE, bbox, 1)
  190
+        elif self.kind == "bullet":
  191
+            pygame.draw.circle(
  192
+                surface,
  193
+                WHITE,
  194
+                (scaled_x, scaled_y),
  195
+                int(round(scaled_r * 0.3)),
  196
+                1)
  197
+            bbox = pygame.Rect(0, 0, scaled_r * 2, scaled_r * 2)
  198
+            bbox.center = (scaled_x, scaled_y)
  199
+            pygame.draw.rect(surface, WHITE, bbox, 1)
  200
+        elif self.kind == "freeze":
  201
+            bbox = pygame.Rect(0, 0, scaled_r * 2, scaled_r * 2)
  202
+            bbox.center = (scaled_x, scaled_y)
  203
+            pygame.draw.rect(surface, WHITE, bbox, 1)
  204
+            bbox.inflate_ip(-scaled_r, -scaled_r)
  205
+            pygame.draw.rect(surface, WHITE, bbox, 1)
  206
+            bbox.inflate_ip(-scaled_r * 0.5, -scaled_r * 0.5)
  207
+            pygame.draw.rect(surface, WHITE, bbox, 1)
  208
+        else:
  209
+            raise "Bad power-up kind: " + self.kind
  210
+
179 211
 
180 212
 class Ship(ObjectOnMap):
181 213
     def __init__(self):
@@ -546,42 +578,11 @@ def render_game_world(self):
546 578
         for explosion in self.world.explosions:
547 579
             explosion.render(self.screen)
548 580
 
549  
-        for i in self.world.powerups:
550  
-            self.render_powerup(i)
  581
+        for powerup in self.world.powerups:
  582
+            powerup.render(self.screen)
551 583
 
552 584
         self.screen.set_clip(None)
553 585
 
554  
-    def render_powerup(self, powerup):
555  
-        pos = powerup.pos
556  
-        radius = powerup.radius * MAP_SIZE
557  
-        if powerup.kind    == "shield":
558  
-            bbox = pygame.draw.circle(
559  
-                self.screen,
560  
-                WHITE,
561  
-                scale_and_round(pos.x, pos.y),
562  
-                int(round(radius)),
563  
-                1)
564  
-            pygame.draw.rect(self.screen, WHITE, bbox, 1)
565  
-        elif powerup.kind == "bullet":
566  
-            pygame.draw.circle(
567  
-                self.screen,
568  
-                WHITE,
569  
-                scale_and_round(pos.x, pos.y),
570  
-                int(round(radius * 0.3)),
571  
-                1)
572  
-            bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
573  
-            bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
574  
-            pygame.draw.rect(self.screen, WHITE, bbox, 1)
575  
-        elif powerup.kind == "freeze":
576  
-            bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
577  
-            bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
578  
-            pygame.draw.rect(self.screen, WHITE, bbox, 1)
579  
-            bbox.inflate_ip(-radius, -radius)
580  
-            pygame.draw.rect(self.screen, WHITE, bbox, 1)
581  
-            bbox.inflate_ip(-radius * 0.5, -radius * 0.5)
582  
-            pygame.draw.rect(self.screen, WHITE, bbox, 1)
583  
-        else:
584  
-            raise "Bad power-up kind: " + powerup.kind
585 586
 
586 587
     def render_pause_text(self):
587 588
         pause_text = self.msg_font.render("Game paused", False, GREEN)

0 notes on commit deeff18

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