Python游戏之运动物体寻路——A星算法与扩展

本文介绍了如何在Python游戏中应用A星算法进行寻路,特别是处理大于格栅大小的物体寻路问题。通过A-Star算法和Brushfire算法的结合,实现任意大小物体的寻路。内容包括A-Star算法的要点,格栅的描述,路径的简化,以及在实际游戏中的应用。
摘要由CSDN通过智能技术生成

上一篇文章主要完成了游戏地图的创建,其中简单为游戏寻路做了一点准备,就是格栅背景地图。游戏场景中的各种山川、河流、楼阁等都都可以视为为某种运动精灵的障碍物,那么精灵如何绕过障碍物运动都自己的目的地呢?目前关于寻路的算法有很多,最常见的就是A-Star算法,也叫A星算法。
典型的A-star算法处理的是运动物体和格栅大小一致,如果运动物体比格栅小,自然没有任何问题。如果运动物体比格栅大,典型的A-star算法就不能处理了。那么是不是A-star方法就不能处理了呢?不是,针对这个问题,国外有人提出了叫做 Brushfire的算法。据说得google才能找到。我简单看了一下,应该也是可以实现的。有兴趣的朋友可以自行探索。
典型的Astar算法网上有很多详细的讲解,有一位大拿的文章请参考,还有一些前人给出了C/C++/JAVA/JSP等各种语言的实现。这里就不再重复了。
本文解决的问题是大于格栅大小的物体的寻路问题,并尽可能实现任何大小的物体寻路。下面有几个示意图可以先看一下。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
为了方便理解,这里用虚线展示精灵的运动路径,实际上是精灵的左上角运动轨迹。有些Astar算法只允许精灵在水平和垂直方向运动,有些可以8个方向(45度角)。这其实没有本质的区别,只是在寻路过程中对合格邻接点的筛选策略不同而已。完全可以根据自己的需要来处理。
好了,现在开始展示A-star算法。
既然涉及到格栅,那么格栅怎么描述呢?就是点的集合。先描述点:

class Node:
    def __init__(self, point, target, father=None):
        self.point = point
        self.father = father

        if father is not None:
            self.gCost = father.gCost + self.get_gCost(father.point)
            self.hCost = self.get_hCost(target)
            self.fCost = self.gCost + self.hCost
        else:
            self.gCost = 0
            self.hCost = 0
            self.fCost = 0

    # 计算自己到父节点的gCost。如果到父节点为 45 度方向,设置 gCost 为 14,否则设为 10
    def get_gCost(self, father):
        if abs(self.point[0] - father[0]) + abs(self.point[1] - father[1]) == 2:
            return 14

        return 10

    def get_hCost(self, target):
        return (abs(target[0] - self.point[0]) + abs(target[1] - self.point[1])) * 10

    def reset_father(self, father, newgCost):
        if father is not None:
            self.gCost = newgCost
            self.fCost = self.gCost + self.hCost

        self.father = father

关于cost的设置方法,有很多策略,有兴趣的可以进一步研究。这里使用最简单的实现,便于理解。
point是什么呢?就是格栅点的(X轴索引,Y轴索引),一个Tuple。
有了Node,下面就是A-Star了。
类属性定义:

class AStar:
    # stat and target 是Tuple (x, y), 是格栅的索引点,不是像素坐标。
    # size 为寻径主体的大小,表示边长为格栅尺寸的倍数
    # 对于非正方形主体,将其边长大的边计算size就可以
    def __init__(self, grids, start, target, size):
        self.grids = grids

        self.size = size

        self.start = self.adjust_position(start)
        self.target = self.adjust_position(target)

        self.startNode = Node(self.start, self.target, None)
        self.targetNode = Node
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Python代码实现A寻路算法可以使用以下代码:def astar(start, goal): closedset = set() openset = set([start]) came_from = {} gscore = {start: 0} fscore = {start: heuristic_cost_estimate(start, goal)} while openset: current = min(openset, key=fscore.get) if current == goal: return reconstruct_path(came_from, current) openset.remove(current) closedset.add(current) for neighbor in get_neighbors(current): if neighbor in closedset: continue tentative_gscore = gscore[current] + dist_between(current, neighbor) if neighbor not in openset: openset.add(neighbor) elif tentative_gscore >= gscore[neighbor]: continue came_from[neighbor] = current gscore[neighbor] = tentative_gscore fscore[neighbor] = gscore[neighbor] + heuristic_cost_estimate(neighbor, goal) return False ### 回答2: A寻路算法是一种常用的路径规划算法,可以在给定的地图中找到最短路径。下面是用Python实现A寻路算法的代码示例: ```python import heapq # 定义节点类 class Node: def __init__(self, row, col, g, h): self.row = row self.col = col self.g = g self.h = h def __lt__(self, other): return self.g + self.h < other.g + other.h # 计算启发式代价(h值) def heuristic(row, col, target_row, target_col): return abs(target_row - row) + abs(target_col - col) # A寻路算法 def astar_search(start, target, grid): directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 上下左右四个方向 rows, cols = len(grid), len(grid[0]) visited = set() # 已访问的节点集合 pq = [] # 优先队列,用来选择下一个节点 heapq.heappush(pq, start) while pq: curr_node = heapq.heappop(pq) row, col = curr_node.row, curr_node.col visited.add((row, col)) if row == target.row and col == target.col: return curr_node.g for d in directions: new_row, new_col = row + d[0], col + d[1] if 0 <= new_row < rows and 0 <= new_col < cols and grid[new_row][new_col] != 1: new_g = curr_node.g + 1 new_h = heuristic(new_row, new_col, target.row, target.col) new_node = Node(new_row, new_col, new_g, new_h) if (new_row, new_col) not in visited: heapq.heappush(pq, new_node) return -1 # 示例 grid = [[0, 0, 0], [1, 1, 0], [0, 0, 0]] start = Node(0, 0, 0, 0) target = Node(2, 2, 0, 0) result = astar_search(start, target, grid) print(result) ``` 以上代码实现了A寻路算法,并可用于寻找给定地图中起点到目标点的最短路径。其中,`grid`是二维列表表示地图,在地图中,0表示可通过的节点,1表示不可通过的节点。`start`表示起点,`target`表示目标点。通过调用`astar_search`函数,将起点、目标点和地图作为参数进行传递,即可得到最短路径的长度。 ### 回答3: A寻路算法是一种常用于寻找最短路径的算法,广泛应用于游戏开发、路径规划等领域。下面是使用Python实现A寻路算法的代码: ```python # 定义地图和起终点 map = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0] ] start = (1, 1) # 起点坐标 goal = (6, 5) # 终点坐标 # 定义辅助函数 def heuristic(pos1, pos2): # 估算两点之间的距离(启发函数) x1, y1 = pos1 x2, y2 = pos2 return abs(x1 - x2) + abs(y1 - y2) def get_neighbors(pos): # 获取当前位置的邻近位置 x, y = pos neighbors = [] for dx in [-1, 0, 1]: for dy in [-1, 0, 1]: if dx == dy == 0: # 排除当前位置 continue new_x, new_y = x + dx, y + dy if 0 <= new_x < len(map) and 0 <= new_y < len(map[0]) and map[new_x][new_y] == 0: neighbors.append((new_x, new_y)) return neighbors # 定义A算法函数 def astar_search(start, goal): # 初始化起点和终点 open_list = [start] # 待探索的节点 came_from = {} # 记录路径中每个节点的上一个节点 g_score = {start: 0} # 起点到每个节点的实际代价 f_score = {start: heuristic(start, goal)} # 起点到每个节点的估算代价(f_score = g_score + h_score) while open_list: current = min(open_list, key=lambda x: f_score[x]) # 获取f_score最小的节点 if current == goal: # 已到达终点 path = [] while current in came_from: path.append(current) current = came_from[current] path.append(start) path.reverse() return path open_list.remove(current) for neighbor in get_neighbors(current): g_temp = g_score[current] + 1 # 节点到邻近节点的代价为1 if neighbor not in g_score or g_temp < g_score[neighbor]: came_from[neighbor] = current g_score[neighbor] = g_temp f_score[neighbor] = g_temp + heuristic(neighbor, goal) if neighbor not in open_list: open_list.append(neighbor) return [] # 未找到路径 # 在地图上运行A算法 path = astar_search(start, goal) print("路径: ", path) ``` 该代码首先定义了一个地图,使用0表示可行走的区域,使用1表示障碍物。将起点和终点设定好后,通过`heuristic`函数计算两点间的估算距离。`get_neighbors`函数用于获取当前位置的邻近位置。 `astar_search`函数是实现A算法的核心部分。其中,`open_list`用于存放待探索的节点,`came_from`用于记录路径中每个节点的上一个节点,`g_score`用于记录起点到每个节点的实际代价,`f_score`用于记录起点到每个节点的估算代价。算法使用一个循环不断探索下一个最有可能的节点,直到找到终点或无法找到路径为止。 最后,在地图上运行A算法,并输出结果路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值