summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2017-10-17 21:29:03 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2017-10-17 21:29:03 +0700
commite879dcf58dc03ae6730d925675ae6ee6f9733e71 (patch)
tree31afa82e88667f7db220b2cf3854e07c3d08bb89
parent22bb77f9184b813bf0841a0939546d5eeb0c58e3 (diff)
downloadbrutalmaze-e879dcf58dc03ae6730d925675ae6ee6f9733e71.tar.gz
Optimize maze regeneration and show score
-rw-r--r--brutalmaze/characters.py3
-rw-r--r--brutalmaze/constants.py3
-rw-r--r--brutalmaze/main.py1
-rw-r--r--brutalmaze/maze.py39
4 files changed, 29 insertions, 17 deletions
diff --git a/brutalmaze/characters.py b/brutalmaze/characters.py
index 0ef5d17..8dc3209 100644
--- a/brutalmaze/characters.py
+++ b/brutalmaze/characters.py
@@ -136,7 +136,8 @@ class Enemy:
 
     def draw(self, distance, middlex, middley, color):
         """Draw the enemy, given distance between grids and the middle grid."""
-        square = regpoly(4, distance / SQRT2, self.angle,
+        radious = distance/SQRT2 - (self.awake and 2)
+        square = regpoly(4, radious, self.angle,
                          *self.pos(distance, middlex, middley))
         fill_aapolygon(self.surface, square, color)
 
diff --git a/brutalmaze/constants.py b/brutalmaze/constants.py
index 367bcb0..427226e 100644
--- a/brutalmaze/constants.py
+++ b/brutalmaze/constants.py
@@ -32,7 +32,8 @@ FPS = 30
 SIZE = 400, 400
 MAZE_SIZE = 10
 ROAD_WIDTH = 5
-MIDDLE = (MAZE_SIZE + (MAZE_SIZE&1) - 1)*ROAD_WIDTH + (ROAD_WIDTH >> 1)
+CELL_WIDTH = ROAD_WIDTH * 2
+MIDDLE = (MAZE_SIZE + MAZE_SIZE%2 - 1)*ROAD_WIDTH + ROAD_WIDTH//2
 LAST_ROW = (MAZE_SIZE-1) * ROAD_WIDTH * 2
 INIT_SCORE = 208.2016
 MOVE_SPEED = 5  # step/grid
diff --git a/brutalmaze/main.py b/brutalmaze/main.py
index c41d444..40b4116 100644
--- a/brutalmaze/main.py
+++ b/brutalmaze/main.py
@@ -26,7 +26,6 @@ from .maze import Maze
 def main():
     """Start game and main loop."""
     pygame.init()
-    pygame.display.set_caption('Brutal Maze')
     pygame.display.set_icon(ICON)
     pygame.fastevent.init()
     maze, going, clock = Maze(SIZE), True, pygame.time.Clock()
diff --git a/brutalmaze/maze.py b/brutalmaze/maze.py
index 1eb1784..4b2abdb 100644
--- a/brutalmaze/maze.py
+++ b/brutalmaze/maze.py
@@ -132,23 +132,13 @@ class Maze:
         if x:
             self.offsetx = 0
             self.map.rotate(x)
-            self.rotatex = (self.rotatex+x) % (ROAD_WIDTH*2)
+            self.rotatex += x
         if y:
             self.offsety = 0
             for d in self.map: d.rotate(y)
-            self.rotatey = (self.rotatey+y) % (ROAD_WIDTH*2)
-        if not self.rotatex and not self.rotatey:
-            for _ in range(ROAD_WIDTH * 2): self.map.pop()
-            self.map.extend(new_column())
-            for i in range(MAZE_SIZE):
-                b = getrandbits(1)
-                for j, grid in enumerate(cell(b)):
-                    for k in range(ROAD_WIDTH):
-                        self.map[i*2*ROAD_WIDTH+k][LAST_ROW + j] = grid
-                for j, grid in enumerate(cell(b, False)):
-                    for k in range(ROAD_WIDTH):
-                        self.map[(i*2+1)*ROAD_WIDTH+k][LAST_ROW + j] = grid
+            self.rotatey += y
 
+        # Respawn the enemies that fall off the display
         killist = []
         for i, enemy in enumerate(self.enemies):
             enemy.place(x, y)
@@ -158,6 +148,25 @@ class Maze:
         for i in reversed(killist): self.enemies.pop(i)
         self.add_enemy()
 
+        # Regenerate the maze
+        if abs(self.rotatex) == CELL_WIDTH:
+            self.rotatex = 0
+            for _ in range(CELL_WIDTH): self.map.pop()
+            self.map.extend(new_column())
+            for i in range(-CELL_WIDTH, 0):
+                self.map[i].rotate(self.rotatey)
+        if abs(self.rotatey) == CELL_WIDTH:
+            self.rotatey = 0
+            for i in range(MAZE_SIZE):
+                b, c = getrandbits(1), (i-1)*CELL_WIDTH + self.rotatex
+                for j, grid in enumerate(cell(b)):
+                    for k in range(ROAD_WIDTH):
+                        self.map[c + k][LAST_ROW + j] = grid
+                c += ROAD_WIDTH
+                for j, grid in enumerate(cell(b, False)):
+                    for k in range(ROAD_WIDTH):
+                        self.map[c + k][LAST_ROW + j] = grid
+
     def slash(self):
         """Slash the enemies."""
         unit, killist = self.distance/SQRT2 * self.hero.speed, []
@@ -218,7 +227,9 @@ class Maze:
             if d <= self.slashd:
                 self.hero.wound += (self.slashd-d) / self.hero.R / enemy.speed
         pygame.display.flip()
-        if self.hero.wound + 1 >= len(self.hero.color): self.lose()
+        pygame.display.set_caption('Brutal Maze - Score: {}'.format(
+            int(self.score - INIT_SCORE)))
+        if self.hero.wound + 1 > len(self.hero.color): self.lose()
 
     def resize(self, w, h):
         """Resize the maze."""