all repos — Legends-RPG @ 8bf064b33239c7f22877ebb6915c97727a53837d

A fantasy mini-RPG built with Python and Pygame.

::w (view raw)

  1"""
  2Module for all game observers.
  3"""
  4import pygame as pg
  5from . import constants as c
  6from . import setup
  7from .components import attackitems
  8from . import setup
  9
 10class Battle(object):
 11    """
 12    Observes events of battle and passes info to components.
 13    """
 14    def __init__(self, level):
 15        self.level = level
 16        self.player = level.player
 17        self.set_observer_for_enemies()
 18        self.event_dict = self.make_event_dict()
 19
 20    def set_observer_for_enemies(self):
 21        for enemy in self.level.enemy_list:
 22            enemy.observers.append(self)
 23
 24    def make_event_dict(self):
 25        """
 26        Make a dictionary of events the Observer can
 27        receive.
 28        """
 29        event_dict = {c.ENEMY_DEAD: self.enemy_dead,
 30                      c.ENEMY_DAMAGED: self.enemy_damaged,
 31                      c.PLAYER_DAMAGED: self.player_damaged}
 32
 33        return event_dict
 34
 35    def on_notify(self, event):
 36        """
 37        Notify Observer of event.
 38        """
 39        if event in self.event_dict:
 40            self.event_dict[event]()
 41
 42    def player_damaged(self):
 43        self.level.enter_player_damaged_state()
 44
 45    def enemy_damaged(self):
 46        """
 47        Make an attack animation over attacked enemy.
 48        """
 49        self.level.enter_enemy_damaged_state()
 50
 51    def enemy_dead(self):
 52        """
 53        Eliminate all traces of enemy.
 54        """
 55        self.level.player.attacked_enemy = None
 56
 57
 58class SoundEffects(object):
 59    """
 60    Observer for sound effects.
 61    """
 62    def on_notify(self, event):
 63        """
 64        Observer is notified of SFX event.
 65        """
 66        if event in setup.SFX:
 67            setup.SFX[event].play()
 68
 69class MusicChange(object):
 70    """
 71    Observer for special music events.
 72    """
 73    def __init__(self):
 74        self.event_dict = self.make_event_dict()
 75
 76    def make_event_dict(self):
 77        """
 78        Make a dictionary with events keyed to new music.
 79        """
 80        new_dict = {c.BATTLE_WON: 'enchanted_festival'}
 81        return new_dict
 82
 83    def on_notify(self, event):
 84        """
 85        Observer is notified of change in music.
 86        """
 87        print event
 88        if event in self.event_dict:
 89            new_music = self.event_dict[event]
 90            if new_music in setup.MUSIC:
 91                music_file = setup.MUSIC[new_music]
 92                pg.mixer.music.load(new_music)
 93                pg.mixer.music.play(-1)
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112