Ejemplo n.º 1
0
    def test_tools_function(self):
        b = engine.Board()
        g = engine.Game()
        g.player = constants.NO_PLAYER
        self.assertIsNone(g.add_board(1, b))
        self.assertIsNone(g.change_level(1))
        self.assertIsNone(
            g.add_npc(1, board_items.NPC(value=10, inventory_space=1), 1, 1))
        self.assertIsNone(
            b.place_item(board_items.Door(value=10, inventory_space=1), 1, 2))
        self.assertIsNone(
            b.place_item(board_items.Wall(value=10, inventory_space=1), 1, 3))
        self.assertIsNone(
            b.place_item(
                board_items.GenericStructure(value=10, inventory_space=1), 1,
                4))
        self.assertIsNone(
            b.place_item(
                board_items.GenericActionableStructure(value=10,
                                                       inventory_space=1),
                1,
                5,
            ))
        self.assertIsNone(
            b.place_item(
                board_items.Door(value=10,
                                 inventory_space=1,
                                 sprixel=core.Sprixel("#")),
                2,
                2,
            ))
        self.assertIsNone(
            b.place_item(board_items.Treasure(value=10, inventory_space=1), 1,
                         6))
        with self.assertRaises(base.PglInvalidTypeException):
            g.neighbors("2")

        with self.assertRaises(base.PglInvalidTypeException):
            g.neighbors(2, "crash")

        g.object_library.append(board_items.NPC())
        self.assertIsNone(
            g.save_board(1, "test-pygamelib.engine.Game.lvl1.json"))
        with self.assertRaises(base.PglInvalidTypeException):
            g.save_board("1", "test-pygamelib.engine.Game.lvl1.json")
        with self.assertRaises(base.PglInvalidTypeException):
            g.save_board(1, 1)
        with self.assertRaises(base.PglInvalidLevelException):
            g.save_board(11, "test-pygamelib.engine.Game.lvl1.json")
        self.assertIsInstance(
            g.load_board("test-pygamelib.engine.Game.lvl1.json", 1),
            engine.Board)
        self.assertEqual(g._string_to_constant("UP"), constants.UP)
        self.assertEqual(g._string_to_constant("DOWN"), constants.DOWN)
        self.assertEqual(g._string_to_constant("LEFT"), constants.LEFT)
        self.assertEqual(g._string_to_constant("RIGHT"), constants.RIGHT)
        self.assertEqual(g._string_to_constant("DRUP"), constants.DRUP)
        self.assertEqual(g._string_to_constant("DRDOWN"), constants.DRDOWN)
        self.assertEqual(g._string_to_constant("DLUP"), constants.DLUP)
        self.assertEqual(g._string_to_constant("DLDOWN"), constants.DLDOWN)
Ejemplo n.º 2
0
    def test_item(self):
        self.board = pgl_engine.Board(name="test_board",
                                      size=[10, 10],
                                      player_starting_position=[5, 5])
        self.placed_item = pgl_board_items.BoardItem()

        self.board.place_item(self.placed_item, 1, 1)
        self.returned_item = self.board.item(1, 1)
        self.assertEqual(self.placed_item, self.returned_item)

        with self.assertRaises(
                pgl_base.PglOutOfBoardBoundException) as excinfo:
            self.board.item(15, 15)
        self.assertTrue(
            "out of the board boundaries" in str(excinfo.exception))
        sprix = gfx_core.Sprixel(bg_color=gfx_core.Color(45, 45, 45))
        sprix.is_bg_transparent = True
        self.board.place_item(
            pgl_board_items.Door(sprixel=gfx_core.Sprixel(
                bg_color=gfx_core.Color(15, 15, 15))),
            5,
            5,
        )

        i = pgl_board_items.NPC(sprixel=sprix)
        self.assertIsNone(self.board.place_item(i, 5, 5))
        self.assertIsNone(
            self.board.place_item(
                pgl_board_items.ComplexNPC(
                    base_item_type=pgl_board_items.Movable),
                8,
                8,
                8,
            ))
        self.assertIsNone(self.board.place_item(pgl_board_items.Tile(), 8, 2))
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            self.board.place_item(1, 1, 1)
        with self.assertRaises(pgl_base.PglOutOfBoardBoundException):
            self.board.place_item(i, 100, 100)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            self.board.remove_item(1)
        # Let's try to break things
        j = pgl_board_items.NPC()
        j.store_position(2, 2)
        with self.assertRaises(pgl_base.PglException) as e:
            self.board.remove_item(j)
        self.assertEqual(e.exception.error, "invalid_item")
        self.assertTrue(self.board.remove_item(i))
        b = pgl_engine.Board()
        i = pgl_board_items.ComplexNPC(sprite=gfx_core.Sprite(
            sprixels=[[gfx_core.Sprixel("#"),
                       gfx_core.Sprixel("#")]]))
        self.assertIsNone(b.place_item(i, 5, 5, 0))
        self.assertTrue(b.remove_item(i))
Ejemplo n.º 3
0
    def test_play_all(self):
        self.item = pgl_board_items.NPC(model="-o-", name="Dancer")
        self.animation = gfx_core.Animation(parent=self.item,
                                            refresh_screen=self.redraw,
                                            display_time=0.5)
        self.animation.add_frame("-o-")
        self.animation.add_frame("\\o-")
        self.animation.add_frame("\\o\\")
        self.animation.add_frame("|o|")
        self.assertTrue(self.animation.play_all())
        self.animation.pause()
        self.assertFalse(self.animation.play_all())
        self.animation.stop()
        self.assertFalse(self.animation.play_all())
        self.animation = gfx_core.Animation(parent="breaking",
                                            refresh_screen=self.redraw,
                                            display_time=0.5)
        with self.assertRaises(Exception) as context:
            self.animation.play_all()
        self.assertTrue(
            "needs to be a sub class of BoardItem" in str(context.exception))

        self.animation = gfx_core.Animation(parent=self.item,
                                            refresh_screen="breaking",
                                            display_time=0.5)
        with self.assertRaises(Exception) as context:
            self.animation.play_all()
        self.assertTrue("needs to be a callback function reference" in str(
            context.exception))
Ejemplo n.º 4
0
 def test_next_frame(self):
     self.item = pgl_board_items.NPC(model="-o-", name="Dancer")
     self.animation = gfx_core.Animation(parent=self.item,
                                         refresh_screen=self.redraw,
                                         display_time=0.5)
     self.animation.add_frame("-o-")
     self.animation.add_frame("\\o-")
     self.animation.add_frame("\\o\\")
     self.animation.add_frame("|o|")
     self.assertEqual(self.animation.next_frame(), "\\o-")
     self.animation.pause()
     self.assertEqual(self.animation.next_frame(), "\\o-")
     self.animation.stop()
     self.assertIsNone(self.animation.next_frame())
     self.animation.start()
     self.animation.next_frame()
     self.animation.next_frame()
     self.animation.next_frame()
     self.animation.reset()
     self.animation.auto_replay = False
     self.animation.next_frame()
     self.animation.next_frame()
     self.animation.next_frame()
     self.animation.next_frame()
     self.animation.next_frame()
     self.assertEqual(self.animation._frame_index, 3)
     self.animation.parent = "This is going to break!"
     with self.assertRaises(Exception) as context:
         self.animation.next_frame()
     self.assertTrue(
         "needs to be a sub class of BoardItem" in str(context.exception))
Ejemplo n.º 5
0
 def test_stop(self):
     self.item = pgl_board_items.NPC(model="-o-", name="Dancer")
     self.animation = gfx_core.Animation(parent=self.item,
                                         refresh_screen=self.redraw,
                                         display_time=0.5)
     self.animation.stop()
     self.assertEqual(self.animation.state, pgl_constants.STOPPED)
Ejemplo n.º 6
0
 def test_create_animation(self):
     self.item = pgl_board_items.NPC(model="-o-", name="Dancer")
     self.animation = gfx_core.Animation(parent=self.item,
                                         refresh_screen=self.redraw,
                                         display_time=0.5)
     self.assertEqual(self.item.name, self.animation.parent.name)
     a = gfx_core.Animation(
         animated_object=self.item,
         refresh_screen=self.redraw,
         display_time=0.5,
         initial_index=2,
     )
     self.assertEqual(a._initial_index, 2)
     a.reset()
     self.assertEqual(a._initial_index, a._frame_index)
     col = gfx_core.SpriteCollection()
     col.add(gfx_core.Sprite(name="s1"))
     col.add(gfx_core.Sprite(name="s2"))
     i = pgl_board_items.ComplexNPC(sprite=col["s1"])
     a = gfx_core.Animation(frames=col, parent=i)
     i.animation = a
     self.assertIsInstance(a, gfx_core.Animation)
     self.assertEqual(len(a.frames), 2)
     # I shouldn't test that here but I'm tired of writting test!
     self.assertIsInstance(a.next_frame(), gfx_core.Sprite)
Ejemplo n.º 7
0
 def test_add_frame(self):
     self.item = pgl_board_items.NPC(model="-o-", name="Dancer")
     self.animation = gfx_core.Animation(parent=self.item,
                                         refresh_screen=self.redraw,
                                         display_time=0.5)
     self.animation.add_frame("\\o-")
     with self.assertRaises(Exception) as context:
         self.animation.add_frame(2)
     self.assertTrue("must be a string" in str(context.exception))
Ejemplo n.º 8
0
 def test_pathfinder_serialization(self):
     a = actuators.PathFinder(parent=board_items.NPC())
     a.add_waypoint(1, 2)
     data = a.serialize()
     self.assertIsNotNone(data)
     self.assertEqual(data["waypoints"], [(1, 2)])
     al = actuators.PathFinder.load(data)
     self.assertEqual(al.waypoints, [(1, 2)])
     self.assertEqual(a.state, al.state)
     self.assertEqual(a.destination, al.destination)
Ejemplo n.º 9
0
 def test_current_frame(self):
     self.item = pgl_board_items.NPC(model="-o-", name="Dancer")
     self.animation = gfx_core.Animation(parent=self.item,
                                         refresh_screen=self.redraw,
                                         display_time=0.5)
     self.animation.add_frame("-o-")
     self.animation.add_frame("\\o-")
     self.animation.add_frame("\\o\\")
     self.animation.add_frame("|o|")
     self.assertEqual(self.animation.current_frame(), "-o-")
     self.animation.next_frame()
     self.assertEqual(self.animation.current_frame(), "\\o-")
Ejemplo n.º 10
0
 def test_npc_management(self):
     b = engine.Board()
     g = engine.Game()
     self.assertIsNone(g.add_board(1, b))
     npc = board_items.NPC(step=None)
     self.assertIsNone(g.add_npc(1, npc))
     with self.assertRaises(base.PglInvalidTypeException):
         g.add_npc(1, board_items.NPC(step=None), "1")
     with self.assertRaises(base.PglInvalidTypeException):
         g.add_npc(1, board_items.NPC(step=None), None, "1")
     with self.assertRaises(base.PglInvalidTypeException):
         g.add_npc(1, board_items.NPC(step=None), 1, "1")
     with self.assertRaises(base.PglInvalidTypeException):
         g.add_npc(1, board_items.NPC(step=None), "1", 1)
     with self.assertRaises(base.PglInvalidTypeException):
         g.add_npc(1, 1)
     with self.assertRaises(base.PglInvalidTypeException):
         g.add_npc("1", board_items.NPC())
     self.assertIsNone(g.actuate_npcs(1))
     g.mode = constants.MODE_RT
     self.assertIsNone(g.actuate_npcs(1))
     with self.assertRaises(base.PglInvalidLevelException):
         g.actuate_npcs(99)
     with self.assertRaises(base.PglInvalidTypeException):
         g.actuate_npcs("1")
     g.remove_npc(1, npc)
Ejemplo n.º 11
0
 def test_with_sprixel(self):
     i = pgl_board_items.NPC(sprixel=gfx_core.Sprixel())
     i.animation = gfx_core.Animation(parent=i)
     i.animation.add_frame(gfx_core.Sprixel("-"))
     i.animation.add_frame(gfx_core.Sprixel("+"))
     self.assertIsInstance(i.animation, gfx_core.Animation)
     self.assertEqual(len(i.animation.frames), 2)
     # I shouldn't test that here but I'm tired of writting test!
     self.assertIsInstance(i.animation.next_frame(), gfx_core.Sprixel)
     i.animation.frames[1] = pgl_base.Vector2D()
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         i.animation.next_frame()
         i.animation.next_frame()
Ejemplo n.º 12
0
 def test_get_objects(self):
     b = pgl_engine.Board(
         name="test_board", size=[10, 10], player_starting_position=[0, 0],
     )
     for i in range(1, 4):
         b.place_item(pgl_board_items.NPC(name=f"mover{i}", type="mover"), 0, i)
     for i in range(1, 4):
         b.place_item(pgl_board_items.Wall(name=f"static{i}", type="static"), i, 0)
     ret = b.get_immovables(type="static")
     self.assertEqual(len(ret), 3)
     self.assertEqual(len(ret), len(b.get_immovables()))
     ret = b.get_movables(type="static")
     self.assertEqual(len(ret), 0)
     ret = b.get_movables(type="mover")
     self.assertEqual(len(ret), 3)
     self.assertEqual(len(ret), len(b.get_movables()))
Ejemplo n.º 13
0
 def test_remove_frame(self):
     self.item = pgl_board_items.NPC(model="-o-", name="Dancer")
     self.animation = gfx_core.Animation(parent=self.item,
                                         refresh_screen=self.redraw,
                                         display_time=0.5)
     self.animation.add_frame("-o-")
     self.animation.add_frame("\\o-")
     self.animation.add_frame("\\o\\")
     self.animation.add_frame("|o|")
     self.animation.add_frame("/o/")
     self.animation.add_frame("-o/")
     with self.assertRaises(Exception) as context:
         self.animation.remove_frame(999)
     self.assertTrue("out of range" in str(context.exception))
     self.assertEqual(self.animation.remove_frame(0), "-o-")
     self.animation.next_frame()
     self.animation.next_frame()
     self.assertEqual(self.animation.remove_frame(2), "|o|")
     self.assertEqual(self.animation.current_frame(), "\\o\\")
     self.assertEqual(self.animation.next_frame(), "/o/")
     with self.assertRaises(gfx_core.base.PglInvalidTypeException):
         self.animation.remove_frame("999")
Ejemplo n.º 14
0
 def test_display_around(self):
     i = pgl_board_items.NPC()
     self.board.place_item(i, 2, 2)
     self.board.display_around(i, 2, 2)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         self.board.display_around(1, 2, 2)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         self.board.display_around(i, "2", 2)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         self.board.display_around(i, 2, "2")
     self.assertIsNone(self.board.display_around(i, 20, 20))
     self.assertIsNone(self.board.display_around(i, 2, 20))
     self.assertIsNone(self.board.display_around(i, 20, 2))
     self.board.item(2, 3).model = "#"
     self.assertIsNone(self.board.display_around(i, 2, 2))
     self.board.clear_cell(2, 2)
     i = pgl_board_items.ComplexNPC()
     self.board.place_item(i, 2, 2)
     self.assertIsNone(self.board.display_around(i, 2, 2))
     b = pgl_engine.Board(parent=pgl_engine.Game())
     self.assertIsInstance(b, pgl_engine.Board)
     b.place_item(i, 2, 2)
     self.assertIsNone(self.board.display_around(i, 2, 2))
     self.assertIsNone(self.board.display())
Ejemplo n.º 15
0
    def test_projectile_management(self):
        def _hit(p, t, ex):
            if len(ex) > 0:
                ex[0].stop()

        def _fake_hit(p, t, ex):
            pass

        def _upd(g, i, dt):
            pass

        b = engine.Board()
        g = engine.Game(user_update=_upd)
        g.player = constants.NO_PLAYER
        self.assertIsNone(g.add_board(1, b))
        g.change_level(1)
        p = board_items.Projectile(hit_model="*",
                                   hit_callback=_fake_hit,
                                   callback_parameters=[g])
        p.actuator = None
        p.step = None
        self.assertIsNone(g.add_projectile(1, p, 1, 1))
        self.assertIsNone(g.add_projectile(1, board_items.Projectile(), 1,
                                           100))
        b.place_item(board_items.Wall(), 5, 5)
        b.place_item(board_items.Wall(), 1, 3)
        p2 = board_items.Projectile(hit_model="*",
                                    hit_callback=_fake_hit,
                                    callback_parameters=[g])
        p2.set_direction(constants.LEFT)
        g.add_projectile(1, p2, 1, 5)
        g.add_projectile(
            1,
            board_items.Projectile(hit_model="*",
                                   hit_callback=_hit,
                                   callback_parameters=[g]),
            8,
            1,
        )
        g.add_projectile(
            1,
            board_items.Projectile(
                hit_model="*",
                hit_callback=_fake_hit,
                callback_parameters=[g],
                range=3,
                is_aoe=True,
            ),
            9,
            1,
        )
        self.assertIsNone(
            g.add_projectile(1, board_items.Projectile(hit_callback=_hit), 5,
                             5))
        self.assertIsNone(
            g.add_projectile(
                1, board_items.Projectile(hit_callback=_hit, is_aoe=True), 5,
                5))
        with self.assertRaises(base.PglInvalidTypeException):
            g.add_projectile(1, board_items.Projectile(), "1")
        with self.assertRaises(base.PglInvalidTypeException):
            g.add_projectile(1, board_items.Projectile(), None, "1")
        with self.assertRaises(base.PglInvalidTypeException):
            g.add_projectile(1, board_items.Projectile(), 1, "1")
        with self.assertRaises(base.PglInvalidTypeException):
            g.add_projectile(1, board_items.Projectile(), "1", 1)
        with self.assertRaises(base.PglInvalidTypeException):
            g.add_projectile(1, 1)
        with self.assertRaises(base.PglInvalidTypeException):
            g.add_projectile("1", board_items.NPC())
        self.assertIsNone(g.actuate_projectiles(1))
        g.mode = constants.MODE_RT
        g.start()
        self.assertIsNone(g.actuate_projectiles(1))
        with self.assertRaises(base.PglInvalidLevelException):
            g.actuate_projectiles(99)
        with self.assertRaises(base.PglInvalidTypeException):
            g.actuate_projectiles("1")
        g.run()
Ejemplo n.º 16
0
    def test_tools_function(self):
        b = engine.Board()
        g = engine.Game()
        g.player = constants.NO_PLAYER
        self.assertIsNone(g.add_board(1, b))
        self.assertIsNone(g.change_level(1))
        self.assertIsNone(
            g.add_npc(1, board_items.NPC(value=10, inventory_space=1), 1, 1))
        tmp_npc = g.get_board(1).item(1, 1)
        tmp_npc.actuator = actuators.PathFinder(game=g,
                                                actuated_object=tmp_npc)
        tmp_npc.actuator.set_destination(2, 5)
        tmp_npc.actuator.find_path()
        self.assertIsNone(
            b.place_item(board_items.Door(value=10, inventory_space=1), 1, 2))
        self.assertIsNone(
            b.place_item(board_items.Wall(value=10, inventory_space=1), 1, 3))
        self.assertIsNone(
            b.place_item(
                board_items.GenericStructure(value=10, inventory_space=1), 1,
                4))
        self.assertIsNone(
            b.place_item(
                board_items.GenericActionableStructure(value=10,
                                                       inventory_space=1),
                1,
                5,
            ))
        self.assertIsNone(
            b.place_item(
                board_items.Door(value=10,
                                 inventory_space=1,
                                 sprixel=core.Sprixel("#")),
                2,
                2,
            ))
        self.assertIsNone(
            b.place_item(board_items.Treasure(value=10, inventory_space=1), 1,
                         6))
        with self.assertRaises(base.PglInvalidTypeException):
            g.neighbors("2")

        with self.assertRaises(base.PglInvalidTypeException):
            g.neighbors(2, "crash")

        g.object_library.append(board_items.NPC())
        self.assertIsNone(
            g.save_board(1, "test-pygamelib.engine.Game.lvl1.json"))
        with self.assertRaises(base.PglInvalidTypeException):
            g.save_board("1", "test-pygamelib.engine.Game.lvl1.json")
        with self.assertRaises(base.PglInvalidTypeException):
            g.save_board(1, 1)
        with self.assertRaises(base.PglInvalidLevelException):
            g.save_board(11, "test-pygamelib.engine.Game.lvl1.json")
        self.assertIsInstance(
            g.load_board("test-pygamelib.engine.Game.lvl1.json", 1),
            engine.Board)
        self.assertEqual(g._string_to_constant("UP"), constants.UP)
        self.assertEqual(g._string_to_constant("DOWN"), constants.DOWN)
        self.assertEqual(g._string_to_constant("LEFT"), constants.LEFT)
        self.assertEqual(g._string_to_constant("RIGHT"), constants.RIGHT)
        self.assertEqual(g._string_to_constant("DRUP"), constants.DRUP)
        self.assertEqual(g._string_to_constant("DRDOWN"), constants.DRDOWN)
        self.assertEqual(g._string_to_constant("DLUP"), constants.DLUP)
        self.assertEqual(g._string_to_constant("DLDOWN"), constants.DLDOWN)

        with self.assertRaises(base.PglInvalidTypeException):
            g.delete_level()
        with self.assertRaises(base.PglInvalidLevelException):
            g.delete_level(42)
        g.delete_level(1)
        g.delete_all_levels()
        self.assertIsNone(g.current_board())
        bi = board_items.Door(
            value=10,
            inventory_space=0,
            pickable=False,
            overlappable=True,
            restorable=True,
        )
        obj = engine.Game._ref2obj(bi.serialize())
        self.assertIsInstance(obj, board_items.Door)
        bi = board_items.Treasure(
            value=10,
            inventory_space=0,
            pickable=False,
            overlappable=True,
            restorable=True,
        )
        obj = engine.Game._ref2obj(bi.serialize())
        self.assertIsInstance(obj, board_items.Treasure)
        bi = board_items.GenericActionableStructure(
            value=10,
            inventory_space=0,
            pickable=False,
            overlappable=True,
            restorable=True,
        )
        obj = engine.Game._ref2obj(bi.serialize())
        self.assertIsInstance(obj, board_items.GenericActionableStructure)
        bi = board_items.NPC(
            value=10,
            inventory_space=10,
            pickable=False,
            overlappable=True,
            restorable=True,
        )
        bi.actuator = actuators.PathActuator(path=[constants.UP])
        obj = engine.Game._ref2obj(bi.serialize())
        self.assertIsInstance(obj, board_items.NPC)
        bi.actuator = actuators.PatrolActuator(path=[constants.UP])
        obj = engine.Game._ref2obj(bi.serialize())
        self.assertIsInstance(obj.actuator, actuators.PatrolActuator)
Ejemplo n.º 17
0
 def test_pathfinder_bfs(self):
     npc = board_items.NPC()
     b = engine.Board()
     g = engine.Game()
     g.player = board_items.Player()
     g.add_board(1, b)
     g.add_npc(1, npc, 5, 5)
     g.change_level(1)
     actuators.PathFinder(actuated_object=npc)
     npc.actuator = actuators.PathFinder(parent=npc, game=g, circle_waypoints=False)
     with self.assertRaises(engine.base.PglInvalidTypeException):
         actuators.PathFinder(
                 parent=npc, 
                 game=g,
                 circle_waypoints=False,
                 algorithm="constants.ALGO_BFS"
             )
     npc.actuator.set_destination(2, 2)
     npc.actuator.find_path()
     self.assertTrue(len(npc.actuator.current_path()) > 0)
     with self.assertRaises(engine.base.PglInvalidTypeException):
         npc.actuator.set_destination("2", 2)
     npc.actuator.actuated_object = None
     with self.assertRaises(engine.base.PglException) as e:
         npc.actuator.find_path()
         self.assertEqual(e.error, "actuated_object is not defined")
     npc.actuator.actuated_object = board_items.Door()
     with self.assertRaises(engine.base.PglException) as e:
         npc.actuator.find_path()
         self.assertEqual(e.error, "actuated_object not a Movable object")
     npc.actuator.actuated_object = board_items.Door()
     npc.actuator.actuated_object = npc
     npc.actuator.destination = None
     with self.assertRaises(engine.base.PglException) as e:
         npc.actuator.find_path()
         self.assertEqual(e.error, "destination is not defined")
     b.place_item(board_items.Wall(), 2, 2)
     npc.actuator.set_destination(2, 2)
     self.assertEqual(npc.actuator.find_path(), [])
     # These tests are a recipe of how to NOT do things...
     npc.actuator.destination = (None, None)
     self.assertEqual(npc.actuator.next_move(), constants.NO_DIR)
     npc.actuator.set_destination(5, 5)
     npc.actuator._current_path = []
     npc.actuator.next_move()
     npc.actuator.set_destination(2, 5)
     npc.actuator._current_path = []
     nm = npc.actuator.next_move()
     self.assertEqual(nm, constants.UP)
     npc.actuator.add_waypoint(5, 6)
     npc.actuator.add_waypoint(6, 6)
     npc.actuator.add_waypoint(5, 4)
     npc.actuator.add_waypoint(4, 6)
     nm = None
     while nm != constants.NO_DIR:
         nm = npc.actuator.next_move()
         b.move(npc, nm, npc.step)
     with self.assertRaises(engine.base.PglInvalidTypeException):
         npc.actuator.add_waypoint(5, "6")
     with self.assertRaises(engine.base.PglInvalidTypeException):
         npc.actuator.add_waypoint("5", 6)
     npc.actuator.clear_waypoints()
     self.assertEqual(npc.actuator.next_waypoint(), (None, None))
     npc.actuator.clear_waypoints()
     npc.actuator.destination = (None, None)
     npc.actuator.add_waypoint(10, 10)
     npc.actuator.add_waypoint(12, 15)
     self.assertEqual(npc.actuator.destination, (10, 10))
     self.assertEqual(npc.actuator.next_waypoint(), (10, 10))
     self.assertEqual(npc.actuator.next_waypoint(), (12, 15))
     self.assertEqual(npc.actuator.next_waypoint(), (None, None))
     npc.actuator.circle_waypoints = True
     self.assertEqual(npc.actuator.next_waypoint(), (10, 10))
     with self.assertRaises(engine.base.PglInvalidTypeException):
         npc.actuator.remove_waypoint(10, "10")
     with self.assertRaises(engine.base.PglInvalidTypeException):
         npc.actuator.remove_waypoint("10", 10)
     with self.assertRaises(engine.base.PglException) as e:
         npc.actuator.remove_waypoint(30, 30)
         self.assertEqual(e.error, "invalid_waypoint")
     self.assertIsNone(npc.actuator.remove_waypoint(10, 10))
Ejemplo n.º 18
0
    player_starting_position=[10, 20],
)
lvl2 = pgl_engine.Board(
    name="Level_2",
    size=[40, 20],
    ui_border_left=graphics.WHITE_SQUARE,
    ui_border_right=graphics.WHITE_SQUARE,
    ui_border_top=graphics.WHITE_SQUARE,
    ui_border_bottom=graphics.WHITE_SQUARE,
    ui_board_void_cell=graphics.BLACK_SQUARE,
    player_starting_position=[0, 0],
)

game = pgl_engine.Game(name="HAC Game")
p = board_items.Player(model=sprite_player["right"], name="Nazbrok")
npc1 = board_items.NPC(model=sprite_npc, name="Bad guy 1", step=1)
# Test of the PathActuator
npc1.actuator = actuators.PathActuator(path=[
    cst.UP,
    cst.UP,
    cst.UP,
    cst.UP,
    cst.UP,
    cst.UP,
    cst.UP,
    cst.UP,
    cst.RIGHT,
    cst.RIGHT,
    cst.RIGHT,
    cst.RIGHT,
    cst.DOWN,
Ejemplo n.º 19
0
 def test_npc(self):
     npc = board_items.NPC(actuator=board_items.actuators.RandomActuator(), step=1)
     self.assertFalse(npc.pickable())
     self.assertFalse(npc.overlappable())
     self.assertFalse(npc.has_inventory())
Ejemplo n.º 20
0
    def test_screen_buffer(self):
        sprites_panda = SpriteCollection.load_json_file("tests/panda.spr")
        b = engine.Board(size=[20, 20])
        s = engine.Screen(50, 50)
        # Because CircleCI return a console with no size (most probably because we are
        # not attached to any terminal), we need to make sure that the partial display
        # tests work in that environment too
        screen_width = 0
        screen_height = 0
        if s.width <= 0:
            screen_width = 50
        else:
            screen_width = s.width
        if s.height <= 0:
            screen_height = 50
        else:
            screen_height = s.height
        self.assertEqual(s.vcenter, int(s.height / 2))
        self.assertEqual(s.hcenter, int(s.width / 2))
        b.place_item(board_items.Tile(sprite=sprites_panda["panda"]), 0, 0)
        self.assertIsInstance(b.render_cell(1, 1), Sprixel)
        b.item(19, 19).model = "@"
        b.item(19, 19).sprixel = None
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        self.assertEqual(b.render_cell(19, 19), Sprixel())
        b.place_item(board_items.Door(), 19, 19)
        b.place_item(
            board_items.Door(sprixel=Sprixel(
                "*", Color(125, 125, 0), is_bg_transparent=False)),
            19,
            19,
        )
        b.place_item(
            board_items.Door(sprixel=Sprixel("#", is_bg_transparent=True)), 19,
            19)
        b.place_item(
            board_items.NPC(sprixel=Sprixel("$", is_bg_transparent=True)), 19,
            19)
        self.assertEqual(b.layers(19, 19), 4)
        b.place_item(
            board_items.BoardItemVoid(sprixel=Sprixel(is_bg_transparent=True)),
            19, 19)
        b.place_item(
            board_items.BoardItemVoid(sprixel=Sprixel(is_bg_transparent=True)),
            19, 19)
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        b._clean_layers(19, 19)
        self.assertEqual(b.layers(19, 19), 3)
        b._clean_layers(18, 19)
        self.assertEqual(b.layers(18, 19), 1)
        with self.assertRaises(base.PglOutOfBoardBoundException):
            b.render_cell(50, 50)
        self.assertIsNone(s.clear_buffers())
        self.assertIsNone(s.clear_screen_buffer())
        self.assertTrue(s._is_dirty)
        self.assertTrue(functions.pgl_isinstance(s.buffer, "numpy.ndarray"))
        self.assertIsNone(s.update())
        b = engine.Board(size=[1, 1])
        b.place_item(board_items.Wall(model="##"), 0, 0)
        self.assertIsNone(s.place("test", 0, 0))
        self.assertIsNone(s.place(b, 1, 0))
        self.assertIsInstance(s.get(1, 0), engine.Board)
        t = base.Text("test 2")
        self.assertIsNone(s.place(t, 2, 0))
        self.assertIsNone(s.place(sprites_panda["panda"], 0, 5))
        self.assertIsNone(s.place(TB(), 3, 0))
        self.assertIsNone(s.place(board_items.BoardItem(model="##"), 10, 0))
        self.assertIsNone(
            s.place(
                board_items.Tile(sprite=Sprite(sprixels=[
                    [Sprixel("##"), Sprixel("##")],
                    [Sprixel("##"), Sprixel("##")],
                ])),
                4,
                0,
            ))
        self.assertIsNone(
            s.place(
                Sprite(sprixels=[
                    [Sprixel("##"), Sprixel("##")],
                    [Sprixel("###"), Sprixel("##")],
                ]),
                8,
                0,
            ))
        s.force_render()
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(None, 0, 0)
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(1, 0, 0)
        with self.assertRaises(base.PglException):
            s.place(TB(), 400, 0)
        with self.assertRaises(base.PglException):
            s.place(TB(), 0, 400)
        s.force_update()
        t.text = "update"
        self.assertIsNone(
            s.place(sprites_panda["panda"], screen_height - 2,
                    screen_width - 2))
        self.assertIsNone(s.place("test", 1, screen_width - 2))
        s.update()
        self.assertIsNone(s.render())  # Should not render
        self.assertFalse(s.need_rendering)
        s.trigger_rendering()
        self.assertTrue(s.need_rendering)
        # Now testing partial display
        camera = board_items.Camera()
        camera.row = 0
        camera.column = 0

        b = engine.Board(
            size=[screen_width * 2, screen_height * 2],
            enable_partial_display=True,
            partial_display_viewport=[
                int(screen_height / 2) - 1,
                int(screen_width / 2) - 1,
            ],
            partial_display_focus=camera,
            DISPLAY_SIZE_WARNINGS=False,
        )
        for row in range(0, b.height):
            for col in range(0, b.width):
                b.place_item(
                    board_items.Wall(sprixel=Sprixel(
                        " ", Color(row * 4, col, int((row + col) / 2))), ),
                    row,
                    col,
                )
        self.assertIsNone(s.place(b, 0, 0, 2))
        s.trigger_rendering()
        self.assertIsNone(s.update())
        b.partial_display_viewport = [
            int(screen_height / 2) - 1,
            int(screen_width / 2) - 1,
        ]
        camera.row = b.height - 1
        camera.column = b.width - 1
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.update())
        camera = board_items.Tile(
            sprite=Sprite(sprixels=[[Sprixel("+"), Sprixel("+")],
                                    [Sprixel("+"), Sprixel("+")]]))
        b.partial_display_focus = camera
        # Please never do that in real life...
        camera.pos = [1, 1]
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.render())
        self.assertIsNone(s.update())
        # This will succeed but the str type cannot benefit from deferred rendering.
        self.assertIsNone(s.place("test delete", 0, 0, 2))
        self.assertIsNone(s.update())
        self.assertIsNone(s.delete(0, 0))
        self.assertIsNone(s.update())
        self.assertIsNone(
            functions.render_string_to_buffer(
                "hello",
                s._display_buffer,
                0,
                0,
                s._display_buffer.shape[0],
                s._display_buffer.shape[1],
            ))
        # This test has nothing to do here... but I'm lazy.
        self.assertEqual(5, functions.clamp(5, 0, 10))
        self.assertEqual(0, functions.clamp(-5, 0, 10))
        self.assertEqual(10, functions.clamp(15, 0, 10))
        # This one either and it's even worst: it's going to disappear!
        t = base.Text("this is a text\non multiple lines")
        t.render_to_buffer(
            s._display_buffer,
            s._display_buffer.shape[0] - 1,
            s._display_buffer.shape[1] - 5,
            s._display_buffer.shape[0],
            s._display_buffer.shape[1],
        )
        s.update()
        t = base.Text(
            "this is a text",
            Color(0, 0, 0),
            Color(255, 255, 255),
            font=Font("8bits"),
        )
        t.render_to_buffer(
            s._display_buffer,
            s._display_buffer.shape[0] - 1,
            s._display_buffer.shape[1] - 5,
            s._display_buffer.shape[0],
            s._display_buffer.shape[1],
        )
        s.update()