Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Renamed Bubble2D's position member to pos.

  • Loading branch information...
commit 9c7b390e2b3d5d1275ad3e66ddec9264edd3a95b 1 parent 7cfc5f8
Al Sweigart authored August 07, 2012

Showing 1 changed file with 27 additions and 29 deletions. Show diff stats Hide diff stats

  1. 56  square-shooter/square-shooter_makeover.py
56  square-shooter/square-shooter_makeover.py
@@ -78,32 +78,30 @@ class Bubble2D:
78 78
     """Represents a circular object on the game map with position, radius, and velocity."""
79 79
 
80 80
     def __init__(self, radius):
81  
-        self.position = Vector2D(0, 0)
  81
+        self.pos = Vector2D(0, 0)
82 82
         self.radius = radius;
83 83
         self.speed = Vector2D(0, 0)
84 84
 
85 85
     def update(self, delta_t):
86 86
         """Update the bubble's position as though delta_t seconds have passed."""
87  
-        self.position.x += self.speed.x * delta_t
88  
-        self.position.y += self.speed.y * delta_t
  87
+        self.pos.x += self.speed.x * delta_t
  88
+        self.pos.y += self.speed.y * delta_t
89 89
 
90 90
     def wrap_around(self):
91 91
         """Change the position of the bubble to toroidally "wrap around" if it goes off one edge of the map."""
92  
-        pos = self.position
93  
-        if pos.x < 0: pos.x = 1
94  
-        if pos.y < 0: pos.y = 1
95  
-        if pos.x > 1: pos.x = 0
96  
-        if pos.y > 1: pos.y = 0
  92
+        if self.pos.x < 0: self.pos.x = 1
  93
+        if self.pos.y < 0: self.pos.y = 1
  94
+        if self.pos.x > 1: self.pos.x = 0
  95
+        if self.pos.y > 1: self.pos.y = 0
97 96
 
98 97
     def is_out(self):
99 98
         """Returns True if the center of the bubble is outside the game map, False if it is on the map."""
100  
-        pos = self.position;
101  
-        return pos.x < 0 or pos.y < 0 or pos.x > 1 or pos.y > 1
  99
+        return self.pos.x < 0 or self.pos.y < 0 or self.pos.x > 1 or self.pos.y > 1
102 100
 
103 101
     def collides_with(self, bubble):
104 102
         """Returns True if this bubble is intersecting with the Bubble2D object passed in for the bubble parameter."""
105  
-        a = abs(self.position.x - bubble.position.x)
106  
-        b = abs(self.position.y - bubble.position.y)
  103
+        a = abs(self.pos.x - bubble.pos.x)
  104
+        b = abs(self.pos.y - bubble.pos.y)
107 105
         distance = math.sqrt(a * a + b * b)
108 106
         return distance < (self.radius + bubble.radius)
109 107
 
@@ -125,7 +123,7 @@ def make_bubble(kind):
125 123
         speed = 0.25
126 124
 
127 125
     new_bubble = Bubble2D(size)
128  
-    new_bubble.position = Vector2D(
  126
+    new_bubble.pos = Vector2D(
129 127
         random_position(),
130 128
         random_position())
131 129
     new_bubble.speed = Vector2D(
@@ -162,7 +160,7 @@ def init_level(self, level):
162 160
         if (level > self.max_level): self.max_level = level
163 161
         if self.ship == None:
164 162
             self.ship = Bubble2D(1.0 / 25)
165  
-        self.ship.position = Vector2D(0.5, 0.5)
  163
+        self.ship.pos = Vector2D(0.5, 0.5)
166 164
         self.ship.speed = Vector2D(0, 0)
167 165
         self.bullet = None;
168 166
         self.move_timer = 0
@@ -223,7 +221,7 @@ def update(self, delta_t):
223 221
                 self.death_timer -= delta_t
224 222
             elif self.lives > 0:
225 223
                 self.ship = Bubble2D(1.0 / 25)
226  
-                self.ship.position = Vector2D(0.5, 0.5)
  224
+                self.ship.pos = Vector2D(0.5, 0.5)
227 225
                 self.ship_shield_timer = 6;
228 226
             else:
229 227
                 self.level = 0 # Game over
@@ -280,20 +278,20 @@ def spawn_bubbles(self, parent):
280 278
             elif parent.kind == "medium":
281 279
                 new_type = "small"
282 280
             b = make_bubble(new_type)
283  
-            b.position.copy(parent.position)
  281
+            b.pos.copy(parent.pos)
284 282
             self.bubbles.append(b)
285 283
             b = make_bubble(new_type)
286  
-            b.position.copy(parent.position)
  284
+            b.pos.copy(parent.pos)
287 285
             self.bubbles.append(b)
288 286
 
289 287
     def spawn_explosion(self, bubble):
290 288
         explosion = Bubble2D(0)
291  
-        explosion.position.copy(bubble.position)
  289
+        explosion.pos.copy(bubble.pos)
292 290
         self.explosions.append(explosion)
293 291
 
294 292
     def spawn_powerup(self, bubble):
295 293
         powerup = Bubble2D(0.03)
296  
-        powerup.position.copy(bubble.position)
  294
+        powerup.pos.copy(bubble.pos)
297 295
         powerup.kind = random.choice(("shield", "bullet", "freeze"))
298 296
         powerup.age = 0
299 297
         self.powerups.append(powerup)
@@ -327,11 +325,11 @@ def shoot_at(self, x, y):
327 325
         if self.bullet != None or self.ship == None:
328 326
             return
329 327
 
330  
-        x -= self.ship.position.x;
331  
-        y -= self.ship.position.y;
  328
+        x -= self.ship.pos.x;
  329
+        y -= self.ship.pos.y;
332 330
 
333 331
         b = Bubble2D(0.01)
334  
-        b.position.copy(self.ship.position);
  332
+        b.pos.copy(self.ship.pos);
335 333
         b.speed.x = x * 3
336 334
         b.speed.y = y * 3
337 335
 
@@ -350,8 +348,8 @@ def thrust_at(self, x, y):
350 348
         if self.ship == None:
351 349
             return
352 350
 
353  
-        x -= self.ship.position.x;
354  
-        y -= self.ship.position.y;
  351
+        x -= self.ship.pos.x;
  352
+        y -= self.ship.pos.y;
355 353
 
356 354
         self.accel_x += x * 0.03;
357 355
         self.accel_y += y * 0.03;
@@ -456,7 +454,7 @@ def render_game_world(self):
456 454
             if not hasattr(bubble, "color"):
457 455
                 bubble.color = random.choice(
458 456
                     self.bubble_colors)
459  
-            pos = bubble.position
  457
+            pos = bubble.pos
460 458
             pygame.draw.circle(
461 459
                 self.screen,
462 460
                 bubble.color,
@@ -464,7 +462,7 @@ def render_game_world(self):
464 462
                 int(round(bubble.radius * MAP_SIZE)),
465 463
                 1)
466 464
         for explosion in m.explosions:
467  
-            pos = explosion.position
  465
+            pos = explosion.pos
468 466
             pygame.draw.circle(
469 467
                 self.screen,
470 468
                 RED,
@@ -478,7 +476,7 @@ def render_game_world(self):
478 476
 
479 477
     def render_ship(self):
480 478
         ship = self.model.ship
481  
-        pos = ship.position
  479
+        pos = ship.pos
482 480
         bbox = pygame.draw.circle(
483 481
             self.screen,
484 482
             SILVER,
@@ -495,7 +493,7 @@ def render_ship(self):
495 493
 
496 494
     def render_bullet(self):
497 495
         bullet = self.model.bullet
498  
-        pos = bullet.position
  496
+        pos = bullet.pos
499 497
         bbox = pygame.draw.circle(
500 498
             self.screen,
501 499
             RED,
@@ -505,7 +503,7 @@ def render_bullet(self):
505 503
             pygame.draw.rect(self.screen, RED, bbox, 1)
506 504
 
507 505
     def render_powerup(self, powerup):
508  
-        pos = powerup.position
  506
+        pos = powerup.pos
509 507
         radius = powerup.radius * MAP_SIZE
510 508
         if powerup.kind    == "shield":
511 509
             bbox = pygame.draw.circle(

0 notes on commit 9c7b390

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