def init_game(self): # Initialization scenario = Scenario() scenario.map_path = 'content/maps/new.map' tilemap = TileMap(scenario.map_path) ctrl = GameCtrl() # Game Setup euid = uuid4() scenario.entities = { euid: 'gorilla' } if self.parent.has_screen('game'): game = self.parent.get_screen('game') else: game = Game(name='game') self.parent.add_widget(game) player = game.controller player.entity = euid # Start ctrl.setup( scenario=scenario, tilemap=tilemap, players={ player.uid: player, }, assignments={ player.uid: euid, } ) ctrl.start() game._ctrl = ctrl self.parent.switch_to('game')
def main(): """This function is the main function of the game.""" pygame.init() try: Game.run() except FileNotFoundError as err: print(err.args[0]) pygame.quit() exit()
def test_making_choices_removes_listener(): game = Game() # noqa: F841 # scene_0 (Decision) -> combat scene options_0 = { 's': DecisionOption('load next scene', combat_scene.CombatScene) } scene_0 = DecisionScene('first scene', options_0) event_utils.post_scene_change(scene_0) ctl = _get_active_controller() assert isinstance(ctl, DecisionSceneController) # we must remove references otherwise EventManager will keep this listener del ctl del scene_0 event_utils.simulate_key_press('s') # The scene machine only changes scenes on a tick EventManager.post(BasicEvents.TICK) assert not any( isinstance(l, DecisionScene) for l in EventManager.listeners) assert not any( isinstance(l, DecisionSceneController) for l in EventManager.listeners) ctl = _get_active_controller() assert isinstance(ctl, CombatSceneController) assert any( isinstance(l, combat_scene.CombatScene) for l in EventManager.listeners)
def test_combat_scene_to_decision_scene(): game = Game() # noqa: F841 view_manager = ViewManager() # noqa: F841 # dummy decision scene to which will will transision def dummy_scene(): return DecisionScene('dummy scene for testing purposes', {}) # combat scene with two 1 health enemies enemies = [_one_health_enemy() for _ in range(2)] scene = combat_scene.CombatScene( enemies, win_resolution=BasicResolution(dummy_scene)) event_utils.post_scene_change(scene) assert isinstance(_get_active_controller(), CombatSceneController) # Start with player holding nothing player = get_player() [player.chassis.remove_mod(mod) for mod in player.chassis.all_mods()] assert len(list(player.chassis.all_mods())) == 1 # Base mod only # give player ability to fire laser shoot_laser = direct_damage(1, label='laser', time_to_resolve=0, cpu_slots=0) laser_mod = build_mod(subroutines_granted=shoot_laser, valid_slots=tuple(s for s in SlotTypes if s != SlotTypes.STORAGE)) assert player.chassis.can_store(laser_mod) player.chassis.attempt_store(laser_mod) assert laser_mod in player.chassis.all_active_mods() # Kill each enemy for enemy in enemies: # click enemy click_on_char(enemy, scene.layout) assert enemy is selected_char(scene.layout) # select the fire laser ability laser_ind = [ ind for ind, move in enumerate(scene.available_moves()) if move.subroutine is shoot_laser ][0] event_utils.simulate_key_press(str(laser_ind + 1)) assert is_dead(enemy) # check that scene has ended assert scene.is_resolved() # update the scene by waiting a tick, confirm that we've switched to a # Decision scene EventManager.post(BasicEvents.TICK) assert isinstance(_get_active_controller(), DecisionSceneController)
def test_press_debug_in_decision_scene_has_no_effect(): game = Game() view_manager = ViewManager() event_utils.post_scene_change( DecisionScene('dummy scene for test purposes', {})) assert isinstance(game.scene_machine.controller, DecisionSceneController) assert view_manager.current_view is not None EventManager.post(BasicEvents.DEBUG) EventManager.post(BasicEvents.TICK)
def initialize_logging() -> None: log_file = LOGGING_FILE clear_log(log_file) fmt = '%(asctime)s {} [%(levelname)s] %(message)s'.format(VERSION) formatter = logging.Formatter(fmt) logger = logging.getLogger() logger.setLevel(LOG_LEVEL) file_logger = logging.FileHandler(log_file) file_logger.setFormatter(formatter) logger.addHandler(file_logger) console_logger = logging.StreamHandler() console_logger.setFormatter(formatter) logger.addHandler(console_logger) if __name__ == '__main__': initialize_logging() logging.info('Start Application') initialize_pygame() view_manager = ViewManager() # This instantiates the singleton class g = Game() g.run() sys.exit()
def test_initializing_game_adds_listeners(): EventManager.listeners.clear() # Other test modules may populate assert len(EventManager.listeners) == 0 game = Game() # noqa: F841 assert len(EventManager.listeners) > 0
def start_state_game_on(self): print("start state 'game on`") self.cancel_prev_state() self.curr_state = Game(self._loop, self._audio_service, self._boxes, self.players_service, self._stage, self.start_state_play_song)
def test_inventory_scene_control_flow(): game = Game() # noqa: F841 view_manager = ViewManager() # noqa: F841 # Start with player holding nothing chassis = get_player().chassis [chassis.remove_mod(mod) for mod in chassis.all_mods()] assert len(list(chassis.all_mods())) == 1 # Base mod only # Setup start scene that loads the loot scene mod_locs = [ SlotTypes.HEAD, SlotTypes.HEAD, SlotTypes.CHEST, SlotTypes.LEGS ] ground_mods = _typical_mods(mod_locs) def start_scene() -> DecisionScene: loot_scene = partial(InventoryScene, start_scene, lambda: ground_mods) return DecisionScene('dummy scene for testing purposes', {'1': DecisionOption('Loot', loot_scene)}) # Load loot scene event_utils.post_scene_change(start_scene()) event_utils.simulate_key_press('1') # The scene machine only changes scenes during a game tick EventManager.post(BasicEvents.TICK) assert isinstance(_get_active_controller(), InventoryController) inv_scene = _get_current_scene() assert isinstance(inv_scene, InventoryScene) # Player selectes head 1 and moves it to head slot head_mod_1 = ground_mods[0] event_utils.simulate_mouse_click( *_mod_slot_position(head_mod_1, inv_scene)) assert head_mod_1 is inv_scene.selected_mod head_slot_position = _slot_header_position(SlotTypes.HEAD, inv_scene) event_utils.simulate_mouse_click(*(head_slot_position)) assert inv_scene.selected_mod is None assert head_mod_1 in chassis.all_mods() # Player selects head_mod_2 and tries to move it to head slot, but it is # full so it remains on the ground. assert chassis.slot_capacities[SlotTypes.HEAD] == 1 head_mod_2 = ground_mods[1] head_2_slot_pos = _mod_slot_position(head_mod_2, inv_scene) event_utils.simulate_mouse_click(*head_2_slot_pos) assert head_mod_2 is inv_scene.selected_mod head_slot_position = _slot_header_position(SlotTypes.HEAD, inv_scene) event_utils.simulate_mouse_click(*(head_slot_position)) assert inv_scene.selected_mod is None assert head_mod_2 not in chassis.all_mods() assert head_2_slot_pos == _mod_slot_position(head_mod_2, inv_scene) # Player moves leg mod to storage leg_mod = ground_mods[3] assert leg_mod not in chassis.all_mods() event_utils.simulate_mouse_click(*_mod_slot_position(leg_mod, inv_scene)) assert leg_mod is inv_scene.selected_mod storage_slot_pos = _slot_header_position(SlotTypes.STORAGE, inv_scene) event_utils.simulate_mouse_click(*storage_slot_pos) assert inv_scene.selected_mod is None assert leg_mod in chassis.all_mods() assert leg_mod in chassis.mods_in_slot(SlotTypes.STORAGE) # Player tries to move chest mod to arms slot, so nothing happens chest_mod = ground_mods[2] assert SlotTypes.CHEST in chest_mod.valid_slots() assert chest_mod not in chassis.all_mods() event_utils.simulate_mouse_click(*_mod_slot_position(chest_mod, inv_scene)) assert inv_scene.selected_mod is chest_mod arms_slot_pos = _slot_header_position(SlotTypes.ARMS, inv_scene) event_utils.simulate_mouse_click(*arms_slot_pos) assert inv_scene.selected_mod is None assert chest_mod not in chassis.all_mods()
from uuid import uuid4 from main import BomberApe from screens.game import Game from controllers.player import Player from controllers.game import Game as Server ba = BomberApe() game = Game(name='game') ba.view.add_widget(game) # Example game setup server = Server() server.load_map('content/maps/new.map') player = Player(uuid4(), server) server.players = { player.uid: player, } euid = uuid4() server.spawn_entity('gorilla', euid, player.uid) game.start(player) ### ba.run()