Add easy sound effects

This commit is contained in:
Nguyễn Gia Phong 2017-12-17 17:05:17 +07:00
parent 4c65f2130d
commit 966951db60
8 changed files with 34 additions and 16 deletions

View File

@ -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):

View File

@ -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)

View File

@ -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()

View File

@ -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()

BIN
brutalmaze/soundfx/lose.ogg Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.