Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added the Shotgun powerup

  • Loading branch information...
commit 35a149cf3f01193647ba2c63340978587b6ac8f1 1 parent 0d58313
Al Sweigart authored

Showing 1 changed file with 42 additions and 17 deletions. Show diff stats Hide diff stats

  1. 59  square-shooter/square-shooter_makeover.py
59  square-shooter/square-shooter_makeover.py
@@ -175,7 +175,7 @@ class Powerup(ObjectOnMap):
175 175
     def __init__(self, pos):
176 176
         super(Powerup, self).__init__(0.03) # all Powerups are the same size.
177 177
         self.pos.copy(pos)
178  
-        self.kind = random.choice(("shield", "bullet", "freeze"))
  178
+        self.kind = random.choice(("shield", "bullet", "freeze", "shotgun"))
179 179
         self.age = 0
180 180
 
181 181
     def render(self, surface):
@@ -207,6 +207,11 @@ def render(self, surface):
207 207
             pygame.draw.rect(surface, WHITE, bbox, 1)
208 208
             bbox.inflate_ip(-scaled_r * 0.5, -scaled_r * 0.5)
209 209
             pygame.draw.rect(surface, WHITE, bbox, 1)
  210
+        elif self.kind == "shotgun":
  211
+            bbox = pygame.Rect(0, 0, scaled_r * 2, scaled_r * 2)
  212
+            bbox.center = (scaled_x, scaled_y)
  213
+            pygame.draw.rect(surface, WHITE, bbox, 1)
  214
+            pygame.draw.line(surface, WHITE, bbox.midleft, bbox.midright, 1)
210 215
         else:
211 216
             raise "Bad power-up kind: " + self.kind
212 217
 
@@ -217,6 +222,7 @@ def __init__(self):
217 222
         self._shield_timer = 0
218 223
         self._super_bullet_timer = 0
219 224
         self._freeze_timer = 0
  225
+        self._shotgun_timer = 0
220 226
         self.accel_x = 0 # acceleration rate of the ship
221 227
         self.accel_y = 0
222 228
 
@@ -245,6 +251,7 @@ def update(self, delta_t):
245 251
         if self.has_shield():        self._shield_timer       -= delta_t
246 252
         if self.has_super_bullets(): self._super_bullet_timer -= delta_t
247 253
         if self.has_freeze():        self._freeze_timer       -= delta_t
  254
+        if self.has_shotgun():       self._shotgun_timer      -= delta_t
248 255
 
249 256
         super(Ship, self).update(delta_t)
250 257
 
@@ -272,24 +279,41 @@ def has_freeze(self):
272 279
         """Returns True if this ship currently has a freeze powerup, False if it does not have a shield."""
273 280
         return self._freeze_timer > 0
274 281
 
  282
+    def add_shotgun(self, secs=6):
  283
+        """Extends the time on the ship's shotgun powerup by secs seconds."""
  284
+        self._shotgun_timer += secs
  285
+
  286
+    def has_shotgun(self):
  287
+        """Returns True if this ship currently has a shotgun powerup, False if it does not have a shield."""
  288
+        return self._shotgun_timer > 0
  289
+
275 290
     def shoot_at(self, x, y):
276 291
         """Returns a list of bullet objects that were created by the Ship."""
277 292
         x -= self.pos.x;
278 293
         y -= self.pos.y;
279 294
 
280  
-        b = Bullet()
281  
-        b.pos.copy(self.pos);
282  
-        b.speed.x = x * 3
283  
-        b.speed.y = y * 3
  295
+        bullets = []
  296
+        for i in range(5):
  297
+            b = Bullet()
  298
+            b.pos.copy(self.pos);
  299
+            b.speed.x = x * 3
  300
+            b.speed.y = y * 3
  301
+
  302
+            # Help out the poor sods who click on their
  303
+            # own ship and get stuck with a non-moving
  304
+            # bullet. (2009-11-14)
  305
+            if abs(x) < 0.1 and abs(y) < 0.1:
  306
+                b.speed.x *= 30
  307
+                b.speed.y *= 30
  308
+
  309
+            if not self.has_shotgun():
  310
+                return [b] # just return the one bullet
284 311
 
285  
-        # Help out the poor sods who click on their
286  
-        # own ship and get stuck with a non-moving
287  
-        # bullet. (2009-11-14)
288  
-        if abs(x) < 0.1 and abs(y) < 0.1:
289  
-            b.speed.x *= 30
290  
-            b.speed.y *= 30
  312
+            b.speed.x += random.uniform(-0.15, 0.15)
  313
+            b.speed.y += random.uniform(-0.15, 0.15)
291 314
 
292  
-        return [b]
  315
+            bullets.append(b)
  316
+        return bullets
293 317
 
294 318
     def render(self, surface):
295 319
         bbox = pygame.draw.circle(
@@ -481,10 +505,11 @@ def mark_score(self, bubble):
481 505
             self.high_score = self.score
482 506
 
483 507
     def apply_powerup(self, powerup):
484  
-        #                  function to call
485  
-        kinds = {'shield': self.ship.add_shield,
486  
-                 'bullet': self.ship.add_super_bullets,
487  
-                 'freeze': self.ship.add_freeze}
  508
+        #                   function to call
  509
+        kinds = {'shield':  self.ship.add_shield,
  510
+                 'bullet':  self.ship.add_super_bullets,
  511
+                 'freeze':  self.ship.add_freeze,
  512
+                 'shotgun': self.ship.add_shotgun}
488 513
         kinds[powerup.kind]()
489 514
 
490 515
         self.score += self.level * 10
@@ -634,7 +659,7 @@ def render_pause_text(self):
634 659
         # on mouse down, fire a bullet and start the thruster of the ship
635 660
         if (world.level > 0) and (world.ship != None) and (not renderer.game_paused):
636 661
             x, y = ev.pos
637  
-            if len(world.bullets) < 5:
  662
+            if len(world.bullets) < 6:
638 663
                 world.bullets.extend(world.ship.shoot_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT)))
639 664
             world.ship.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
640 665
     elif ev.type == pygame.MOUSEBUTTONUP:

0 notes on commit 35a149c

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