def make_platform(b, row, column): psize = random.randint(2, 10) plateform = [] tmp_game = Game() # Only because Game needs it, we don't care. tmp_game.player = 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), 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, Door(pos=[row, i])): if e.type == "ground": get_up = 3 break if get_up < 4: for e in tmp_game.neighbors(1, Door(pos=[row, i])): if e.type == "ground": get_up = 4 break m = block_color() + " " + Utils.Style.RESET_ALL plateform.append([Wall(model=m, 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])
g = Game() b = g.load_board('hac-maps/kneighbors.json',1) g.player = Player(model=Sprites.FLYING_SAUCER,name='player') g.change_level(1) key = None while True: 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) elif key == 'q': break g.clear_screen() g.display_board() for i in g.neighbors(1): print(f'Player: {i.name} ({i.pos[0]},{i.pos[1]})') for i in g.neighbors(1, g.current_board().item(7,7) ): print(f'NPC: {i.name} ({i.pos[0]},{i.pos[1]})') key = Utils.get_key()
game.move_player(cst.DLDOWN, 1) elif key == '3': p.model = sprite_player['right'] game.move_player(cst.DRDOWN, 1) elif key == 'q': game.clear_screen() print(Utils.cyan_bright(f'Thanks for playing {game.name}')) print(Utils.yellow_bright('Good bye!')) break elif key == 'v': current_menu = 'speed_menu' elif key == 'k': game.player.hp -= 5 # Once we've moved we check if we are in the neighborhood of Unipici if game.player in game.neighbors(1, nice_npc): current_menu = 'unipici_dialog' # Here we change the speed of the game and then go back to main menu. elif current_menu == 'speed_menu': if key == '1': game_speed = 0.5 current_menu = 'main_menu' elif key == '2': game_speed = 0.25 current_menu = 'main_menu' elif key == '3': game_speed = 0.1 current_menu = 'main_menu' elif key == '4': game_speed = 0.05 current_menu = 'main_menu'
# the player goes. g.timer += 1 / g.current_level # On the other end, the player gains more points the further he goes g.score += int((g.player.pos[1] - g.player.last_x) - 1 / (g.player.pos[1] - g.player.last_x)) g.player.last_x = g.player.pos[1] if key == Utils.key.LEFT: g.move_player(Constants.LEFT, 1) elif key == Utils.key.RIGHT: g.move_player(Constants.RIGHT, 1) # elif key == Utils.key.DOWN: # gravity_friction -= 0.1 # elif key == Utils.key.UP: # gravity_friction += 0.1 elif key == Utils.key.SPACE: if g.player.dy == 0 and len(g.neighbors(1, g.player)) > 0: # Jump # Start with sound jump_wave_obj.play() g.player.max_y = g.player.pos[0] - 3 g.player.dy = 1 elif key == "X": g.stop() break elif key in "awsdzq": projectile = Projectile( name="treeball", direction=Constants.RIGHT, range=2, model=Utils.green_bright(bg_color + " *" + Utils.Style.RESET_ALL),
Utils.print_white_on_red("GAME OVER") break r = g.player.pos[0] c = g.player.pos[1] if key == Utils.key.UP: g.move_player(Constants.UP, 1) elif key == Utils.key.DOWN: g.move_player(Constants.DOWN, 1) elif key == Utils.key.RIGHT: g.move_player(Constants.RIGHT, 1) elif key == Utils.key.LEFT: g.move_player(Constants.LEFT, 1) elif key == "Q": break for guard in guards: if g.player in g.neighbors(5, guard): guard.actuator = PathFinder(game=g, actuated_object=guard) chasing = True winner = nearest_cell(g.player, guard) guard.actuator.set_destination(winner[0], winner[1]) else: guard.actuator = RandomActuator(moveset=[ Constants.UP, Constants.DOWN, Constants.LEFT, Constants.RIGHT ]) chasing = False if chasing and g.player in g.neighbors(1, guard): g.player.hp -= 5 else: g.actuate_npcs(1) redraw() time.sleep(0.1)
class GameNeighborTestCase(unittest.TestCase): def setUp(self): super().setUp() self.game = Game() self.board = self.game.load_board("hac-maps/kneighbors.json", 1) self.game.player = 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 test_player_neighbors(self): testcases = ( ([3, 3], []), ([3, 5], [self.tree26, self.wall45, self.wall46]), ([3, 6], [self.tree26, self.wall45, self.wall46, self.wall47]), ([3, 7], [self.tree26, self.wall46, self.wall47, self.treasure38]), ([4, 8], [self.wall47, self.wall57, self.treasure38, self.tree39]), ([5, 6], [self.wall45, self.wall46, self.wall47, self.wall57]), ) for pos, expected_board_items in testcases: with self.subTest(str(pos)): self.game.player.pos = pos actual_board_items = self.game.neighbors() assertion_message = " ".join([ readable_board_items(expected_board_items), "!=", readable_board_items(actual_board_items), ]) self.assertSetEqual( set(expected_board_items), set(actual_board_items), assertion_message, ) def test_npc_neighbors(self): def move_player_next_to_npc77(): pos_next_to_npc = [7, 8] self.game.move_player(Constants.DOWN, pos_next_to_npc[0]) self.game.move_player(Constants.RIGHT, pos_next_to_npc[1]) self.assertEqual(pos_next_to_npc, self.game.player.pos) actual_board_items = self.game.neighbors(1, self.npc77) self.assertSetEqual(set(), set(actual_board_items)) move_player_next_to_npc77() actual_board_items = self.game.neighbors(1, self.npc77) self.assertSetEqual({self.game.player}, set(actual_board_items)) def dump_board_items(self): h, w = self.board.size for y in range(h): for x in range(w): boarditem = self.board.item(y, x) assert isinstance(boarditem, BoardItem) if isinstance(boarditem, BoardItemVoid): continue print(readable_board_item(boarditem))