Exemplo n.º 1
0
def test_get_sprite_returns_sprite():

    go = GameObject("sprite.png")
    sprite_mock = Mock()
    go.register_loaded_sprite(sprite_mock)

    assert go.get_sprite() is sprite_mock
Exemplo n.º 2
0
def test_clone_produces_object_with_same_sprite_info():

    go = GameObject("sprite.png")
    clone = go.clone()

    assert clone.sprite_location == go.sprite_location
    assert clone.animation == go.animation
Exemplo n.º 3
0
def test_the_registered_sprite_can_be_loaded():

    go = GameObject("sprite.png")
    sprite_mock = Mock()
    go.register_loaded_sprite(sprite_mock)

    assert go._loaded_sprite is sprite_mock
Exemplo n.º 4
0
def test_set_sprite_position_sets_position_correctly():

    go = GameObject("sprite.png")
    sprite_mock = Mock()
    go.register_loaded_sprite(sprite_mock)

    go.set_sprite_position(pos_x=5, pos_y=4)

    assert sprite_mock.image.get_rect.called
    assert sprite_mock.rect.x == 5
    assert sprite_mock.rect.y == 4
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
def test_game_object_type_name_in_string():

    go = GameObject("spring.png")

    assert type(go).__name__ in str(go)
Exemplo n.º 9
0
def test_name_in_game_object_string_when_set():

    go = GameObject("sprite.png", name="Bob")

    assert "Bob" in str(go)
Exemplo n.º 10
0
def test_set_sprite_position_on_spriteless_object_throws_value_error():

    go = GameObject("sprite.png")

    with pytest.raises(AttributeError):
        go.set_sprite_position(pos_x=5, pos_y=4)
Exemplo n.º 11
0
def test_initialize_game_object_with_animation_sets_it():

    go = GameObject("sprite.png", animation="animation.gif")

    assert go.sprite_location == "sprite.png"
    assert go.animation == "animation.gif"
Exemplo n.º 12
0
def test_get_sprite_on_spriteless_object_throws_value_error():

    go = GameObject("sprite.png")

    with pytest.raises(AttributeError):
        go.get_sprite()
Exemplo n.º 13
0
def test_initialize_game_object_without_animation_set_it_to_none():

    go = GameObject("sprite.png")

    assert go.sprite_location == "sprite.png"
    assert go.animation is None
Exemplo n.º 14
0
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)