def test_swap_swaps_two_tiles(): output_sheet = [[2, 1]] test_map = Map([[1, 2]], [[1, 2]], [[1, 2]], [[1, 2]], validate=False) test_map.swap((0, 0), (1, 0), Map.FOREGROUND_SHEET_INDEX) test_map.swap((0, 0), (1, 0), Map.CHARACTER_SHEET_INDEX) test_map.swap((0, 0), (1, 0), Map.PATH_SHEET_INDEX) test_map.swap((0, 0), (1, 0), Map.BACKGROUND_SHEET_INDEX) assert test_map.tile_sheets == ( list(output_sheet), list(output_sheet), list(output_sheet), list(output_sheet), )
def test_handle_keystrokes_calls_each_player_controlled_objects_interaction( keypress_mock): pco1 = Mock(spec=PlayerControlledObject) pco2 = Mock(spec=PlayerControlledObject) pco1.sprite_location = "sprite.png" pco2.sprite_location = "sprite.png" keypress_mock.side_effect = generate_keypress_pattern( ["w"], convert_to_keystroke=True) test_map = Map( [[pco1, pco2]], [[Mock(), Mock()]], [[Mock(), Mock()]], [[Mock(), Mock()]], validate=False, ) game = BaseGame(initial_map=test_map, camera_width=3, camera_height=1, camera_x=1, camera_y=1) engine = Engine(game=game) try: engine.start() except StopIteration: # When keypresses have run out, a StopIteration exception will be thrown by mock. # This exception will be used to tell when the necessary testing is done, and to # stop the game, hence, pass. pass assert pco1.player_interaction.called assert pco2.player_interaction.called
def test_initializing_engine_with_game_loads_player_controlled_objects( event_queue): pco1 = PlayerControlledObject("sprite.png") pco2 = PlayerControlledObject("sprite.png") test_map = Map( [[pco1, pco2]], [[Mock(), Mock()]], [[Mock(), Mock()]], [[Mock(), Mock()]], validate=False, ) game = BaseGame(initial_map=test_map, camera_width=3, camera_height=1, camera_x=1, camera_y=1) engine = Engine(game=game) event_queue.return_value = [DummyEvent(pygame.QUIT)] try: engine.start() except StopIteration: # When keypresses have run out, a StopIteration exception will be thrown by mock. # This exception will be used to tell when the necessary testing is done, and to # stop the game, hence, pass. pass assert pco1 in engine.context.player_controlled_objects assert pco2 in engine.context.player_controlled_objects assert engine.context.player_controlled_objects[pco1] == (0, 0) assert engine.context.player_controlled_objects[pco2] == (1, 0)
def test_player_controlled_objects_contains_all_player_controlled_objects_and_their_coordinates(): pco1 = PlayerControlledObject("sprite.png") pco2 = PlayerControlledObject("sprite.png") test_map = Map([[pco1, pco2]], [[1, 2]], [[1, 2]], [[1, 2]], validate=False) player_controlled_objects = test_map.player_controlled_objects assert (pco1, 0, 0) in player_controlled_objects assert (pco2, 1, 0) in player_controlled_objects assert len(player_controlled_objects) == 2
def test_invalid_sheet_data_raises_exception( foreground_sheet_valid, path_sheet_valid, background_sheet_valid, character_sheet_valid, ): foreground_sheet = generate_sheet(foreground_sheet_valid) path_sheet = generate_sheet(path_sheet_valid) background_sheet = generate_sheet(background_sheet_valid) character_sheet = generate_sheet(character_sheet_valid) with pytest.raises(Map.InvalidObjectInSheetException): Map(foreground_sheet, path_sheet, background_sheet, character_sheet)
def test_invalid_sheet_type_raises_exception( foreground_sheet_valid, path_sheet_valid, background_sheet_valid, character_sheet_valid, ): foreground_sheet = generate_sheet(None, foreground_sheet_valid) path_sheet = generate_sheet(None, path_sheet_valid) background_sheet = generate_sheet(None, background_sheet_valid) character_sheet = generate_sheet(None, character_sheet_valid) with pytest.raises(ValueError): Map(foreground_sheet, path_sheet, background_sheet, character_sheet)
def test_load_player_controlled_objects_loads_them(): game = BaseGame(initial_map=generate_valid_map()) pco1 = PlayerControlledObject("sprite.png") pco2 = PlayerControlledObject("sprite.png") load_map = Map([[pco1, pco2]], [[0, 0]], [[0, 0]], [[0, 0]], validate=False) game.load_player_controlled_objects(load_map) assert pco1 in game.player_controlled_objects assert pco2 in game.player_controlled_objects assert game.player_controlled_objects[pco1] == (0, 0) assert game.player_controlled_objects[pco2] == (1, 0)
def test_tile_sheets_is_list_of_specific_sheets(): foreground_sheet = generate_sheet(True) path_sheet = generate_sheet(True) background_sheet = generate_sheet(True) character_sheet = generate_sheet(True) test_map = Map(foreground_sheet, path_sheet, background_sheet, character_sheet) tiles = test_map.tile_sheets assert tiles[Map.FOREGROUND_SHEET_INDEX] == foreground_sheet assert tiles[Map.PATH_SHEET_INDEX] == path_sheet assert tiles[Map.CHARACTER_SHEET_INDEX] == character_sheet assert tiles[Map.BACKGROUND_SHEET_INDEX] == background_sheet
def test_change_map_loads_new_player_controlled_objects_and_unloads_previous_ones(): pco1 = PlayerControlledObject("sprite.png") pco2 = PlayerControlledObject("sprite.png") pco3 = PlayerControlledObject("sprite.png") pco4 = PlayerControlledObject("sprite.png") go_mock = Mock() go_mock.sprite_location = "sprite.png" test_map = Map( [[pco1, pco2]], [[go_mock, go_mock]], [[go_mock, go_mock]], [[go_mock, go_mock]], validate=False, ) alt_map = Map( [[pco3, pco4]], [[go_mock, go_mock]], [[go_mock, go_mock]], [[go_mock, go_mock]], validate=False, ) game = BaseGame(initial_map=test_map) game.object_images["sprite.png"] = Mock() # Setup a mock to load the image as. game.register_map("alt", alt_map) game.change_map("alt") assert pco1 not in game.player_controlled_objects assert pco2 not in game.player_controlled_objects assert game.player_controlled_objects[pco3] == (0, 0) assert game.player_controlled_objects[pco4] == (1, 0) assert len(game.player_controlled_objects) == 2
def start_game(): logging.getLogger().setLevel(logging.INFO) if len(sys.argv) > 1 and sys.argv[1] == "-m": test_obj_one = GameObject( sprite_location="thegame/resources/TestBoxOne.png") test_obj_two = GameObject( sprite_location="thegame/resources/TestBoxTwo.png") exit_obj = ExitObject(sprite_location="thegame/resources/ExitBox.png") top_sheet = [[ test_obj_one.clone() if not 8 <= _ <= 11 else None for _ in range(20) ] for _ in range(20)] character_sheet = [[None for _ in range(20)] for _ in range(20)] path_sheet = [[None for _ in range(20)] for _ in range(20)] bottom_sheet = [[test_obj_two.clone() for _ in range(20)] for _ in range(20)] character_sheet[10][10] = PlayerCharacter( sprite_location="thegame/resources/PC.png") top_sheet[6][10] = exit_obj main_map = Map(top_sheet, character_sheet, path_sheet, bottom_sheet) game = TheGame( initial_map=main_map, screen_width=300, screen_height=300, base_sprite_height=10, base_sprite_width=10, camera_width=30, camera_height=30, camera_x=10, camera_y=10, ) else: game = TheGame(main_menu=BaseMenu( menu_image_location="thegame/resources/main_menu.png")) game.active_menu.register_interactive_zone(173, 228, 334, 303, exit_button_interaction) game_engine = Engine(game) game_engine.start()
def test_player_character_facing_south_can_interact_with_igos(): igo = Mock(spec=InteractiveGameObject) pc = PlayerCharacter(sprite_location="sprite.png", facing_direction=PlayerCharacter.SOUTH) # Create a map of 1x2 sheets as we only need to look south. character_sheet = [[pc], [igo]] sheet = [[None], [None]] keystrokes = ["\r"] test_map = Map(sheet, character_sheet, sheet, sheet) game = BaseGame(initial_map=test_map) game.register_player_controlled_object(pc, 0, 0) pc.player_interaction(keystrokes, game) assert igo.interact.called
def test_unload_active_map_sets_game_object_sprites_to_none(): go = GameObject("sprite.png") base_sheet = [[go.clone(), go.clone()], [go.clone(), go.clone()]] base_map = Map( list(base_sheet), list(base_sheet), list(base_sheet), list(base_sheet) ) game = BaseGame(initial_map=base_map) game.unload_active_map() assert base_sheet[0][0]._loaded_sprite is None assert base_sheet[0][1]._loaded_sprite is None assert base_sheet[1][0]._loaded_sprite is None assert base_sheet[1][1]._loaded_sprite is None
def test_load_active_maps_sets_game_object_sprites(): go = GameObject("sprite.png") base_sheet = [[go.clone(), go.clone()], [go.clone(), go.clone()]] base_map = Map( list(base_sheet), list(base_sheet), list(base_sheet), list(base_sheet) ) sprite_mock = Mock() game = BaseGame(initial_map=base_map) game.object_images["sprite.png"] = sprite_mock # Usually the engine handles calling this, but there is no # engine running so we need to call this manually. game.load_active_map() assert base_sheet[0][0].get_sprite().image is sprite_mock assert base_sheet[0][1].get_sprite().image is sprite_mock assert base_sheet[1][0].get_sprite().image is sprite_mock assert base_sheet[1][1].get_sprite().image is sprite_mock
def test_camera_fov_produces_borders_and_reduces_map(camera_width, camera_height, output_sheet): # camera_x,y = (1, 1). This positions the camera in the center of the 3x3 map, no matter what the # actual camera width and height are. camera = Camera(camera_width=camera_width, camera_height=camera_height, camera_x=1, camera_y=1) sheet = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] test_map = Map(list(sheet), list(sheet), list(sheet), list(sheet), validate=False) fov = camera.get_camera_fov(test_map) assert fov == [output_sheet, output_sheet, output_sheet, output_sheet]
def test_onscreen_sprites_are_correctly_added(sprite_group_mock, event_get_mock, image_load_mock): event_get_mock.return_value = [DummyEvent(pygame.QUIT)] sprite_mock = Mock(name="sprite_mock") convert_alpha_mock = Mock() convert_alpha_mock.convert_alpha.return_value = sprite_mock image_load_mock.return_value = convert_alpha_mock go1 = GameObject("sprite.png") go2 = GameObject("sprite2.png") go3 = GameObject("sprite3.png") sheet = [ [go1.clone(), go2.clone(), go3.clone()], [go3.clone(), go1.clone(), go2.clone()], [go2.clone(), go3.clone(), go1.clone()], ] game_map = Map(list(sheet), list(sheet), list(sheet), list(sheet), validate=False) game = BaseGame(initial_map=game_map, camera_width=3, camera_height=3, camera_x=1, camera_y=1) engine = Engine(game) engine.start() call_args = sprite_group_mock.call_args # Ignore empty calls. assert all(call_arg[0].image is sprite_mock for call_arg in call_args if len(call_arg) >= 1)
def test_swap_done_on_sheet_greater_than_3_raises_value_error(): test_map = Map([[1, 2]], [[1, 2]], [[1, 2]], [[1, 2]], validate=False) with pytest.raises(ValueError): test_map.swap((0, 1), (0, 1), Map.BACKGROUND_SHEET_INDEX + 1)
def test_swap_done_on_sheet_less_than_0_raises_value_error(): test_map = Map([[1, 2]], [[1, 2]], [[1, 2]], [[1, 2]], validate=False) with pytest.raises(ValueError): test_map.swap((0, 1), (0, 1), Map.FOREGROUND_SHEET_INDEX - 1)