Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

#!/usr/bin/python

  • Loading branch information...
commit f30ae547311e944bbb6ed8576ff4782c03f0d9b5 1 parent 113c91d
Al Sweigart authored August 08, 2012

Showing 1 changed file with 54 additions and 53 deletions. Show diff stats Hide diff stats

  1. 107  square-shooter/square-shooter_makeover.py
107  square-shooter/square-shooter_makeover.py
@@ -74,7 +74,7 @@ def copy(self, vector):
74 74
         self.y = vector.y
75 75
 
76 76
 
77  
-class ObjectOnMap:
  77
+class ObjectOnMap(object):
78 78
     """Represents a circular object on the game map with position, radius, and velocity."""
79 79
 
80 80
     def __init__(self, radius):
@@ -110,31 +110,60 @@ def collides_with(self, other):
110 110
         return distance < (self.radius + other.radius)
111 111
 
112 112
 
113  
-def random_position():
114  
-    """Returns a random float value that will be near the edge of the map"""
115  
-    if random.randint(0, 1) == 0:
116  
-        return random.uniform(0.0, 0.25)
117  
-    else:
118  
-        return random.uniform(0.75, 1.0)
119  
-
120  
-
121  
-def bubble_factory(kind):
  113
+class Bubble(ObjectOnMap):
122 114
     #                   (size, speed)
123 115
     kinds  = {'big':    (0.1,   0.1),
124 116
               'medium': (0.075, 0.15),
125 117
               'small':  (0.05,  0.25)}
126 118
 
127  
-    size, speed = kinds[kind]
  119
+    colors = [pygame.Color('#ffffcc'),
  120
+              pygame.Color('#ffccff'),
  121
+              pygame.Color('#ccffff'),
  122
+              pygame.Color('#ffdddd'),
  123
+              pygame.Color('#ddffdd'),
  124
+              pygame.Color('#ddddff')]
  125
+
  126
+    def __init__(self, kind):
  127
+        size, speed = Bubble.kinds[kind]
  128
+        super(Bubble ,self).__init__(size)
  129
+
  130
+        self.pos = Vector2D(
  131
+            random.random(),
  132
+            random.random())
  133
+        self.speed = Vector2D(
  134
+            random.uniform(-speed, speed),
  135
+            random.uniform(-speed, speed))
  136
+        self.kind = kind
  137
+        self.color = random.choice(Bubble.colors)
  138
+
  139
+    def spawn(self):
  140
+        spawned_bubbles = []
  141
+        spawned_powerups = []
  142
+
  143
+        if self.kind == "small":
  144
+            if random.random() < 0.25:
  145
+                spawned_powerups.append(Powerup(self.pos))
  146
+        else:
  147
+            print(self.kind)
  148
+            if self.kind == "medium":
  149
+                new_kind = "small"
  150
+            elif self.kind == "big":
  151
+                new_kind = "medium"
  152
+
  153
+            for i in range(2):
  154
+                spawned_bubbles.append(Bubble(new_kind))
  155
+                spawned_bubbles[-1].pos.copy(self.pos)
  156
+
  157
+        return (spawned_bubbles, spawned_powerups)
  158
+
  159
+
  160
+class Powerup(ObjectOnMap):
  161
+    def __init__(self, pos):
  162
+        super(Powerup, self).__init__(0.03)
  163
+        self.pos.copy(pos)
  164
+        self.kind = random.choice(("shield", "bullet", "freeze"))
  165
+        self.age = 0
128 166
 
129  
-    new_bubble = ObjectOnMap(size)
130  
-    new_bubble.pos = Vector2D(
131  
-        random_position(),
132  
-        random_position())
133  
-    new_bubble.speed = Vector2D(
134  
-        random.uniform(-speed, speed),
135  
-        random.uniform(-speed, speed))
136  
-    new_bubble.kind = kind
137  
-    return new_bubble
138 167
 
139 168
 class GameWorld:
140 169
     bubbles = []
@@ -179,7 +208,7 @@ def init_level(self, level):
179 208
         del self.explosions[:]
180 209
         del self.powerups[:]
181 210
         for i in range(level):
182  
-            self.bubbles.append(bubble_factory("big"))
  211
+            self.bubbles.append(Bubble("big"))
183 212
 
184 213
     def update(self, delta_t):
185 214
         self.handle_collisions(delta_t)
@@ -246,7 +275,9 @@ def handle_collisions(self, delta_t):
246 275
                     # Push it along or it will just
247 276
                     # destroy the newly formed bubbles.
248 277
                     self.bullet.update(delta_t * 5)
249  
-                self.spawn_bubbles(b)
  278
+                spawned_bubbles, spawned_powerups = b.spawn()
  279
+                self.bubbles.extend(spawned_bubbles)
  280
+                self.powerups.extend(spawned_powerups)
250 281
                 self.spawn_explosion(b)
251 282
                 self.mark_score(b)
252 283
                 if len(self.bubbles) == 0:
@@ -270,34 +301,11 @@ def handle_collisions(self, delta_t):
270 301
                 self.apply_powerup(p)
271 302
                 self.powerups.remove(p)
272 303
 
273  
-    def spawn_bubbles(self, parent):
274  
-        if parent.kind == "small":
275  
-            if random.random() < 0.25:
276  
-                self.spawn_powerup(parent)
277  
-        else:
278  
-            if parent.kind == "big":
279  
-                new_type = "medium"
280  
-            elif parent.kind == "medium":
281  
-                new_type = "small"
282  
-            b = bubble_factory(new_type)
283  
-            b.pos.copy(parent.pos)
284  
-            self.bubbles.append(b)
285  
-            b = bubble_factory(new_type)
286  
-            b.pos.copy(parent.pos)
287  
-            self.bubbles.append(b)
288  
-
289 304
     def spawn_explosion(self, bubble):
290 305
         explosion = ObjectOnMap(0)
291 306
         explosion.pos.copy(bubble.pos)
292 307
         self.explosions.append(explosion)
293 308
 
294  
-    def spawn_powerup(self, bubble):
295  
-        powerup = ObjectOnMap(0.03)
296  
-        powerup.pos.copy(bubble.pos)
297  
-        powerup.kind = random.choice(("shield", "bullet", "freeze"))
298  
-        powerup.age = 0
299  
-        self.powerups.append(powerup)
300  
-
301 309
     def mark_score(self, bubble):
302 310
         if bubble.kind == "small":
303 311
             self.score += 5
@@ -374,11 +382,7 @@ def __init__(self, model, screen):
374 382
         self.msg_font = pygame.font.SysFont(
375 383
             font_name, self.height / 20)
376 384
 
377  
-        self.bubble_colors = ["#ffffcc", "#ffccff", "#ccffff",
378  
-            "#ffdddd", "#ddffdd", "#ddddff"]
379  
-        for i in range(6):
380  
-            self.bubble_colors[i] = pygame.Color(
381  
-                self.bubble_colors[i])
  385
+
382 386
 
383 387
         self.game_paused = False
384 388
 
@@ -453,9 +457,6 @@ def render_game_world(self):
453 457
         if m.bullet != None: self.render_bullet()
454 458
 
455 459
         for bubble in m.bubbles:
456  
-            if not hasattr(bubble, "color"):
457  
-                bubble.color = random.choice(
458  
-                    self.bubble_colors)
459 460
             pos = bubble.pos
460 461
             pygame.draw.circle(
461 462
                 self.screen,

0 notes on commit f30ae54

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