about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2017-12-17 17:05:17 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2017-12-17 17:05:17 +0700
commit966951db60b60bacfd2c8559acc46c47f62f3295 (patch)
tree106a5803dbe83c4897deaf7025a8f7439cd395dd
parent4c65f2130d6f1c4e3450b988e0f1bed0e086062f (diff)
downloadbrutalmaze-966951db60b60bacfd2c8559acc46c47f62f3295.tar.gz
Add easy sound effects
-rw-r--r--brutalmaze/characters.py26
-rw-r--r--brutalmaze/constants.py6
-rw-r--r--brutalmaze/main.py5
-rw-r--r--brutalmaze/maze.py13
-rw-r--r--brutalmaze/soundfx/lose.oggbin0 -> 18159 bytes
-rw-r--r--brutalmaze/soundfx/music.oggbin0 -> 1409097 bytes
-rw-r--r--brutalmaze/soundfx/shot-enemy.oggbin0 -> 7703 bytes
-rw-r--r--brutalmaze/soundfx/shot-hero.oggbin0 -> 7066 bytes
8 files changed, 34 insertions, 16 deletions
diff --git a/brutalmaze/characters.py b/brutalmaze/characters.py
index 0947300..8a7ff8d 100644
--- a/brutalmaze/characters.py
+++ b/brutalmaze/characters.py
@@ -61,9 +61,13 @@ class Hero:
         self.spin_speed = fps / HERO_HP
         self.spin_queue = self.wound = 0.0
 
+        self.sfx_shot = pygame.mixer.Sound(SFX_SHOT_HERO)
+
     def update(self, fps):
         """Update the hero."""
-        if self.dead: return
+        if self.dead:
+            self.spin_queue = 0
+            return
         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
@@ -85,11 +89,6 @@ class Hero:
         trigon = regpoly(3, self.R, self.angle, self.x, self.y)
         fill_aapolygon(self.surface, trigon, self.color[int(self.wound)])
 
-    def die(self):
-        """Handle the hero's death."""
-        self.dead = True
-        self.slashing = self.firing = False
-
     def resize(self):
         """Resize the hero."""
         w, h = self.surface.get_width(), self.surface.get_height()
@@ -126,6 +125,8 @@ class Enemy:
         self.spin_speed = self.maze.fps / ENEMY_HP
         self.spin_queue = self.wound = 0.0
 
+        self.sfx_shot = pygame.mixer.Sound(SFX_SHOT_ENEMY)
+
     def get_pos(self):
         """Return coordinate of the center of the enemy."""
         x, y = self.maze.get_pos(self.x, self.y)
@@ -205,7 +206,7 @@ class Enemy:
             d = self.maze.slashd - self.maze.get_distance(*self.get_pos())
             wound = d / self.maze.hero.R / self.spin_speed
             if wound >= 0:
-                self.maze.hit(wound, self.color)
+                self.maze.hit(wound, self.color, per_frame=True)
                 return wound
         return 0.0
 
@@ -230,9 +231,12 @@ class Enemy:
                 self.angle, self.spin_queue = pi / 4, 0.0
         self.draw()
 
-    def hit(self, wound):
+    def hit(self, wound, per_frame=False):
         """Handle the enemy when it's attacked."""
         self.wound += wound
+        if not per_frame:
+            self.sfx_shot.set_volume(wound)
+            self.sfx_shot.play()
 
     def die(self):
         """Handle the enemy's death."""
@@ -264,10 +268,10 @@ class Chameleon(Enemy):
         if not self.awake or pygame.time.get_ticks() <= self.visible:
             Enemy.draw(self)
 
-    def hit(self, wound):
-        """Handle the Chameleon when it's hit by a bullet."""
+    def hit(self, wound, per_frame=False):
+        """Handle the Chameleon when it's attacked."""
         self.visible = pygame.time.get_ticks() + 1000//ENEMY_SPEED
-        self.wound += wound
+        Enemy.hit(self, wound, per_frame)
 
 
 class Plum(Enemy):
diff --git a/brutalmaze/constants.py b/brutalmaze/constants.py
index 5f5e25a..6c896e1 100644
--- a/brutalmaze/constants.py
+++ b/brutalmaze/constants.py
@@ -20,9 +20,15 @@
 __doc__ = 'brutalmaze module for shared constants'
 
 from pygame import image, K_UP, K_w, K_LEFT, K_a, K_DOWN, K_s, K_RIGHT, K_d
+from pygame.mixer import Sound
 from pkg_resources import resource_filename
 
 ICON = image.load(resource_filename('brutalmaze', 'icon.png'))
+MUSIC = resource_filename('brutalmaze', 'soundfx/music.ogg')
+SFX_SHOT_ENEMY = resource_filename('brutalmaze', 'soundfx/shot-enemy.ogg')
+SFX_SHOT_HERO = resource_filename('brutalmaze', 'soundfx/shot-hero.ogg')
+SFX_LOSE = resource_filename('brutalmaze', 'soundfx/lose.ogg')
+
 UP = (K_UP, K_w)
 LEFT = (K_LEFT, K_a)
 DOWN = (K_DOWN, K_s)
diff --git a/brutalmaze/main.py b/brutalmaze/main.py
index bc78962..291981a 100644
--- a/brutalmaze/main.py
+++ b/brutalmaze/main.py
@@ -22,14 +22,17 @@ from collections import deque
 import pygame
 from pygame.locals import *
 
-from .constants import ICON, SIZE, INIT_FPS, MAX_FPS, UP, LEFT, DOWN, RIGHT
+from .constants import *
 from .maze import Maze
 from .utils import some
 
 
 def main():
     """Start game and main loop."""
+    pygame.mixer.pre_init(frequency=44100)
     pygame.init()
+    pygame.mixer.music.load(MUSIC)
+    pygame.mixer.music.play(-1)
     pygame.display.set_icon(ICON)
     pygame.fastevent.init()
     maze, clock = Maze(SIZE, INIT_FPS), pygame.time.Clock()
diff --git a/brutalmaze/maze.py b/brutalmaze/maze.py
index 5788777..8597055 100644
--- a/brutalmaze/maze.py
+++ b/brutalmaze/maze.py
@@ -183,8 +183,11 @@ class Maze:
         """
         return ((self.x-x)**2 + (self.y-y)**2)**0.5
 
-    def hit(self, wound, color):
+    def hit(self, wound, color, per_frame=False):
         """Handle the hero when he loses HP."""
+        if not per_frame:
+            self.hero.sfx_shot.set_volume(wound)
+            self.hero.sfx_shot.play()
         fx = (uniform(0, sum(self.enemy_weights.values()))
               < self.enemy_weights[color])
         time = pygame.time.get_ticks()
@@ -198,7 +201,7 @@ class Maze:
             self.hero.wound += wound
         if self.enemy_weights[color] + wound < MAXW:
             self.enemy_weights[color] += wound
-        if self.hero.wound > HERO_HP: self.lose()
+        if self.hero.wound > HERO_HP and not self.hero.dead: self.lose()
 
     def slash(self):
         """Handle close-range attacks."""
@@ -209,7 +212,7 @@ class Maze:
             x, y = enemy.get_pos()
             d = self.get_distance(x, y)
             if d <= self.slashd:
-                enemy.hit((self.slashd-d) / unit)
+                enemy.hit((self.slashd-d) / unit, per_frame=True)
                 if enemy.wound >= ENEMY_HP:
                     self.score += enemy.wound
                     enemy.die()
@@ -338,5 +341,7 @@ class Maze:
 
     def lose(self):
         """Handle loses."""
-        self.hero.die()
+        self.hero.dead = True
+        self.hero.slashing = self.hero.firing = False
         self.vx = self.vy = 0.0
+        pygame.mixer.Sound(SFX_LOSE).play()
diff --git a/brutalmaze/soundfx/lose.ogg b/brutalmaze/soundfx/lose.ogg
new file mode 100644
index 0000000..a71cc0f
--- /dev/null
+++ b/brutalmaze/soundfx/lose.ogg
Binary files differdiff --git a/brutalmaze/soundfx/music.ogg b/brutalmaze/soundfx/music.ogg
new file mode 100644
index 0000000..9e764bf
--- /dev/null
+++ b/brutalmaze/soundfx/music.ogg
Binary files differdiff --git a/brutalmaze/soundfx/shot-enemy.ogg b/brutalmaze/soundfx/shot-enemy.ogg
new file mode 100644
index 0000000..adc80cf
--- /dev/null
+++ b/brutalmaze/soundfx/shot-enemy.ogg
Binary files differdiff --git a/brutalmaze/soundfx/shot-hero.ogg b/brutalmaze/soundfx/shot-hero.ogg
new file mode 100644
index 0000000..94bf4a1
--- /dev/null
+++ b/brutalmaze/soundfx/shot-hero.ogg
Binary files differ