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_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 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_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_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_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))
max_iter = 10 if len(sys.argv) > 2: max_iter = int(sys.argv[2]) g = engine.Game() b = g.load_board(board_to_load, 1) if b.width >= g.screen.width or b.height >= g.screen.height: g.enable_partial_display = True g.partial_display_viewport = [ int((g.screen.height - 2) / 2), int((g.screen.width - 2) / 4), ] g.player = board_items.Player(model=graphics.Models.FLYING_SAUCER) g.player.inventory.max_size = 99999 g.change_level(1) idx = 0 key = None while idx < max_iter or max_iter == 0: if key == "w": g.move_player(constants.UP, 1) elif key == "s": g.move_player(constants.DOWN, 1) elif key == "a": g.move_player(constants.LEFT, 1) elif key == "d": g.move_player(constants.RIGHT, 1)
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 mygame.clear_screen() # We haven't place nazbrok on any board, but that's ok because we are going # to use Game to manage the starting position of our player
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) mygame.add_board(1, board1) mygame.add_board(2, board2) mygame.change_level(1) key = None # Main game loop while True: if key == "q": print(base.Text.yellow_bright("Good bye and thank you for playing!")) break elif key == "w" or key == engine.key.UP: mygame.move_player(constants.UP, 1)
(20 - nb_blocks) + "]") g.display_board() # manage_fireballs() b = engine.Board( ui_borders=graphics.WHITE_SQUARE, ui_board_void_cell=graphics.BLACK_SQUARE, size=[20, 20], player_starting_position=[5, 5], ) wall = board_items.Wall(model=graphics.Models.BRICK) b.place_item(wall, 1, 6) g = engine.Game() g.add_board(1, b) g.player = board_items.Player(model=graphics.Models.MAGE, name="The Maje") g.player.mp = 20 g.player.max_mp = 20 g.change_level(1) key = None black_circle = "-\U000025CF" circle_jot = "-\U0000233E" throw_fireball = False projectile = board_items.Projectile() while True: if key == "Q": break elif key == "1":
if constants.PYGAMELIB_VERSION < "1.2.0": base.Text.print_white_on_red( "Super Panda Run EX require the hac-game-lib version 1.1.0 or greater." f" Version installed is {constants.PYGAMELIB_VERSION}" ) raise SystemExit() # Start background music bg_music_play_obj = bg_music_wave_obj.play() g = engine.Game() g.enable_partial_display = True g.partial_display_viewport = [10, int(term_res / 4)] g.current_level = 0 g.player = board_items.Player( model=bg_color + graphics.Models.PANDA + graphics.Style.RESET_ALL ) g.player.name = "Zigomar" g.player.max_y = g.player.pos[0] g.player.dy = gravity_speed g.player.last_y = g.player.pos[0] g.player.last_x = g.player.pos[1] g.timer = 60 g.score = 0 g.obj_stack = [] g.pause() if os.path.exists("settings-suparex.json"): g.load_config("settings-suparex.json", "settings") if g.config("settings")["player_name"] is not None: g.player.name = g.config("settings")["player_name"]
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)
def test_player(self): p = board_items.Player(inventory=board_items.engine.Inventory()) self.assertFalse(p.pickable()) self.assertTrue(p.has_inventory())
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, cst.RIGHT,