def generate_treasure(b, row, column): t = random.choice(list(b.treasures.keys())) if ( random.randint(0, 100) >= 100 - b.treasures[t]["rate"] and b.treasures[t]["count"] > 0 ): # The value of each treasure object is used differently. if t == "scorers": b.place_item( board_items.Treasure( model=bg_color + graphics.Models.TANABATA_TREE + graphics.Style.RESET_ALL, value=25, type="treasure.scorers", ), row, column, ) elif t == "timers": b.place_item( board_items.Treasure( model=bg_color + graphics.Models.HOURGLASS_NOT_DONE + graphics.Style.RESET_ALL, value=15, type="treasure.timers", ), row, column, ) elif t == "diamond": b.place_item( board_items.Treasure( model=bg_color + graphics.Models.GEM_STONE + graphics.Style.RESET_ALL, value=30, type="treasure.diamond", ), row, column, ) elif t == "1UP": b.place_item( board_items.Treasure( model=bg_color + graphics.Models.HEART_WITH_RIBBON + graphics.Style.RESET_ALL, value=30, type="treasure.1UP", ), row, column, ) b.treasures[t]["count"] -= 1 b.treasures[t]["placed"] += 1
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_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_treasure(self): bi = board_items.Treasure(value=1000, inventory_space=5) self.assertTrue(bi.pickable()) self.assertFalse(bi.overlappable()) self.assertFalse(bi.restorable())
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)
cst.DOWN, cst.DOWN, cst.DOWN, cst.DOWN, cst.DOWN, cst.LEFT, cst.LEFT, cst.LEFT, cst.LEFT, ]) 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)