summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2017-10-22 20:10:40 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2017-10-22 20:31:17 +0700
commit3e25eb424c0ed645eb6be437d05bce47aab920b7 (patch)
tree7118f4e7a0e22a1b77ef18994ce2f10647a0f719
parentaafa943a5313721cb2dee408a0b60c8b11ff5ad2 (diff)
downloadbrutalmaze-3e25eb424c0ed645eb6be437d05bce47aab920b7.tar.gz
Add fancy losing screen and keybinding for new game
-rw-r--r--MANIFEST.in2
-rw-r--r--README.rst6
-rw-r--r--brutalmaze/characters.py36
-rw-r--r--brutalmaze/constants.py2
-rw-r--r--brutalmaze/main.py12
-rw-r--r--brutalmaze/maze.py8
6 files changed, 44 insertions, 22 deletions
diff --git a/MANIFEST.in b/MANIFEST.in
index 0c73842..4384b67 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1 +1 @@
-include README.rst LICENSE
+include README.rst LICENSE screenshot.png
diff --git a/README.rst b/README.rst
index 7676d22..720d270 100644
--- a/README.rst
+++ b/README.rst
@@ -35,6 +35,12 @@ The installation procedure should be as simply as follow:
 Control
 -------
 
+F2
+   New game.
+
+Escape, ``p``
+   Pause.
+
 Up, ``w``
    Move up.
 
diff --git a/brutalmaze/characters.py b/brutalmaze/characters.py
index 0490a55..b64c2fa 100644
--- a/brutalmaze/characters.py
+++ b/brutalmaze/characters.py
@@ -39,7 +39,7 @@ class Hero:
         self.R = int((w * h / sin(pi*2/3) / 624) ** 0.5)
 
         self.next_strike = 0
-        self.slashing = self.firing = False
+        self.slashing = self.firing = self.dead = False
         self.spin_speed = fps / HERO_HP
         self.spin_queue = self.wound = 0.0
 
@@ -48,7 +48,7 @@ class Hero:
         old_speed, time = self.spin_speed, pygame.time.get_ticks()
         self.spin_speed = fps / (HERO_HP-self.wound**0.5)
         self.spin_queue *= self.spin_speed / old_speed
-        self.wound -= HEAL_SPEED / self.spin_speed / HERO_HP
+        if not self.dead: self.wound -= HEAL_SPEED / self.spin_speed / HERO_HP
         if self.wound < 0: self.wound = 0.0
 
         if self.slashing and time >= self.next_strike:
@@ -63,7 +63,15 @@ class Hero:
             self.angle = atan2(y - self.y, x - self.x)
             self.spin_queue = 0.0
         trigon = regpoly(3, self.R, self.angle, self.x, self.y)
-        fill_aapolygon(self.surface, trigon, self.color[int(self.wound)])
+        try:
+            fill_aapolygon(self.surface, trigon, self.color[int(self.wound)])
+        except IndexError:  # When the hero is wounded over his HP
+            self.wound = HERO_HP
+
+    def die(self):
+        """Handle the hero's death."""
+        self.dead = True
+        self.slashing = self.firing = False
 
     def resize(self):
         """Resize the hero."""
@@ -87,16 +95,6 @@ class Enemy:
         self.spin_speed = fps / ENEMY_HP
         self.spin_queue = self.wound = 0.0
 
-    def firable(self):
-        """Return True if the enemies should shoot the hero,
-        False otherwise.
-        """
-        if not self.awake or self.spin_queue or self.offsetx or self.offsety:
-            return False
-        else:
-            self.next_move = pygame.time.get_ticks() + ATTACK_SPEED
-            return True
-
     def pos(self, distance, middlex, middley):
         """Return coordinate of the center of the enemy."""
         x, y = pos(self.x, self.y, distance, middlex, middley)
@@ -152,6 +150,16 @@ class Enemy:
         color = self.color[int(self.wound)] if self.awake else FG_COLOR
         fill_aapolygon(self.surface, square, color)
 
+    def firable(self):
+        """Return True if the enemies should shoot the hero,
+        False otherwise.
+        """
+        if (not self.awake or self.spin_queue or self.offsetx or self.offsety
+            or (self.x, self.y) in SURROUND_HERO):
+            return False
+        self.next_move = pygame.time.get_ticks() + ATTACK_SPEED
+        return True
+
     def die(self):
-        """Kill the enemy."""
+        """Handle the enemy's death."""
         self.maze[self.x][self.y] = EMPTY if self.awake else WALL
diff --git a/brutalmaze/constants.py b/brutalmaze/constants.py
index d3d97b5..0835738 100644
--- a/brutalmaze/constants.py
+++ b/brutalmaze/constants.py
@@ -44,6 +44,8 @@ BULLET_LIFETIME = 1000  # ms
 
 EMPTY, WALL, HERO, ENEMY = range(4)
 ADJACENT_GRIDS = (1, 0), (0, 1), (-1, 0), (0, -1)
+SURROUND_HERO = set((MIDDLE + x, MIDDLE + y) for x, y in
+                    ADJACENT_GRIDS + ((1, 1), (-1, 1), (-1, -1), (1, -1)))
 
 TANGO = {'Butter': ((252, 233, 79), (237, 212, 0), (196, 160, 0)),
          'Orange': ((252, 175, 62), (245, 121, 0), (206, 92, 0)),
diff --git a/brutalmaze/main.py b/brutalmaze/main.py
index c58e9cb..cd3a026 100644
--- a/brutalmaze/main.py
+++ b/brutalmaze/main.py
@@ -38,8 +38,14 @@ def main():
         for event in events:
             if event.type == QUIT:
                 going = False
+            elif event.type == VIDEORESIZE:
+                maze.resize(event.w, event.h)
             elif event.type == KEYDOWN:
-                if event.key in (K_ESCAPE, K_p):
+                if event.key == K_F2:   # new game
+                    maze.__init__((maze.w, maze.h), fps)
+                elif maze.hero.dead:
+                    continue
+                elif event.key in (K_ESCAPE, K_p):
                     maze.paused ^= True
                 elif event.key in (K_UP, K_w):
                     maze.move(up=-1)
@@ -51,6 +57,8 @@ def main():
                     maze.move(right=-1)
                 elif event.key == K_RETURN:
                     maze.hero.slashing = True
+            elif maze.hero.dead:
+                continue
             elif event.type == KEYUP:
                 if event.key in (K_UP, K_w):
                     maze.move(up=1)
@@ -72,8 +80,6 @@ def main():
                     maze.hero.firing = False
                 elif event.button == 3:
                     maze.hero.slashing = False
-            elif event.type == VIDEORESIZE:
-                maze.resize(event.w, event.h)
         if len(flash_time) > 5:
             new_fps = 5000.0 / (flash_time[-1] - flash_time[0])
             flash_time.popleft()
diff --git a/brutalmaze/maze.py b/brutalmaze/maze.py
index 2e1e44c..01dd6de 100644
--- a/brutalmaze/maze.py
+++ b/brutalmaze/maze.py
@@ -262,11 +262,11 @@ class Maze:
             for bullet in self.bullets: bullet.place(dx, dy, self.step)
 
         self.draw()
-        self.slash()
-        self.track_bullets()
         for enemy in self.enemies:
             enemy.update(fps, self.distance, self.middlex, self.middley)
         self.hero.update(fps)
+        self.slash()
+        self.track_bullets()
         pygame.display.flip()
         pygame.display.set_caption('Brutal Maze - Score: {}'.format(
             int(self.score - INIT_SCORE)))
@@ -299,5 +299,5 @@ class Maze:
 
     def lose(self):
         """Handle loses."""
-        print('Your score is: {}'.format(int(self.score - INIT_SCORE)))
-        quit()
+        self.hero.die()
+        self.up = self.left = self.down = self.right = 0