def play_sfx(self, name, offset=(0., 0., 0.), volume=1.): source = SoundSource() source.queue(load_wav_file(get_sfx(name))) source.gain = volume source.position = tuple( new_pt(*self.sink.listener.position) + new_pt(*offset)) self.sink.play(source)
def __init__(self, world): self.world = world self.step_size = world.SCALE self.pos = new_pt() self.pos_t = new_pt() self.since_last_point = 0. self.was_on_wall = False self.health = self.MAX_HEALTH self.listener = world.sink.listener self.damage_source = ContinuousSoundSource(lambda: load_wav_file(get_sfx('damage.wav')))
def calculate_bg_color(self): if not self.alive: return new_pt(0.7, 0.0, 0.0) def light_ratio(dist, damper=0.2): return 1. / max(1., dist / damper) enemy = light_ratio(vec_dist(self.player.pos, self.enemy.pos)) if self.enemy else 0. target = light_ratio(vec_dist(self.player.pos, self.orb.pos)) wall = light_ratio(self.SIZE - max(abs(self.player.pos))) color = new_pt() for ratio, item_color in [(target, (1., 1., 1.)), (enemy, (1., 0., 0.)), (wall, (.2, .4, .9))]: color += (1. - max(color)) * ratio * new_pt(*item_color) return color
def update(self): if not self.world.alive: self.damage_source.gain = max(0, self.damage_source.gain - 0.01) return self.update_controls() on_wall = False for i in 0, 1, 2: if self.pos_t[i] > self.world.SIZE: self.pos_t[i] = self.world.SIZE sign = +1 elif self.pos_t[i] < -self.world.SIZE: self.pos_t[i] = -self.world.SIZE sign = -1 else: continue on_wall = True if not self.was_on_wall: offset = new_pt() offset[i] = sign self.world.play_sfx('wall.wav', offset=offset) self.was_on_wall = on_wall self.pos += 0.2 * (self.pos_t - self.pos) if self.world.enemy: enemy_dist = vec_dist(self.pos, self.world.enemy.pos) if enemy_dist < 8 * self.step_size: self.damage_source.gain = 1. self.health -= 2. / self.world.FPS else: self.damage_source.gain = 0. else: self.health -= 0.05 / self.world.FPS self.damage_source.gain = 0. if self.health < 0.: self.health = 0. self.world.game_over() sound_dist = vec_dist(self.pos, self.world.orb.pos_t) if sound_dist < 8 * self.step_size: self.world.score_points(10 + 1000 / max(1., self.since_last_point)) self.health = min(self.health + 0.1, self.MAX_HEALTH) self.since_last_point = 0. self.since_last_point += 1. / self.world.FPS self.listener.position = tuple(self.pos) self.damage_source.position = tuple(self.pos)
def render_text(text, pos, color=(255, 255, 255)): surface = font.render(str(text), True, color) pos = (new_pt(pos[0] * screen.get_width(), pos[1] * screen.get_height()) - new_pt(*surface.get_size()) / 2) screen.blit(surface, tuple(pos))
def random_point(self, ratio=1.): width = int(ratio * self.SIZE) return new_pt( randint(0, 2 * width) - width, 0, randint(0, 2 * width) - width)