Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Created a Ship class and moved code into it.

  • Loading branch information...
commit 7170b5af834cb48796badb887208ea83813fe274 1 parent a6f8972
Al Sweigart authored August 08, 2012

Showing 1 changed file with 47 additions and 38 deletions. Show diff stats Hide diff stats

  1. 85  square-shooter/square-shooter_makeover.py
85  square-shooter/square-shooter_makeover.py
@@ -48,6 +48,7 @@
48 48
 MAP_HALF_HEIGHT          = int(MAP_HEIGHT / 2.0)
49 49
 MAP_THREE_QUARTER_HEIGHT = int(3 * MAP_HEIGHT / 4.0)
50 50
 
  51
+DECELERATION = 0.99
51 52
 
52 53
 def scale_and_round(x, y):
53 54
     """Returns x and y coordinates from 0.0 to 1.0 scaled to 0 to MAP_WIDTH or MAP_HEIGHT."""
@@ -78,12 +79,12 @@ class ObjectOnMap(object):
78 79
     """Represents a circular object on the game map with position, radius, and velocity."""
79 80
 
80 81
     def __init__(self, radius):
81  
-        self.pos = Vector2D(0, 0)
  82
+        self.pos = Vector2D(0.5, 0.5)
82 83
         self.radius = radius;
83 84
         self.speed = Vector2D(0, 0)
84 85
 
85 86
     def update(self, delta_t):
86  
-        """Update the bubble's position as though delta_t seconds have passed."""
  87
+        """Update the object's position as though delta_t seconds have passed."""
87 88
         self.pos.x += self.speed.x * delta_t
88 89
         self.pos.y += self.speed.y * delta_t
89 90
 
@@ -167,6 +168,35 @@ def __init__(self, pos):
167 168
         self.age = 0
168 169
 
169 170
 
  171
+class Ship(ObjectOnMap):
  172
+    def __init__(self):
  173
+        super(Ship, self).__init__(0.04) # all Ships are the same size.
  174
+        self.shield_timer = 0
  175
+        self.accel_x = 0 # acceleration rate of the ship
  176
+        self.accel_y = 0
  177
+
  178
+    def thrust_at(self, x, y):
  179
+        """Increase acceleration of the ship in the direction of x, y.
  180
+        The further away x, y is from the current position of the ship, the larger the acceleration increase."""
  181
+        x -= self.pos.x;
  182
+        y -= self.pos.y;
  183
+
  184
+        self.accel_x += x * 0.03;
  185
+        self.accel_y += y * 0.03;
  186
+
  187
+    def stop_thrust(self):
  188
+        """Immediately stop all ship acceleration. Note that this doesn't stop the velocity of the ship, just the acceleration."""
  189
+        self.accel_x = 0
  190
+        self.accel_y = 0
  191
+
  192
+    def update(self, delta_t):
  193
+        """Update the ship's position as though delta_t seconds have passed."""
  194
+        self.speed.x += self.accel_x
  195
+        self.speed.y += self.accel_y
  196
+        self.speed.x *= DECELERATION
  197
+        self.speed.y *= DECELERATION
  198
+        super(Ship, self).update(delta_t)
  199
+
170 200
 class GameWorld:
171 201
     bubbles = []
172 202
     explosions = []
@@ -177,10 +207,8 @@ class GameWorld:
177 207
     accel_x = 0
178 208
     accel_y = 0
179 209
 
180  
-    move_timer = 0
181 210
     death_timer = 0
182 211
     finish_timer = 0
183  
-    ship_shield_timer = 0
184 212
     bullet_shield_timer = 0
185 213
     freeze_timer = 0
186 214
 
@@ -194,15 +222,12 @@ def init_level(self, level):
194 222
         self.level = level
195 223
         if (level > self.max_level): self.max_level = level
196 224
         if self.ship == None:
197  
-            self.ship = ObjectOnMap(1.0 / 25)
198  
-        self.ship.pos = Vector2D(0.5, 0.5)
199  
-        self.ship.speed = Vector2D(0, 0)
  225
+            self.ship = Ship()
  226
+        self.ship.shield_timer += 6 # add 6 seconds of shield at the start of a level
200 227
         self.bullet = None;
201  
-        self.move_timer = 0
202 228
         self.death_timer = 0
203 229
         self.finish_timer = 0
204 230
 
205  
-        self.ship_shield_timer = 6;
206 231
         self.bullet_shield_timer = 0;
207 232
         self.freeze_timer = 0;
208 233
 
@@ -226,8 +251,8 @@ def update(self, delta_t):
226 251
                 self.powerups.pop(0)
227 252
         for i in self.powerups:
228 253
             i.age += delta_t
229  
-        if self.ship_shield_timer > 0:
230  
-            self.ship_shield_timer -= delta_t
  254
+        if (self.ship != None) and (self.ship.shield_timer > 0):
  255
+            self.ship.shield_timer -= delta_t
231 256
         if self.bullet_shield_timer > 0:
232 257
             self.bullet_shield_timer -= delta_t
233 258
         if self.freeze_timer > 0:
@@ -254,17 +279,12 @@ def update(self, delta_t):
254 279
             if self.death_timer > 0:
255 280
                 self.death_timer -= delta_t
256 281
             elif self.lives > 0:
257  
-                self.ship = ObjectOnMap(1.0 / 25)
258  
-                self.ship.pos = Vector2D(0.5, 0.5)
259  
-                self.ship_shield_timer = 6;
  282
+                self.ship = Ship()
  283
+                self.ship.shield_timer += 6 # add 6 seconds of shield at the start of a life
260 284
             else:
261 285
                 self.level = 0 # Game over
262 286
             return
263 287
 
264  
-        self.ship.speed.x += self.accel_x
265  
-        self.ship.speed.y += self.accel_y
266  
-        self.ship.speed.x *= 0.99
267  
-        self.ship.speed.y *= 0.99
268 288
         self.ship.update(delta_t)
269 289
 
270 290
     def handle_collisions(self, delta_t):
@@ -288,7 +308,7 @@ def handle_collisions(self, delta_t):
288 308
             elif self.ship != None:
289 309
                 if not b.collides_with(self.ship):
290 310
                     continue
291  
-                if self.ship_shield_timer > 0:
  311
+                if self.ship.shield_timer > 0:
292 312
                     continue
293 313
                 self.spawn_explosion(self.ship)
294 314
                 self.ship = None
@@ -321,7 +341,7 @@ def mark_score(self, bubble):
321 341
 
322 342
     def apply_powerup(self, powerup):
323 343
         if powerup.kind == "shield":
324  
-            self.ship_shield_timer += 6
  344
+            self.ship.shield_timer += 6
325 345
         elif powerup.kind == "bullet":
326 346
             self.bullet_shield_timer += 6
327 347
         elif powerup.kind == "freeze":
@@ -356,19 +376,6 @@ def shoot_at(self, x, y):
356 376
 
357 377
         self.bullet = b
358 378
 
359  
-    def thrust_at(self, x, y):
360  
-        if self.ship == None:
361  
-            return
362  
-
363  
-        x -= self.ship.pos.x;
364  
-        y -= self.ship.pos.y;
365  
-
366  
-        self.accel_x += x * 0.03;
367  
-        self.accel_y += y * 0.03;
368  
-
369  
-    def stop_thrust(self):
370  
-        self.accel_x = 0
371  
-        self.accel_y = 0
372 379
 
373 380
 class GameScreen:
374 381
     def __init__(self, model, screen):
@@ -493,7 +500,7 @@ def render_ship(self):
493 500
             scale_and_round(pos.x, pos.y),
494 501
             int(round(ship.radius * 0.5 * MAP_SIZE)),
495 502
             1)
496  
-        if self.model.ship_shield_timer > 0:
  503
+        if self.model.ship.shield_timer > 0:
497 504
             pygame.draw.rect(self.screen, SILVER, bbox, 1)
498 505
 
499 506
     def render_bullet(self):
@@ -579,13 +586,15 @@ def render_pause_text(self):
579 586
             else:
580 587
                 renderer.game_paused = not renderer.game_paused
581 588
     elif ev.type == pygame.MOUSEBUTTONDOWN:
582  
-        if model.level > 0 and not renderer.game_paused:
  589
+        # on mouse down, fire a bullet and start the thruster of the ship
  590
+        if (model.level > 0) and (model.ship != None) and (not renderer.game_paused):
583 591
             x, y = ev.pos
584 592
             model.shoot_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
585  
-            model.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
  593
+            model.ship.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
586 594
     elif ev.type == pygame.MOUSEBUTTONUP:
587  
-        if model.level > 0:
588  
-            model.stop_thrust()
  595
+        # on mouse up, stop accelerating the ship
  596
+        if (model.level > 0) and (model.ship != None):
  597
+            model.ship.stop_thrust()
589 598
 
590 599
     # Simulations need the time in seconds, dammit!
591 600
     if model.level > 0 and not renderer.game_paused:

0 notes on commit 7170b5a

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