def test_board_management(self): b = engine.Board() g = engine.Game(player=board_items.Player()) self.assertIsNone(g.add_board(19, b)) self.assertIsNone(g.change_level(19)) self.assertIsNone(g.display_board()) # Test display_board but with partial display on. g.enable_partial_display = True g.partial_display_viewport = [2, 2] self.assertIsNone(g.display_board()) # Reset g = engine.Game() with self.assertRaises(base.PglInvalidTypeException): g.add_board(1, 1) with self.assertRaises(base.PglInvalidTypeException): g.add_board("1", 1) with self.assertRaises(base.PglInvalidTypeException): g.get_board("1") g.add_board(1, b) self.assertIsInstance(g.get_board(1), engine.Board) with self.assertRaises(base.PglInvalidLevelException): g.current_board() with self.assertRaises(base.PglException) as e: g.change_level(1) self.assertEqual(e.exception.error, "undefined_player") g.player = board_items.Player() self.assertIsNone(g.change_level(1)) self.assertIsNone(g.add_board(2, engine.Board())) self.assertIsNone(g.change_level(2)) self.assertIsNone(g.change_level(1)) with self.assertRaises(base.PglInvalidLevelException): g.change_level(99) with self.assertRaises(base.PglInvalidTypeException): g.change_level("2")
def test_board_management(self): b = engine.Board() g = engine.Game(player=constants.NO_PLAYER) self.assertIsNone(g.add_board(19, b)) self.assertIsNone(g.change_level(19)) self.assertIsNone(g.display_board()) # Reset g = engine.Game() with self.assertRaises(base.PglInvalidTypeException): g.add_board(1, 1) with self.assertRaises(base.PglInvalidTypeException): g.add_board("1", 1) with self.assertRaises(base.PglInvalidTypeException): g.get_board("1") self.assertIsInstance(g.get_board(1), engine.Board) with self.assertRaises(base.PglInvalidLevelException): g.current_board() with self.assertRaises(base.PglException) as e: g.change_level(1) self.assertEqual(e.error, "undefined_player") g.player = board_items.Player() self.assertIsNone(g.change_level(1)) self.assertIsNone(g.add_board(2, engine.Board())) self.assertIsNone(g.change_level(2)) self.assertIsNone(g.change_level(1)) with self.assertRaises(base.PglInvalidLevelException): g.change_level(99) with self.assertRaises(base.PglInvalidTypeException): g.change_level("2")
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)
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 test_menu(self): game = engine.Game() self.assertIsNone( game.add_menu_entry("main_menu", "d", "Go right", constants.RIGHT)) self.assertIsNone( game.add_menu_entry("main_menu", None, "-----------------")) self.assertIsNone( game.add_menu_entry("main_menu", "v", "Change game speed")) self.assertIsNone( game.add_menu_entry("destroy", None, "-----------------")) self.assertIsNone(game.delete_menu_category("destroy")) with self.assertRaises(base.PglInvalidTypeException): game.delete_menu_category(12) self.assertIsNone( game.update_menu_entry("main_menu", "d", "Go LEFT", constants.LEFT)) self.assertIsNone(game.get_menu_entry("main_menu_bork", "d")) self.assertEqual( game.get_menu_entry("main_menu", "d")["data"], constants.LEFT) self.assertIsNone( game.display_menu("main_menu", constants.ORIENTATION_HORIZONTAL, 1)) with self.assertRaises(base.PglException) as e: game.display_menu("main_menu_bork") self.assertEqual(e.error, "invalid_menu_category") self.assertIsNone(game.clear_screen())
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_move_simple(self): def _act(p): 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], ) g = pgl_engine.Game() 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]) 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)) 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=g.terminal.on_color_rgb(45, 45, 45)) ), i.row + 1, i.column, ) b.place_item( pgl_board_items.Door( sprixel=gfx_core.Sprixel(bg_color=g.terminal.on_color_rgb(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 test_run(self): def user_update_placeholder(g, i, dt): self.assertGreater(dt, 0) g.stop() g = engine.Game() with self.assertRaises(base.PglInvalidTypeException) as e: g.run() self.assertTrue("undefined" in e.message) g = engine.Game(user_update=1) with self.assertRaises(base.PglInvalidTypeException) as e: g.run() self.assertTrue("callable" in e.message) g = engine.Game(user_update=user_update_placeholder, mode=constants.MODE_RT) g.pause() self.assertIsNone( g.screen.display_line("testing the Game.run() mechanic.")) g.run() g = engine.Game(user_update=user_update_placeholder, mode=constants.MODE_RT) g.player = board_items.Player() g.pause() g.run()
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)) g = pgl_engine.Game() sprix = gfx_core.Sprixel(bg_color=g.terminal.on_color_rgb(45, 45, 45)) sprix.is_bg_transparent = True self.board.place_item( pgl_board_items.Door( sprixel=gfx_core.Sprixel(bg_color=g.terminal.on_color_rgb(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 ) ) 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.error, "invalid_item") self.assertTrue(self.board.remove_item(i)) b = pgl_engine.Board() i = pgl_board_items.ComplexNPC() self.assertIsNone(b.place_item(i, 5, 5)) self.assertTrue(b.remove_item(i))
def setUp(self): super().setUp() self.game = pgl_engine.Game() self.board = self.game.load_board("hac-maps/kneighbors.json", 1) self.game.player = pgl_board_items.Player(name="player") self.game.change_level(1) self.tree26 = self.board.item(2, 6) self.treasure38 = self.board.item(3, 8) self.tree39 = self.board.item(3, 9) self.wall45 = self.board.item(4, 5) self.wall46 = self.board.item(4, 6) self.wall47 = self.board.item(4, 7) self.wall57 = self.board.item(5, 7) self.npc77 = self.board.item(7, 7) self.treasure1212 = self.board.item(12, 12) self.tree1310 = self.board.item(13, 10) self.npc168 = self.board.item(16, 8)
def make_platform(b, row, column): psize = random.randint(2, 10) plateform = [] tmp_game = engine.Game() # Only because Game needs it, we don't care. tmp_game.player = board_items.Player() tmp_game.add_board(0, b) tmp_game.change_level(0) # print( # f"[d] make_platform at {row}, {column}, psize is {psize} column will be " # f"between {column} and {column + psize + 1}" # ) get_up = 0 # for i in range(column, column + psize + 1): for i in range(column - psize - 1, column): if i >= b.size[0]: break if not isinstance(b.item(row, i), board_items.BoardItemVoid): break if i in b.visited_columns: break # Check if we have other platforms around. # If yes moving the platform up. if get_up < 3: for e in tmp_game.neighbors(2, board_items.Door(pos=[row, i])): if e.type == "ground": get_up = 3 break if get_up < 4: for e in tmp_game.neighbors(1, board_items.Door(pos=[row, i])): if e.type == "ground": get_up = 4 break m = block_color() + " " + graphics.Style.RESET_ALL plateform.append( [board_items.Wall(model=m, item_type="platform"), row, i]) for i in plateform: b.place_item(i[0], i[1] - get_up, i[2]) if random.choice([True, False]): generate_treasure(b, i[1] - get_up - 1, i[2]) else: generate_trap(b, i[1] - get_up - 1, i[2]) b.visited_columns.append(i[2])
def test_config(self): g = engine.Game() self.assertIsNone(g.create_config("high_scores")) g.config("high_scores")["first_place"] = "Test" self.assertEqual(g.config("high_scores")["first_place"], "Test") self.assertIsNone( g.save_config("high_scores", "test-pygamelib.engine.Game.config.json")) with self.assertRaises(base.PglInvalidTypeException): g.save_config(None, "test-pygamelib.engine.Game.config.json") with self.assertRaises(base.PglInvalidTypeException): g.save_config("high_scores", None) with self.assertRaises(base.PglException) as e: g.save_config("Unknown", "test-pygamelib.engine.Game.config.json") self.assertEqual(e.error, "unknown section") # Don't do that... g._configuration = None g._configuration_internals = None g.load_config("test-pygamelib.engine.Game.config.json", "new_high_scores") self.assertIsNone(g.save_config("new_high_scores", None)) self.assertIsNone(g.save_config("new_high_scores", None, True))
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())
import examples_includes # noqa: F401 from pygamelib import engine, board_items, constants, base from pygamelib.assets import graphics import time ############################################################################### # # # IMPORTANT NOTE: # # # # The companion article for this tutorial code is at: # # https://astro.hyrul.es/guides/hac-game-lib/tutorial-02-player-movements.html# # # ############################################################################### mygame = engine.Game(name="Demo game") board1 = engine.Board( name="Level 1", ui_borders=graphics.Models.BRICK, ui_board_void_cell=graphics.BLACK_SQUARE, player_starting_position=[0, 0], ) board2 = engine.Board( name="Level 2", ui_borders=graphics.RED_SQUARE, ui_board_void_cell=graphics.BLACK_SQUARE, player_starting_position=[4, 4], ) mygame.player = board_items.Player(name="DaPlay3r", model=graphics.Models.UNICORN)
ui_border_bottom=graphics.WHITE_SQUARE, ui_board_void_cell=graphics.BLACK_SQUARE, 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,
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, ) print("pygamelib Screen Buffer benchmark\n") print("Loading game engine...", end="", flush=True) g_start = time.time() g = engine.Game(user_update=upd, mode=constants.MODE_TBT) g_stop = time.time() print("done") if g.screen.width < 155: 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})."
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()
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_game_create(self): def user_update_placeholder(p): pass self.assertIsInstance(engine.Game(user_update=user_update_placeholder), engine.Game)
# tile = board_items.Tile(sprite=spr) b = engine.Board(size=spr.size, ui_borders="", name=spr_id, DISPLAY_SIZE_WARNINGS=False) # b.place_item(tile, 0, 0) # Here we should just place a Tile but unfortunately the previous version of the # pygamelib do not actually save and load complex items but their components. # So for the moment we will convert each sprixel into a Door object. # TODO: add a --compatibility flag : when on we do that, when off we don't (i.e: we # place a Tile on the board) for sr in range(0, spr.height): for sc in range(0, spr.width): b.place_item(board_items.Door(sprixel=spr.sprixel(sr, sc)), sr, sc) # The game object is required to save boards... not a terribly good design... g = engine.Game() g.add_board(1, b) g.save_board(1, board_file) if not args.silent: print("done", flush=True) if args.show and not args.silent and sprite_valid: if spr.width <= base.Console.instance().width: print(spr) else: print( base.Text.yellow_bright( "The generated sprite is to large to display all at once correctly in " f"this terminal (sprite size is {spr.width}x{spr.height} and your " f"terminal is {base.Console.instance().width}x" f"{base.Console.instance().height}).")) if not args.silent and sprite_valid:
# On that board the player starts at the center lvl2 = engine.Board( ui_borders=graphics.MAGENTA_SQUARE, ui_board_void_cell=graphics.BLACK_SQUARE, player_starting_position=[5, 5], ) # And on that board the player starts at the bottom right corner lvl3 = engine.Board( ui_borders=graphics.RED_SQUARE, ui_board_void_cell=graphics.BLACK_SQUARE, player_starting_position=[9, 9], ) # Now let's create a game object. mygame = engine.Game(name="demo") # And a Player nazbrok = board_items.Player(name="Nazbrok", model=base.Text.green_bright("¤¤")) # Now add the boards to the game so the Game object can manage them # the parameters of add_board() are a level number and a board. mygame.add_board(1, lvl1) mygame.add_board(2, lvl2) mygame.add_board(3, lvl3) # Now we also want our player to be managed by the game mygame.player = nazbrok # Now let's show a clean screen to our player
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))
# First let's create a Board that uses squares as delimiters # Borders are going to be white squares # Cells with nothing inside are going to be black squares myboard = engine.Board( name="A demo board", 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=[10, 10], ) # Then create a Game object game = engine.Game() # Then create a player, and give it to manage to the Game engine game.player = Player(model=base.Text.yellow_bright("××")) # Associate the board to level number 1 game.add_board(1, myboard) # Now change the current level to 1. This will automatically place the player at the # player_starting_position of the board associated with level 1. game.change_level(1) # And make a short loop that moves the player and display the board for k in range(1, 10, 1): # Clear screen. game.clear_screen()