def __init__(self, event_manager): self.event_manager = event_manager self.event_manager.register_listener(self) self.state = Game.STATE_PREPARING self.id_generator = ObjectIdGenerator() self.players = [Player(event_manager, self.id_generator)] program_path = os.path.dirname(__file__) conf_file_path = os.path.abspath(os.path.join(program_path, "../../config/config.cfg")) config = ConfigParser.ConfigParser() config.read(conf_file_path) config.set("Images", "rootdir", os.path.abspath(os.path.join(program_path, "../../"))) constants.CONFIG = config grid_size_x = constants.CONFIG.getint("Map", "grid_size_x") grid_size_y = constants.CONFIG.getint("Map", "grid_size_y") walls_up = [int(x) for x in constants.CONFIG.get('Map', 'walls_up').split(',')] walls_right = [int(x) for x in constants.CONFIG.get('Map', 'walls_right').split(',')] walls_down = [int(x) for x in constants.CONFIG.get('Map', 'walls_down').split(',')] walls_left = [int(x) for x in constants.CONFIG.get('Map', 'walls_left').split(',')] self.map = Map(event_manager, grid_size_x, grid_size_y, walls_up, walls_right, walls_left, walls_down) self.bullets = Bullets(event_manager)
class Game: """...""" STATE_PREPARING = 0 STATE_RUNNING = 1 STATE_PAUSED = 2 def __init__(self, event_manager): self.event_manager = event_manager self.event_manager.register_listener(self) self.state = Game.STATE_PREPARING self.id_generator = ObjectIdGenerator() self.players = [Player(event_manager, self.id_generator)] program_path = os.path.dirname(__file__) conf_file_path = os.path.abspath(os.path.join(program_path, "../../config/config.cfg")) config = ConfigParser.ConfigParser() config.read(conf_file_path) config.set("Images", "rootdir", os.path.abspath(os.path.join(program_path, "../../"))) constants.CONFIG = config grid_size_x = constants.CONFIG.getint("Map", "grid_size_x") grid_size_y = constants.CONFIG.getint("Map", "grid_size_y") walls_up = [int(x) for x in constants.CONFIG.get('Map', 'walls_up').split(',')] walls_right = [int(x) for x in constants.CONFIG.get('Map', 'walls_right').split(',')] walls_down = [int(x) for x in constants.CONFIG.get('Map', 'walls_down').split(',')] walls_left = [int(x) for x in constants.CONFIG.get('Map', 'walls_left').split(',')] self.map = Map(event_manager, grid_size_x, grid_size_y, walls_up, walls_right, walls_left, walls_down) self.bullets = Bullets(event_manager) def start(self): self.map.build() self.state = Game.STATE_RUNNING event = GameStartedEvent() self.event_manager.post( event ) def notify(self, event): if isinstance( event, TickEvent ): if self.state == Game.STATE_PREPARING: self.start() if isinstance( event, CharactorShootEvent ): bullet = self.bullets.create_bullet(event.charactor.sector, event.charactor.direction) new_event = BulletPlaceEvent(event.charactor.sector, bullet) self.event_manager.post(new_event)