Commits

Richard Jones committed 039df47

simplification and commenting

Comments (0)

Files changed (1)

         self.gun_cooldown = 0
 
     def update(self, dt, game):
-        # handle the player movement left/right keys
+        # handle the player movement keys
         key = pygame.key.get_pressed()
-        self.rect.x += game.view_dx
-        if key[pygame.K_LEFT]:
-            self.rect.x -= int(200 * dt)
-        if key[pygame.K_RIGHT]:
-            self.rect.x += int(200 * dt)
-        if key[pygame.K_UP]:
-            self.rect.y -= int(200 * dt)
-        if key[pygame.K_DOWN]:
-            self.rect.y += int(200 * dt)
+        dx = 100
+        dy = 0
+        if key[pygame.K_LEFT]: dx -= 200
+        if key[pygame.K_RIGHT]: dx += 200
+        if key[pygame.K_UP]: dy -= 200
+        if key[pygame.K_DOWN]: dy += 200
+        self.rect.x += int(dx * dt)
+        self.rect.y += int(dy * dt)
 
         # keep the player from moving off-screen
         viewport = game.tilemap.viewport
         self.shoot = pygame.mixer.Sound('shoot.wav')
         self.explosion = pygame.mixer.Sound('explosion.wav')
 
+        # flag to allow us to detect when the player completes the level
         self.won = False
+
+        # start the view focus at the player spawn point
         view_x = start_cell.px
         while 1:
             # limit updates to 30 times per second and determine how much time
                 if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                     return
 
-            # move the view
+            # move the view at a fixed rate; round to int so we don't introduce
+            # any floating-point accumulation problems
+            view_x += int(100 * dt)
             self.tilemap.set_focus(view_x, start_cell.py)
-            self.view_dx = int(100 * dt)
-            view_x += self.view_dx
 
             # update the tilemap and everything in it passing the elapsed time
             # since the last update (in seconds) and this Game object