def test_screen_flip_vert(self): spr = gfx_core.Sprite( sprixels=[ [ gfx_core.Sprixel.cyan_rect(), gfx_core.Sprixel.red_rect(), gfx_core.Sprixel.green_rect(), ], [ gfx_core.Sprixel.yellow_rect(), gfx_core.Sprixel.blue_rect(), gfx_core.Sprixel.white_rect(), ], ] ) spr_flipped = spr.flip_vertically() self.assertEqual(spr.sprixel(1, 0), gfx_core.Sprixel.yellow_rect()) self.assertEqual(spr_flipped.sprixel(1, 0), gfx_core.Sprixel.cyan_rect()) spr = gfx_core.Sprite( sprixels=[ [gfx_core.Sprixel("▄"), gfx_core.Sprixel("▄")], [gfx_core.Sprixel("▀"), gfx_core.Sprixel("▀")], ] ) spr_flipped = spr.flip_vertically() self.assertEqual(spr.sprixel(0, 0).model, "▀") self.assertIsNone(spr.set_transparency(True))
def test_sprixel_cmp(self): s1 = gfx_core.Sprixel() s2 = gfx_core.Sprixel() s3 = gfx_core.Sprixel("m") self.assertEqual(s1, s2) self.assertNotEqual(s1, s3) self.assertFalse(s1 == s3) self.assertFalse(s1 != s2)
def test_sprixel_cmp(self): s1 = gfx_core.Sprixel() s2 = gfx_core.Sprixel() s3 = gfx_core.Sprixel("m") self.assertEqual(s1, s2) self.assertNotEqual(s1, s3) self.assertFalse(s1 == s3) self.assertFalse(s1 != s2) self.assertEqual(s1.__eq__("nope"), NotImplemented)
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))
def test_sprixel_create(self): sprix = gfx_core.Sprixel() self.assertEqual(sprix.model, "") self.assertEqual(sprix.bg_color, "") self.assertEqual(sprix.fg_color, "") sprix = gfx_core.Sprixel("m", "b", "f") self.assertEqual(sprix.model, "m") self.assertEqual(sprix.bg_color, "b") self.assertEqual(sprix.fg_color, "f") with self.assertRaises(gfx_core.base.PglInvalidTypeException): gfx_core.Sprixel(False, False, False)
def test_message_dialog(self): conf = ui.UiConfig.instance(game=self.game) # Test when height is none and adpatative_height is False (it will be tuned on) md = ui.MessageDialog( [ "Test", "Message dialog", FakeText("fake"), pgl_base.Text("Another test"), core.Sprixel("!"), ], adaptive_height=False, config=conf, ) self.assertEqual(md.title, "") md.title = "test" self.assertEqual(md.title, "test") md.title = pgl_base.Text("test") self.assertEqual(md.title, "test") with self.assertRaises(pgl_base.PglInvalidTypeException): md.title = 42 self.game.screen.place(md, 0, 0) self.game.screen.update() md.add_line("test 2", constants.ALIGN_RIGHT) md.add_line("test 3", constants.ALIGN_CENTER) md.add_line(pgl_base.Text("test 4"), constants.ALIGN_RIGHT) md.add_line(pgl_base.Text("test 5"), constants.ALIGN_CENTER) self.game.screen.force_update() with self.assertRaises(pgl_base.PglInvalidTypeException): md.add_line(1, constants.ALIGN_CENTER) self.assertIs(type(md.height), int) # Now test with fixed height md = ui.MessageDialog( [ "Test", "Message dialog", FakeText("fake"), pgl_base.Text("Another test"), core.Sprixel("!"), ], adaptive_height=False, height=10, config=conf, ) self.assertEqual(md.height, 10) md.height = 6 self.assertEqual(md.height, 6) with self.assertRaises(pgl_base.PglInvalidTypeException): md.height = "6" md = ui.MessageDialog(title=pgl_base.Text("test"), config=conf) self.assertEqual(md.title, "test") with self.assertRaises(pgl_base.PglInvalidTypeException): ui.MessageDialog(title=42, config=conf)
def test_move_complex(self): def _act(p): p[0].assertEqual(p[1], 1) self.board = pgl_engine.Board( name="test_board", size=[10, 10], player_starting_position=[5, 5], ) i = pgl_board_items.ComplexNPC(sprite=gfx_core.Sprite( default_sprixel=gfx_core.Sprixel("*"))) g = pgl_engine.Game(mode=constants.MODE_RT) self.board.place_item(i, 1, 1) self.assertIsInstance(self.board.item(1, 1), pgl_board_items.ComplexNPC) self.assertIsNone(self.board.move(i, constants.RIGHT, 1)) i = pgl_board_items.ComplexPlayer(sprite=gfx_core.Sprite( default_sprixel=gfx_core.Sprixel("*"), sprixels=[[gfx_core.Sprixel("@"), gfx_core.Sprixel("@")]], )) self.board.place_item(i, 3, 1) self.assertIsInstance(self.board.item(3, 1), pgl_board_items.ComplexPlayer) self.board.place_item( pgl_board_items.GenericActionableStructure( action=_act, action_parameters=[self, 1]), i.row, i.column + i.width, ) self.assertIsNone(self.board.move(i, constants.RIGHT, 1)) self.board.place_item(pgl_board_items.Treasure(value=50), i.row + i.height, i.column) self.assertIsNone(self.board.move(i, constants.DOWN, 1)) self.assertEqual(i.inventory.value(), 50) i.parent = g i.dtmove = 0.0 self.assertIsNone(self.board.move(i, pgl_base.Vector2D(1, 0))) i.dtmove = 5.0 self.assertIsNone(self.board.move(i, pgl_base.Vector2D(1, 0))) with self.assertRaises(pgl_base.PglObjectIsNotMovableException): self.board.move(pgl_board_items.Immovable(), constants.DOWN, 1) g.mode = constants.MODE_TBT self.board.place_item(pgl_board_items.Door(), i.row, i.column + i.width) self.assertIsNone(self.board.move(i, constants.RIGHT, 1)) self.assertIsNone(self.board.move(i, constants.RIGHT, 2)) self.assertIsNone(self.board.move(i, constants.DOWN, 2)) with self.assertRaises(pgl_base.PglInvalidTypeException): self.board.move(i, constants.DOWN, "1") with self.assertRaises(pgl_base.PglInvalidTypeException): self.board.move(i, "constants.DOWN", 1)
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()
def test_move_simple(self): def _act(p): setattr(p[0], "test_callback", True) p[0].assertEqual(p[1], 1) i = pgl_board_items.Player(sprixel=gfx_core.Sprixel("*")) i.sprixel.is_bg_transparent = True b = pgl_engine.Board( name="test_board", size=[10, 10], player_starting_position=[0, 0], ) b.place_item(i, 0, 0) self.assertIsNone(b.move(i, constants.DOWN, 1)) self.assertIsNone(b.move(i, constants.UP, 1)) self.assertIsNone(b.move(i, constants.RIGHT, 1)) self.assertIsNone(b.move(i, constants.LEFT, 1)) self.assertIsNone(b.move(i, constants.DRDOWN, 1)) self.assertIsNone(b.move(i, constants.DRUP, 1)) self.assertIsNone(b.move(i, constants.DLDOWN, 1)) self.assertIsNone(b.move(i, constants.DLUP, 1)) self.assertIsNone(b.move(i, pgl_base.Vector2D(0, 0))) self.assertEqual(i.pos, [0, 0, 0]) setattr(self, "test_callback", False) b.place_item( pgl_board_items.GenericActionableStructure( action=_act, action_parameters=[self, 1]), 0, 1, ) self.assertIsNone(b.move(i, constants.RIGHT, 1)) self.assertIsNone(b.move(i, constants.RIGHT, 1)) self.assertTrue(getattr(self, "test_callback")) b.place_item(pgl_board_items.Treasure(value=50), i.row + 1, i.column) self.assertIsNone(b.move(i, constants.DOWN, 1)) self.assertEqual(i.inventory.value(), 50) b.place_item( pgl_board_items.Door(sprixel=gfx_core.Sprixel( bg_color=gfx_core.Color(45, 45, 45))), i.row + 1, i.column, ) b.place_item( pgl_board_items.Door(sprixel=gfx_core.Sprixel( bg_color=gfx_core.Color(45, 45, 45))), i.row + 2, i.column, ) self.assertIsNone(b.move(i, constants.DOWN, 1)) self.assertIsNone(b.move(i, constants.DOWN, 1)) self.assertIsNone(b.clear_cell(i.row, i.column))
def draw_box(game, row, column, height, width, title=""): scr = game.screen scr.place( f"{graphics.BoxDrawings.LIGHT_ARC_DOWN_AND_RIGHT}" f"{graphics.BoxDrawings.LIGHT_HORIZONTAL*round(width/2-1-len(title)/2)}" f"{title}" f"{graphics.BoxDrawings.LIGHT_HORIZONTAL*round(width/2-1-len(title)/2)}" f"{graphics.BoxDrawings.LIGHT_HORIZONTAL*(width-round(width/2-1-len(title)/2)*2-len(title)-2)}" # noqa: E501 f"{graphics.BoxDrawings.LIGHT_ARC_DOWN_AND_LEFT}", row, column, ) vert_sprix = core.Sprixel(graphics.BoxDrawings.LIGHT_VERTICAL) for r in range(1, height - 1): scr.place(vert_sprix, row + r, column) scr.place( vert_sprix, row + r, column + width - 1, ) scr.place( f"{graphics.BoxDrawings.LIGHT_ARC_UP_AND_RIGHT}" f"{graphics.BoxDrawings.LIGHT_HORIZONTAL*(width-2)}" f"{graphics.BoxDrawings.LIGHT_ARC_UP_AND_LEFT}", row + height - 1, column, )
def test_sprixel_update(self): sprix = gfx_core.Sprixel() self.assertEqual(sprix.model, "") self.assertEqual(sprix.length, 0) self.assertIsNone(sprix.bg_color) self.assertIsNone(sprix.fg_color) sprix.model = "@" self.assertEqual(sprix.model, "@") self.assertIsNone(sprix.bg_color) self.assertIsNone(sprix.fg_color) sprix.bg_color = gfx_core.Color.from_ansi("\x1b[48;2;160;26;23m") self.assertEqual(sprix.model, "@") self.assertEqual( sprix.bg_color, gfx_core.Color.from_ansi("\x1b[48;2;160;26;23m") ) self.assertIsNone(sprix.fg_color) sprix.fg_color = gfx_core.Color.from_ansi("\x1b[38;2;120;18;15m") self.assertEqual(sprix.model, "@") self.assertEqual( sprix.bg_color, gfx_core.Color.from_ansi("\x1b[48;2;160;26;23m") ) self.assertEqual( sprix.fg_color, gfx_core.Color.from_ansi("\x1b[38;2;120;18;15m") ) with self.assertRaises(gfx_core.base.PglInvalidTypeException): sprix.bg_color = 1 with self.assertRaises(gfx_core.base.PglInvalidTypeException): sprix.fg_color = 1
def test_sprite_create_empty(self): spr = gfx_core.Sprite() self.assertEqual(spr.size[0], 2) self.assertEqual(spr.size[1], 2) self.assertEqual(spr.width, 2) self.assertEqual(spr.height, 2) self.assertEqual(spr.sprixel(1, 1), gfx_core.Sprixel()) self.assertEqual(spr.__repr__(), "\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m") with self.assertRaises(gfx_core.base.PglInvalidTypeException): spr.sprixel("crash", 1) with self.assertRaises(gfx_core.base.PglInvalidTypeException): spr.sprixel(1, "crash") with self.assertRaises(gfx_core.base.PglException) as e: spr.sprixel(1, 10) self.assertEqual(e.exception.error, "out_of_sprite_boundaries") self.assertTrue(type(spr.sprixel(1)) is list) with self.assertRaises(gfx_core.base.PglException) as e: spr.sprixel(10, 1) self.assertEqual(e.exception.error, "out_of_sprite_boundaries") with self.assertRaises(gfx_core.base.PglInvalidTypeException): spr.set_sprixel(1, "crash", "bork") with self.assertRaises(gfx_core.base.PglException) as e: spr.set_sprixel(1, 10, "bork") self.assertEqual(e.exception.error, "out_of_sprite_boundaries") with self.assertRaises(gfx_core.base.PglInvalidTypeException): spr.set_sprixel(1, 1, "bork")
def test_box(self): conf = ui.UiConfig.instance(game=self.game) conf.borderless_dialog = False b = ui.Box(20, 10, "test box", conf, True, core.Sprixel(" "), constants.ALIGN_LEFT) self.game.screen.place(b, 0, 0) self.game.screen.update() b.config = ui.UiConfig.instance() self.assertIsInstance(b.config, ui.UiConfig) self.assertEqual(b.title, "test box") b.title = "test 2" self.assertEqual(b.title, "test 2") with self.assertRaises(pgl_base.PglInvalidTypeException): b.config = 2 self.assertEqual(b.width, 20) b.width = 30 self.assertEqual(b.width, 30) with self.assertRaises(pgl_base.PglInvalidTypeException): b.width = "20" self.assertEqual(b.height, 10) b.height = 20 self.assertEqual(b.height, 20) with self.assertRaises(pgl_base.PglInvalidTypeException): b.height = "20" b.title = "" self.assertEqual(b.title, "") self.game.screen.place(b, 0, 0) self.game.screen.update() with self.assertRaises(pgl_base.PglInvalidTypeException): b.title = 2 self.game.screen.place(b, self.game.screen.height - 5, self.game.screen.width - 5) self.game.screen.update()
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)
def __init__(self, **kwargs): board_items.Movable.__init__(self, **kwargs) self.size = [1, 1] self.name = str(uuid.uuid4()) self.type = "base_particle" self.sprixel = gfx_core.Sprixel(graphics.GeometricShapes.BULLET) if "bg_color" in kwargs: self.sprixel.bg_color = kwargs["bg_color"] if "fg_color" in kwargs: self.sprixel.fg_color = kwargs["fg_color"] if "model" in kwargs: self.sprixel.model = kwargs["model"] self.directions = [ base.Vector2D.from_direction(constants.UP, 1), base.Vector2D.from_direction(constants.DLUP, 1), base.Vector2D.from_direction(constants.DRUP, 1), ] self.lifespan = 5 if "velocity" in kwargs and isinstance(kwargs["velocity"], base.Vector2D): self.velocity = kwargs["velocity"] else: self.velocity = base.Vector2D() if "acceleration" in kwargs and isinstance(kwargs["acceleration"], base.Vector2D): self.acceleration = kwargs["acceleration"] else: self.acceleration = base.Vector2D() for item in ["lifespan", "sprixel", "name", "type", "directions"]: if item in kwargs: setattr(self, item, kwargs[item])
def test_serialization(self): class Bork(pgl_board_items.Wall): def __init__(self, **kwargs): super().__init__(**kwargs) b = pgl_engine.Board(ui_board_void_cell_sprixel=gfx_core.Sprixel( " ", gfx_core.Color(0, 0, 0))) b.place_item(pgl_board_items.Wall(), 2, 2) b.place_item(TestItem(), 4, 4) data = b.serialize() self.assertIsNotNone(data) self.assertIsNone(pgl_engine.Board.load(None)) bl = pgl_engine.Board.load(data) self.assertEqual(b.player_starting_position, bl.player_starting_position) self.assertEqual(b.name, bl.name) self.assertEqual(b.ui_board_void_cell, bl.ui_board_void_cell) self.assertIsInstance(b.item(2, 2), pgl_board_items.Wall) self.assertEqual(b.item(2, 2).model, bl.item(2, 2).model) data["map_data"]["(2, 2, 0)"]["object"] = "<'=bork.bork'>" with self.assertRaises(SyntaxError): pgl_engine.Board.load(data) b.place_item(Bork(), 6, 6) with self.assertRaises(SyntaxError): pgl_engine.Board.load(b.serialize())
def test_complexnpc(self): sprite = gfx_core.Sprite( name="test_sprite", sprixels=[ [ gfx_core.Sprixel(" ", gfx_core.Color(255, 0, 0)), gfx_core.Sprixel(" ", gfx_core.Color(255, 0, 0)), ] ], ) n = board_items.ComplexNPC(name="Test NPC", sprite=sprite) n.actuator = actuators.RandomActuator() data = n.serialize() self.assertEqual(n.name, data["name"]) self.assertEqual(n.sprite.name, data["sprite"]["name"]) n2 = board_items.ComplexNPC.load(data) self.assertEqual(n.name, n2.name)
def test_player(self): p = board_items.Player(inventory=board_items.engine.Inventory()) self.assertFalse(p.pickable()) self.assertTrue(p.has_inventory()) sprite = gfx_core.Sprite( name="test_sprite", sprixels=[ [ gfx_core.Sprixel(" ", gfx_core.Color(255, 0, 0)), gfx_core.Sprixel(" ", gfx_core.Color(255, 0, 0)), ] ], ) n = board_items.ComplexPlayer(name="Test Player", sprite=sprite) n.actuator = actuators.RandomActuator() data = n.serialize() self.assertEqual(n.name, data["name"]) self.assertEqual(n.sprite.name, data["sprite"]["name"]) n2 = board_items.ComplexPlayer.load(data) self.assertEqual(n.name, n2.name)
def test_custom_boardItem(self): self.boardItem = board_items.BoardItem( name="test_boardItem", item_type="test_type", pos=[10, 10], model="test_model", ) self.assertEqual(self.boardItem.name, "test_boardItem") self.assertEqual(self.boardItem.type, "test_type") self.assertEqual(self.boardItem.pos, [10, 10]) self.assertEqual(self.boardItem.model, "test_model") self.assertEqual(self.boardItem.__repr__(), "test_model\x1b[0m") self.assertIsNone(self.boardItem.display()) self.assertIn("'model' = 'test_model'", self.boardItem.debug_info()) self.assertEqual(self.boardItem.position_as_vector().row, 10) self.assertEqual(self.boardItem.position_as_vector().column, 10) self.assertEqual(self.boardItem.row, 10) self.assertEqual(self.boardItem.column, 10) self.assertEqual(self.boardItem.width, 1) self.assertEqual(self.boardItem.height, 1) bi = board_items.BoardItem( name="test_boardItem", item_type="test_type", pos=[10, 10], model="test_model", ) self.assertTrue(self.boardItem.collides_with(bi)) bi.store_position(8, 9) self.assertFalse(self.boardItem.collides_with(bi)) with self.assertRaises(Exception) as context: self.boardItem.collides_with(12) self.assertAlmostEqual(self.boardItem.distance_to(bi), 2.23606797749979) self.assertTrue("require a BoardItem as parameter" in str(context.exception)) with self.assertRaises(Exception) as context: self.boardItem.distance_to(12) self.assertTrue("require a BoardItem as parameter" in str(context.exception)) bi = board_items.BoardItem(sprixel=gfx_core.Sprixel("-")) self.assertEqual(bi.__str__(), gfx_core.Sprixel("-").__repr__())
def test_sprixel_create(self): sprix = gfx_core.Sprixel() self.assertEqual(sprix.model, "") self.assertIsNone(sprix.bg_color) self.assertIsNone(sprix.fg_color) sprix = gfx_core.Sprixel("m", gfx_core.Color(0, 0, 0), gfx_core.Color(1, 1, 1)) self.assertEqual(sprix.model, "m") self.assertEqual(sprix.bg_color, gfx_core.Color(0, 0, 0)) self.assertEqual(sprix.fg_color, gfx_core.Color(1, 1, 1)) t = pgl_base.Console.instance() self.assertEqual( sprix.__repr__(), "".join([ t.on_color_rgb(0, 0, 0), t.color_rgb(1, 1, 1), "m", "\x1b[0m" ]), ) with self.assertRaises(gfx_core.base.PglInvalidTypeException): gfx_core.Sprixel(False, gfx_core.Color(0, 0, 0), gfx_core.Color(1, 1, 1)) with self.assertRaises(gfx_core.base.PglInvalidTypeException): gfx_core.Sprixel("False", "nope", gfx_core.Color(1, 1, 1)) with self.assertRaises(gfx_core.base.PglInvalidTypeException): gfx_core.Sprixel("False", gfx_core.Color(0, 0, 0), "nope") sprix = gfx_core.Sprixel(is_bg_transparent=True) self.assertTrue(sprix.is_bg_transparent)
def test_sanity_ui_board_void_cell_sprixel(self): with self.assertRaises(pgl_base.PglException) as context: self.board = pgl_engine.Board(name="test_board", size=[10, 10], ui_board_void_cell_sprixel=[]) self.assertEqual(context.error, "SANITY_CHECK_KO") b = pgl_engine.Board( name="test_board", size=[90, 90], DISPLAY_SIZE_WARNINGS=True, ui_board_void_cell_sprixel=gfx_core.Sprixel(), ) self.assertIsInstance(b, pgl_engine.Board) self.assertIsNone(b.display()) self.board.item(0, 2).model = "#" self.assertIsNone(self.board.display())
def test_create_board(self): self.board = pgl_engine.Board( name="test_board", size=[10, 10], player_starting_position=[5, 5], ui_borders="*", ) self.assertEqual(self.board.name, "test_board") self.assertEqual(self.board.ui_border_bottom, "*") ret = self.board.__str__() self.assertIsNotNone(ret) b = pgl_engine.Board( name="test_board_sprixel", size=[10, 10], player_starting_position=[5, 5], ui_board_void_cell_sprixel=gfx_core.Sprixel(), ) vc = b.generate_void_cell() self.assertIsInstance(vc.sprixel, gfx_core.Sprixel)
def test_sprixel_update(self): sprix = gfx_core.Sprixel() self.assertEqual(sprix.model, "") self.assertEqual(sprix.bg_color, "") self.assertEqual(sprix.fg_color, "") sprix.model = "@" self.assertEqual(sprix.model, "@") self.assertEqual(sprix.bg_color, "") self.assertEqual(sprix.fg_color, "") sprix.bg_color = "\x1b[48;2;160;26;23m" self.assertEqual(sprix.model, "@") self.assertEqual(sprix.bg_color, "\x1b[48;2;160;26;23m") self.assertEqual(sprix.fg_color, "") sprix.fg_color = "\x1b[38;2;120;18;15m" self.assertEqual(sprix.model, "@") self.assertEqual(sprix.bg_color, "\x1b[48;2;160;26;23m") self.assertEqual(sprix.fg_color, "\x1b[38;2;120;18;15m") with self.assertRaises(gfx_core.base.PglInvalidTypeException): sprix.bg_color = 1 with self.assertRaises(gfx_core.base.PglInvalidTypeException): sprix.fg_color = 1
def __init__(self, model=None, bg_color=None, fg_color=None, acceleration=None, velocity=None, lifespan=None, directions=None, **kwargs): super().__init__(**kwargs) # NOTE: this cannot be done anymore for BoardItems # self.size = [1, 1] self.name = str(uuid.uuid4()) self.type = "base_particle" self.sprixel = core.Sprixel(graphics.GeometricShapes.BULLET) if bg_color is not None and isinstance(bg_color, core.Color): self.sprixel.bg_color = bg_color if fg_color is not None and isinstance(fg_color, core.Color): self.sprixel.fg_color = fg_color if model is not None: self.sprixel.model = model self.directions = [ base.Vector2D.from_direction(constants.UP, 1), base.Vector2D.from_direction(constants.DLUP, 1), base.Vector2D.from_direction(constants.DRUP, 1), ] self.lifespan = 5 if velocity is not None and isinstance(velocity, base.Vector2D): self.velocity = velocity else: self.velocity = base.Vector2D() if acceleration is not None and isinstance(acceleration, base.Vector2D): self.acceleration = acceleration else: self.acceleration = base.Vector2D() if lifespan is not None: self.lifespan = lifespan if directions is not None and type(directions) is list: self.directions = directions
def test_screen_create1(self): spr = gfx_core.Sprite(size=[4, 4]) self.assertEqual(spr.size[0], 4) self.assertEqual(spr.size[1], 4) self.assertEqual(spr.sprixel(1, 1), gfx_core.Sprixel())
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)
def test_progressbar(self): conf = ui.UiConfig.instance(game=self.game) conf.borderless_dialog = False pb = ui.ProgressDialog( pgl_base.Text("test"), 0, 100, 20, core.Sprixel("="), core.Sprixel("-"), True, True, conf, ) pb.label = "test" self.assertEqual(pb.label, "test") pb.label = pgl_base.Text("Test text") self.assertEqual(pb.label, "Test text") with self.assertRaises(pgl_base.PglInvalidTypeException): pb.label = 2 self.assertEqual(pb.value, 0) pb.value = 2 self.assertEqual(pb.value, 2) with self.assertRaises(pgl_base.PglInvalidTypeException): pb.value = "3" self.assertEqual(pb.maximum, 100) pb.maximum = 20 self.assertEqual(pb.maximum, 20) with self.assertRaises(pgl_base.PglInvalidTypeException): pb.maximum = "3" self.game.screen.place(pb, 0, 0) self.game.screen.update() pb.value = 20 self.game.screen.force_update() self.game.screen.force_update() pb = ui.ProgressBar( 0, 100, 20, core.Sprixel("="), core.Sprixel("-"), conf, ) self.assertIsInstance(pb.config, ui.UiConfig) pb.config = ui.UiConfig.instance() self.assertIsInstance(pb.config, ui.UiConfig) with self.assertRaises(pgl_base.PglInvalidTypeException): pb.config = 3 self.assertIsInstance(pb.progress_marker, core.Sprixel) self.assertIsInstance(pb.empty_marker, core.Sprixel) pb.progress_marker = "#" self.assertEqual(pb.progress_marker, "#") pb.progress_marker = pgl_base.Text("#") self.assertEqual(pb.progress_marker, "#") with self.assertRaises(pgl_base.PglInvalidTypeException): pb.progress_marker = 3 pb.empty_marker = "#" self.assertEqual(pb.empty_marker, "#") pb.empty_marker = pgl_base.Text("#") self.assertEqual(pb.empty_marker, "#") with self.assertRaises(pgl_base.PglInvalidTypeException): pb.empty_marker = 3 pb.progress_marker = core.Sprixel("=") pb.empty_marker = core.Sprixel("-") self.assertIsInstance(pb.progress_marker, core.Sprixel) self.assertIsInstance(pb.empty_marker, core.Sprixel) self.assertEqual(pb.value, 0) with self.assertRaises(pgl_base.PglInvalidTypeException): pb.value = "10" self.assertEqual(pb.maximum, 100) pb.maximum = 10 self.assertEqual(pb.maximum, 10) with self.assertRaises(pgl_base.PglInvalidTypeException): pb.maximum = "10"
def test_tinting(self): sp = gfx_core.Sprite( sprixels=[ [ gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), ], [ gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), gfx_core.Sprixel( " ", gfx_core.Color(255, 255, 255), gfx_core.Color(255, 255, 255), ), ], ] ) spr = sp.tint(gfx_core.Color(0, 255, 0), 0.2) self.assertEqual(spr.sprixel(0, 0).bg_color.r, 204) self.assertEqual(spr.sprixel(0, 0).bg_color.g, 255) self.assertEqual(spr.sprixel(0, 0).bg_color.b, 204) # Original sprite should not be modified by tint self.assertEqual(sp.sprixel(0, 0).bg_color.r, 255) self.assertEqual(sp.sprixel(0, 0).bg_color.g, 255) self.assertEqual(sp.sprixel(0, 0).bg_color.b, 255) with self.assertRaises(gfx_core.base.PglInvalidTypeException): sp.tint(gfx_core.Color(0, 255, 0), 1.2) with self.assertRaises(gfx_core.base.PglInvalidTypeException): sp.tint(gfx_core.Color(0, 255, 0), -1.2) with self.assertRaises(gfx_core.base.PglInvalidTypeException): sp.modulate(gfx_core.Color(0, 255, 0), 1.2) with self.assertRaises(gfx_core.base.PglInvalidTypeException): sp.modulate(gfx_core.Color(0, 255, 0), -1.2) sp.modulate(gfx_core.Color(0, 255, 0), 0.2) # Original sprite should be modified by modulate self.assertEqual(sp.sprixel(0, 0).bg_color.r, 204) self.assertEqual(sp.sprixel(0, 0).bg_color.g, 255) self.assertEqual(sp.sprixel(0, 0).bg_color.b, 204)
print( base.Text.red_bright( "Your console/terminal needs to be at least 155 columns wide" f" to run that benchmark (current width is {g.screen.width})." ) ) exit() if g.screen.height < 65: print( base.Text.red_bright( "Your console/terminal needs to be at least 65 columns high" f" to run that benchmark (current height is {g.screen.height})." ) ) exit() g.player = board_items.Player(sprixel=core.Sprixel("@@", None, core.Color(0, 255, 255))) print("Loading resources: ", end="", flush=True) load_start = time.time() sprites = core.SpriteCollection.load_json_file("tests/pgl-benchmark.spr") panda = sprites["panda"] polus = sprites["Polus_Map"] load_stop = time.time() print("done") print("Generating Boards: ", end="", flush=True) gen_start = time.time() print("Benchmark Board ", end="", flush=True) g.load_board("hac-maps/benchmark.json", 1) print("[ok] ", end="", flush=True) g.change_level(1) print("High Definition Board ", end="", flush=True) polus_cam = board_items.Camera()
print( base.Text.red_bright( "Your console/terminal needs to be at least 155 columns wide" f" to run that benchmark (current width is {g.screen.width})." ) ) exit() if g.screen.height < 65: print( base.Text.red_bright( "Your console/terminal needs to be at least 65 columns high" f" to run that benchmark (current height is {g.screen.height})." ) ) exit() g.player = board_items.Player(sprixel=core.Sprixel("@@", None, core.Color(0, 255, 255))) print("Loading resources: ", end="", flush=True) load_start = time.time() sprites = core.SpriteCollection.load_json_file("tests/pgl-benchmark.spr") panda = board_items.Camera() panda_frames = [sprites["panda"], sprites["panda2"]] panda_frame_idx = 0 polus = sprites["Polus_Map"] # p4ter = polus.scale(0.1) load_stop = time.time() print("done") print("Generating Boards: ", end="", flush=True) gen_start = time.time() print("Benchmark Board ", end="", flush=True) g.load_board("hac-maps/benchmark.json", 1)