summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2017-11-05 16:51:48 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2017-11-05 16:51:48 +0700
commit96a82a67a0173c6c448897c05ddcb4d139329b0f (patch)
tree8e4df7ae9281202f2fd67d07dec45426dfeb339b
parentb1f0c052f9dd009a7a86806cd08867e266451aae (diff)
downloadbrutalmaze-96a82a67a0173c6c448897c05ddcb4d139329b0f.tar.gz
Implement acceleration in hero movement 0.0.3
-rw-r--r--brutalmaze/__init__.py4
-rw-r--r--brutalmaze/main.py4
-rw-r--r--brutalmaze/maze.py45
-rwxr-xr-xsetup.py2
4 files changed, 38 insertions, 17 deletions
diff --git a/brutalmaze/__init__.py b/brutalmaze/__init__.py
index a2a2b70..5111000 100644
--- a/brutalmaze/__init__.py
+++ b/brutalmaze/__init__.py
@@ -1,5 +1,5 @@
-"""Brutal Maze is a research hash and slash game with polyart graphic
-and Tango color palette.
+"""Brutal Maze is a research hash and slash game with fast-paced action
+and a minimalist art style.
 """
 
 from .main import main
diff --git a/brutalmaze/main.py b/brutalmaze/main.py
index 6690b51..bc78962 100644
--- a/brutalmaze/main.py
+++ b/brutalmaze/main.py
@@ -50,8 +50,8 @@ def main():
         if not maze.hero.dead:
             keys = pygame.key.get_pressed()
             buttons = pygame.mouse.get_pressed()
-            maze.right = some(keys, LEFT) - some(keys, RIGHT)
-            maze.down = some(keys, UP) - some(keys, DOWN)
+            maze.move(some(keys, LEFT) - some(keys, RIGHT),
+                      some(keys, UP) - some(keys, DOWN), fps)
             maze.hero.slashing = keys[K_RETURN] or buttons[2]
             maze.hero.firing = buttons[0]
 
diff --git a/brutalmaze/maze.py b/brutalmaze/maze.py
index 8fd8645..4012e2c 100644
--- a/brutalmaze/maze.py
+++ b/brutalmaze/maze.py
@@ -66,7 +66,7 @@ class Maze:
         paused (bool): flag indicates if the game is paused
         score (float): current score
         map (deque of deque): map of grids representing objects on the maze
-        down, right (int): direction the maze moving
+        vx, vy (float): velocity of the maze movement (in pixels per frame)
         rotatex, rotatey: grids rotated
         bullets (list of Bullet): bullets flying
         enemies (list of Enemy): alive enemies
@@ -87,7 +87,7 @@ class Maze:
 
         self.map = deque()
         for _ in range(MAZE_SIZE): self.map.extend(new_column())
-        self.down = self.right = 0
+        self.vx = self.vy = 0.0
         self.rotatex = self.rotatey = 0
         self.bullets, self.enemies = [], []
         self.add_enemy()
@@ -233,27 +233,27 @@ class Maze:
                 fallen.append(i)
         for i in reversed(fallen): self.bullets.pop(i)
 
-    def isvalid(self, step, dx=0, dy=0):
-        """Return True if it is valid to move by (dx, dy) (in steps),
-        False otherwise.
+    def valid_move(self, vx=0.0, vy=0.0):
+        """Return dx or dy if it it valid to move the maze in that
+        velocity, otherwise return 0.0.
         """
         d = self.distance/2 + self.hero.R
-        herox, heroy = self.x - step*dx, self.y - step*dy
+        herox, heroy, dx, dy = self.x - vx, self.y - vy, sign(vx), sign(vy)
         for x in range(MIDDLE - dx - 1, MIDDLE - dx + 2):
             for y in range(MIDDLE - dy - 1, MIDDLE - dy + 2):
                 gridx, gridy = self.pos(x, y)
                 if (max(abs(herox - gridx), abs(heroy - gridy)) < d
                     and self.map[x][y] == WALL):
-                    return False
-        return True
+                    return 0.0
+        return vx or vy
 
     def update(self, fps):
         """Update the maze."""
         if self.paused: return
-        self.fps, step = fps, self.distance * HERO_SPEED / fps
-        dx = step * self.right * self.isvalid(step, dx=self.right)
+        self.fps = fps
+        dx = self.valid_move(vx=self.vx)
         self.centerx += dx
-        dy = step * self.down * self.isvalid(step, dy=self.down)
+        dy = self.valid_move(vy=self.vy)
         self.centery += dy
 
         if dx or dy:
@@ -271,6 +271,27 @@ class Maze:
             int(self.score - INIT_SCORE)))
         if self.hero.wound + 1 > HERO_HP: self.lose()
 
+    def move(self, x, y, fps):
+        """Command the hero to move faster in the given direction."""
+        velocity = self.distance * HERO_SPEED / fps
+        accel = velocity * HERO_SPEED / fps
+        if not x:
+            self.vx -= sign(self.vx) * accel
+            if abs(self.vx) < accel: self.vx = 0.0
+        elif x * self.vx < 0:
+            self.vx += x * 2 * accel
+        else:
+            self.vx += x * accel
+            if abs(self.vx) > velocity: self.vx = x * velocity
+        if not y:
+            self.vy -= sign(self.vy) * accel
+            if abs(self.vy) < accel: self.vy = 0.0
+        elif y * self.vy < 0:
+            self.vy += y * 2 * accel
+        else:
+            self.vy += y * accel
+            if abs(self.vy) > velocity: self.vy = y * velocity
+
     def resize(self, w, h):
         """Resize the maze."""
         size = self.w, self.h = w, h
@@ -291,4 +312,4 @@ class Maze:
     def lose(self):
         """Handle loses."""
         self.hero.die()
-        self.down = self.right = 0
+        self.vx = self.vy = 0.0
diff --git a/setup.py b/setup.py
index cc739a2..20b9129 100755
--- a/setup.py
+++ b/setup.py
@@ -7,7 +7,7 @@ with open('README.rst') as f:
 
 setup(
     name='brutalmaze',
-    version='0.0.2',
+    version='0.0.3',
     description='A research hash and slash game with fast-paced action and a minimalist art style',
     long_description=long_description,
     url='https://github.com/McSinyx/brutalmaze',