|
发表于 2024-1-28 12:56:49
|
显示全部楼层
本楼为最佳答案
 当然有人做战棋游戏啦!战棋游戏是一种非常受欢迎的游戏类型。至于六边形和正方形地块哪个更好,这主要取决于你对游戏的设计需求。
六边形地块与正方形地块相比具有更多的邻居连接方式,这可以使得地图更加自由、灵活,同时也能增加游戏的策略性。但六边形地块的绘制和计算可能会稍微复杂一些。
如果你想根据移动力算出可到达的范围,可以考虑使用广度优先搜索(BFS)算法。BFS算法可以从起点开始,逐步扩展到所有可以到达的地块,直到达到移动力限制为止。
至于A*算法,它是一种启发式搜索算法,可以用来寻找两个地点之间的最短路径。如果你需要计算单位在地图上的实际移动路径,而不仅仅是计算范围,则A*算法可能更适合。
下面是一个使用Python语言实现基本战棋地图和移动范围的示例代码:
- class Tile:
- def __init__(self, row, col):
- self.row = row
- self.col = col
- self.is_passable = True
- class Map:
- def __init__(self, rows, cols):
- self.rows = rows
- self.cols = cols
- self.tiles = [[Tile(row, col) for col in range(cols)] for row in range(rows)]
- def set_tile_passable(self, row, col, passable):
- self.tiles[row][col].is_passable = passable
-
- def get_neighbors(self, tile):
- neighbors = []
- directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
- if tile.row % 2 == 0:
- directions.extend([(1, 1), (-1, 1)])
- else:
- directions.extend([(1, -1), (-1, -1)])
- for dr, dc in directions:
- neighbor_row = tile.row + dr
- neighbor_col = tile.col + dc
- if (0 <= neighbor_row < self.rows and
- 0 <= neighbor_col < self.cols and
- self.tiles[neighbor_row][neighbor_col].is_passable):
- neighbors.append(self.tiles[neighbor_row][neighbor_col])
- return neighbors
- def calculate_reachable_tiles(start_tile, movement_points):
- visited = set()
- queue = [(start_tile, 0)]
- reachable_tiles = []
-
- while queue:
- tile, distance = queue.pop(0)
- if tile in visited:
- continue
- visited.add(tile)
-
- if distance > movement_points:
- continue
-
- reachable_tiles.append(tile)
-
- neighbors = map.get_neighbors(tile)
- for neighbor in neighbors:
- queue.append((neighbor, distance + 1))
-
- return reachable_tiles
- # 使用示例
- map = Map(8, 8)
- map.set_tile_passable(3, 3, False) # 设置(3, 3)位置不可通过
- start_tile = map.tiles[2][2]
- reachable_tiles = calculate_reachable_tiles(start_tile, 3)
- for tile in reachable_tiles:
- print(f"({tile.row}, {tile.col})")
复制代码
希望对你有所帮助!加油,开发属于自己的战棋游戏吧!{:5_142:}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|