Added more music and sfx.
jump to
@@ -0,0 +1,112 @@
+""" +Module for all game observers. +""" +import pygame as pg +from . import constants as c +from . import setup +from .components import attackitems +from . import setup + +class Battle(object): + """ + Observes events of battle and passes info to components. + """ + def __init__(self, level): + self.level = level + self.player = level.player + self.set_observer_for_enemies() + self.event_dict = self.make_event_dict() + + def set_observer_for_enemies(self): + for enemy in self.level.enemy_list: + enemy.observers.append(self) + + def make_event_dict(self): + """ + Make a dictionary of events the Observer can + receive. + """ + event_dict = {c.ENEMY_DEAD: self.enemy_dead, + c.ENEMY_DAMAGED: self.enemy_damaged, + c.PLAYER_DAMAGED: self.player_damaged} + + return event_dict + + def on_notify(self, event): + """ + Notify Observer of event. + """ + if event in self.event_dict: + self.event_dict[event]() + + def player_damaged(self): + self.level.enter_player_damaged_state() + + def enemy_damaged(self): + """ + Make an attack animation over attacked enemy. + """ + self.level.enter_enemy_damaged_state() + + def enemy_dead(self): + """ + Eliminate all traces of enemy. + """ + self.level.player.attacked_enemy = None + + +class SoundEffects(object): + """ + Observer for sound effects. + """ + def on_notify(self, event): + """ + Observer is notified of SFX event. + """ + if event in setup.SFX: + setup.SFX[event].play() + +class MusicChange(object): + """ + Observer for special music events. + """ + def __init__(self): + self.event_dict = self.make_event_dict() + + def make_event_dict(self): + """ + Make a dictionary with events keyed to new music. + """ + new_dict = {c.BATTLE_WON: 'enchanted_festival'} + return new_dict + + def on_notify(self, event): + """ + Observer is notified of change in music. + """ + print event + if event in self.event_dict: + new_music = self.event_dict[event] + if new_music in setup.MUSIC: + music_file = setup.MUSIC[new_music] + pg.mixer.music.load(new_music) + pg.mixer.music.play(-1) + + + + + + + + + + + + + + + + + + +
@@ -186,6 +186,8 @@ thank_you_dialogue = ['Thank you for retrieving my crown.',
'My kingdom is forever in your debt.'] self.talking_sprite.dialogue = thank_you_dialogue self.end_dialogue(current_time) + else: + self.end_dialogue(current_time) else: self.end_dialogue(current_time)
@@ -79,5 +79,7 @@ CLICK2 = 'click2'
CLOTH_BELT = 'cloth_belt' SWORD = 'sword' FIRE = 'fire' +PUNCH = 'punch' +POWERUP = 'powerup' TRANSITION_SPEED = 25
@@ -1,6 +1,7 @@
""" Module for all game observers. """ +import pygame as pg from . import constants as c from . import setup from .components import attackitems@@ -64,6 +65,31 @@ Observer is notified of SFX event.
""" if event in setup.SFX: setup.SFX[event].play() + +class MusicChange(object): + """ + Observer for special music events. + """ + def __init__(self): + self.event_dict = self.make_event_dict() + + def make_event_dict(self): + """ + Make a dictionary with events keyed to new music. + """ + new_dict = {c.BATTLE_WON: 'enchanted_festival'} + return new_dict + + def on_notify(self, event): + """ + Observer is notified of change in music. + """ + if event in self.event_dict: + new_music = self.event_dict[event] + if new_music in setup.MUSIC: + music_file = setup.MUSIC[new_music] + pg.mixer.music.load(music_file) + pg.mixer.music.play(-1)
@@ -393,9 +393,6 @@ self.player_inventory[item_name]['quantity'] -= 1
else: del self.player_inventory[self.item_to_be_sold['type']] - - - def reject_insufficient_gold(self, keys, current_time): """Reject player selection if they do not have enough gold""" dialogue = ["You don't have enough gold!"]@@ -410,7 +407,6 @@
if not keys[pg.K_SPACE]: self.allow_input = True - def accept_purchase(self, keys, current_time): """Accept purchase and confirm with message""" self.dialogue_box = self.make_dialogue_box(self.accept_dialogue, 0)@@ -424,7 +420,6 @@ self.allow_input = False
if not keys[pg.K_SPACE]: self.allow_input = True - def accept_sale(self, keys, current_time): """Confirm to player that item was sold"""@@ -489,9 +484,7 @@ else:
self.state = 'cantsell' self.allow_input = False self.arrow_index = 0 - else: - self.level.state = 'transition out' self.game_data['last state'] = self.level.name@@ -501,7 +494,6 @@
if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]: self.allow_input = True - def check_for_sellable_items(self): """Check for sellable items""" for item in self.player_inventory:@@ -509,7 +501,6 @@ if item in self.sellable_items:
return True else: return False - def sell_items(self, keys, current_time): """Have player select items to sell"""
@@ -41,7 +41,8 @@ self.player_health_box = battlegui.PlayerHealth(self.select_box.rect,
self.game_data) self.select_action_state_dict = self.make_selection_state_dict() - self.observers = [observer.Battle(self)] + self.observers = [observer.Battle(self), + observer.MusicChange()] self.player.observers.extend(self.observers) self.observers.append(observer.SoundEffects()) self.damage_points = pg.sprite.Group()@@ -487,37 +488,7 @@ self.damage_points.add(
attackitems.HealthPoints(HEAL_AMOUNT, self.player.rect.topright, False)) self.player_healed(HEAL_AMOUNT, MAGIC_POINTS) self.info_box.state = c.DRINK_HEALING_POTION - - def drink_ether(self): - """ - Drink ether potion. - """ - self.state = self.info_box.state = c.DRINK_ETHER_POTION - self.player.healing = True - self.set_timer_to_current_time() - self.arrow.state = 'invisible' - self.enemy_index = 0 - self.damage_points.add( - attackitems.HealthPoints(30, - self.player.rect.topright, - False, - True)) - self.magic_boost(30) - - def drink_healing_potion(self): - """ - Drink Healing Potion. - """ - self.state = self.info_box.state = c.DRINK_HEALING_POTION - self.player.healing = True - self.arrow.state = 'invisible' - self.enemy_index = 0 - self.damage_points.add( - attackitems.HealthPoints(30, - self.player.rect.topright, - False)) - self.player_healed(30) - self.set_timer_to_current_time() + self.notify(c.POWERUP) def enter_select_enemy_state(self): """@@ -602,6 +573,7 @@ attackitems.HealthPoints(30,
self.player.rect.topright, False)) self.player_healed(30) + self.notify(c.POWERUP) def enter_drink_ether_potion_state(self): """@@ -618,6 +590,7 @@ False,
True)) self.magic_boost(30) self.set_timer_to_current_time() + self.notify(c.POWERUP) def enter_select_action_state(self): """@@ -644,6 +617,8 @@ self.info_box.set_player_damage(player_damage)
self.set_timer_to_current_time() self.player_damaged(player_damage) if player_damage: + sfx_num = random.randint(1,3) + self.notify('punch{}'.format(sfx_num)) self.player.damaged = True self.player.enter_knock_back_state()@@ -684,6 +659,7 @@ def enter_battle_won_state(self):
""" Transition battle into the battle won state. """ + self.notify(c.BATTLE_WON) self.state = self.info_box.state = c.BATTLE_WON self.player.state = c.VICTORY_DANCE self.set_timer_to_current_time()
@@ -209,7 +209,7 @@
def make_purchasable_items(self): """Make list of items to be chosen""" - longsword_dialogue = 'Long Sword (100 gold)' + longsword_dialogue = 'Long Sword (150 gold)' rapier_dialogue = 'Rapier (50 gold)' item2 = {'type': 'Long Sword',
@@ -51,7 +51,7 @@ """
if self.state.music: pg.mixer.music.load(self.state.music) pg.mixer.music.set_volume(.4) - pg.mixer.music.play() + pg.mixer.music.play(-1) else: pg.mixer.music.stop()