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_genericstructure(self): bi = board_items.GenericStructure(value=5) self.assertFalse(bi.pickable()) self.assertFalse(bi.overlappable()) self.assertFalse(bi.restorable()) with self.assertRaises(board_items.base.PglInvalidTypeException): bi.set_restorable(1) bi.set_restorable(True) self.assertTrue(bi.restorable()) bi = board_items.GenericActionableStructure()
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): 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 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 generate_level(g, b): # When we get the map, the last row is full of blocks last_alt = b.size[1] - 1 gap = 0 gap_threshold = 2 b.visited_columns = [] # We keep a count of the treasures we can put on the map as well as the rate we # can put them on (in percent). There is no possibility that we will end up with # more items, but there is a small possibility that we end up with less b.treasures = { "timers": { "count": int(math.log(g.current_level + 1)), "rate": 10, "placed": 0, }, "scorers": { "count": 1 + int(math.log(g.current_level + 1)), "rate": 10, "placed": 0, }, "1UP": {"count": 1, "rate": int(math.log(g.current_level + 1)), "placed": 0}, "diamond": {"count": 1, "rate": 2, "placed": 0}, } # We go through all the column of the map. for x in range(5, b.size[0]): # print(f"[d] generate level x={x}") # We have 50% chance of putting an obstacle. if random.choice([True, False]): if gap > gap_threshold: for k in range(x - gap, x): b.clear_cell(b.size[1] - 1, k) alt = random.randint(last_alt - 2, b.size[1] - 1) last_alt = alt y = 0 for y in range(alt + 1, b.size[1] - 1): # print(f"[d] generate level y={y}") b.place_item( board_items.Wall( model=block_color() + " " + graphics.Style.RESET_ALL, type="ground", ), y, x, ) # generate_treasure(b, y - 1, x) # For the fun of it and also to break monotony we have 20% of chance # of placing a platform here (of random size). We'll put treasures on # them later. if random.randint(0, 100) >= 80: make_platform(b, last_alt - random.randint(2, 3), x) # if y < b.size[1] - 1: # generate_trap(b, alt, x) # Just to break monotony, we have 33% chance of putting a cloud # in the background if random.randint(0, 100) >= 66: b.place_item( board_items.Door( model=bg_color + "\U00002601 " + graphics.Style.RESET_ALL ), alt - 8 + random.randint(-2, 2), x, ) gap = 0 else: # If we didn't put an obstacle, we keep track of the gap we're leaving. # When the gap is spreading over a threshold, we have 50% chance of # emptying it (we make a pit) gap += 1 b.place_item( board_items.GenericActionableStructure( model=bg_color + graphics.Models.CYCLONE + graphics.Style.RESET_ALL, type="exit", action=change_level, action_parameters=[g], ), last_alt - 1, b.size[0] - 1, ) # The map is done, let's add treasures and traps. # we travel the map once again and look for openings idx = 0 while b.available_traps > 0 or idx < 10: for col in range(5, b.size[0]): if random.choice([True, False]): candidates = [] for row in range(0, b.size[1]): item = b.item(row, col) # We only put treasures and traps on the ground if ( not isinstance(item, board_items.BoardItemVoid) and item.type == "ground" ): # We want at least 2 free cells in all directions (except down) free_cells = [ (row - 1, col), (row - 2, col), (row - 1, col - 1), (row - 1, col - 2), (row - 2, col - 1), (row - 2, col - 2), (row - 1, col + 1), (row - 1, col + 2), (row - 2, col + 1), (row - 2, col + 2), ] good_candidate = True for coord in free_cells: if coord[0] >= b.size[1] or coord[1] >= b.size[0]: continue cell = b.item(coord[0], coord[1]) if not isinstance(cell, board_items.BoardItemVoid): good_candidate = False break if good_candidate: candidates.append(item) for c in candidates: generate_trap(b, c.pos[0], c.pos[1]) idx += 1 idx = 0 while True: for col in range(5, b.size[0]): if random.choice([True, False]): candidates = [] for row in range(0, b.size[1]): item = b.item(row, col) # We only put treasures and traps on the ground if ( not isinstance(item, board_items.BoardItemVoid) and item.type == "ground" ): # We want at least 2 free cells in all directions (except down) free_cells = [ (row - 1, col), (row - 2, col), (row - 1, col - 1), (row - 1, col - 2), (row - 2, col - 1), (row - 2, col - 2), (row - 1, col + 1), (row - 1, col + 2), (row - 2, col + 1), (row - 2, col + 2), ] good_candidate = True for coord in free_cells: if coord[0] >= b.size[1] or coord[1] >= b.size[0]: continue cell = b.item(coord[0], coord[1]) if not isinstance(cell, board_items.BoardItemVoid): good_candidate = False break if good_candidate: candidates.append(item) for c in candidates: generate_trap(b, c.pos[0], c.pos[1]) idx += 1 if b.treasures["scorers"]["count"] <= 0 and b.treasures["timers"]["count"] <= 0: break if idx == 10: break
game.add_board(1, lvl1) game.add_board(2, lvl2) t = board_items.Treasure(model=sprite_treasure, name="Cool treasure", type="gem") money_bag = board_items.Treasure(model=sprite_treasure2, name="money", value=20) tree = board_items.GenericStructure(model=sprite_tree) tree.set_overlappable(False) tree.set_pickable(False) portal2 = board_items.GenericActionableStructure(model=sprite_portal) portal2.set_overlappable(False) portal2.action = change_current_level portal2.action_parameters = [game, 2] portal1 = board_items.GenericActionableStructure(model=sprite_portal) portal1.set_overlappable(False) portal1.action = change_current_level portal1.action_parameters = [game, 1] life_heart = board_items.GenericActionableStructure(model=sprite_heart, name="life_100") life_heart.set_overlappable(True) life_heart.action = add_hp life_heart.action_parameters = [game, 100]