当前位置: 首页 > news >正文

【python A* pygame 格式化 自定义起点、终点、障碍】

- pip install pygame

 test.py(chatgpt版本)

  • 空格键:运行 A* 算法。
  • Ctrl+C 键:清空路径。
  • Ctrl+S 键:保存当前地图到 map.json 文件。
  • Ctrl+L 键:从 map.json 文件加载地图。
import pygame
import json
from queue import PriorityQueue
from tkinter import messagebox, Tk# Initialize pygame
pygame.init()# Constants
WIDTH, HEIGHT = 800, 800
ROWS, COLS = 40, 40
CELL_SIZE = WIDTH // COLS
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GREY = (200, 200, 200)# Pygame setup
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("A* Pathfinding Visualization")class Spot:def __init__(self, row, col):self.row = rowself.col = colself.x = row * CELL_SIZEself.y = col * CELL_SIZEself.color = WHITEself.neighbors = []def is_closed(self):return self.color == REDdef is_open(self):return self.color == YELLOWdef is_barrier(self):return self.color == BLACKdef is_start(self):return self.color == BLUEdef is_end(self):return self.color == GREENdef reset(self):self.color = WHITEdef make_start(self):self.color = BLUEdef make_closed(self):self.color = REDdef make_open(self):self.color = YELLOWdef make_barrier(self):self.color = BLACKdef make_end(self):self.color = GREENdef make_path(self):if not self.is_start() and not self.is_end():self.color = GREYdef draw(self, win):pygame.draw.rect(win, self.color, (self.x, self.y, CELL_SIZE, CELL_SIZE))def update_neighbors(self, grid):self.neighbors = []if (self.row < ROWS - 1 and not grid[self.row + 1][self.col].is_barrier()):  # Downself.neighbors.append(grid[self.row + 1][self.col])if self.row > 0 and not grid[self.row - 1][self.col].is_barrier():  # Upself.neighbors.append(grid[self.row - 1][self.col])if (self.col < COLS - 1 and not grid[self.row][self.col + 1].is_barrier()):  # Rightself.neighbors.append(grid[self.row][self.col + 1])if self.col > 0 and not grid[self.row][self.col - 1].is_barrier():  # Leftself.neighbors.append(grid[self.row][self.col - 1])def __lt__(self, other):return False# Utility functions
def h(p1, p2):x1, y1 = p1x2, y2 = p2return abs(x1 - x2) + abs(y1 - y2)def reconstruct_path(came_from, current, draw):while current in came_from:current = came_from[current]current.make_path()draw()def make_grid():return [[Spot(i, j) for j in range(COLS)] for i in range(ROWS)]def draw_grid_line(win):for i in range(ROWS):pygame.draw.line(win, GREY, (0, i * CELL_SIZE), (WIDTH, i * CELL_SIZE))for j in range(COLS):pygame.draw.line(win, GREY, (j * CELL_SIZE, 0), (j * CELL_SIZE, HEIGHT))def draw(win, grid):win.fill(WHITE)for row in grid:for spot in row:spot.draw(win)draw_grid_line(win)pygame.display.update()def get_clicked_pos(pos):y, x = posrow = y // CELL_SIZEcol = x // CELL_SIZEif 0 <= row < ROWS and 0 <= col < COLS:return row, colreturn None, Nonedef clear_grid(grid):for row in grid:for spot in row:if not (spot.is_start() or spot.is_end() or spot.is_barrier()):spot.reset()def save_grid(grid, filename="map.json"):data = {"start": None, "end": None, "barriers": []}for row in grid:for spot in row:if spot.is_start():data["start"] = (spot.row, spot.col)elif spot.is_end():data["end"] = (spot.row, spot.col)elif spot.is_barrier():data["barriers"].append((spot.row, spot.col))try:with open(filename, "w", encoding="utf-8") as f:json.dump(data, f, indent=4, ensure_ascii=False)Tk().withdraw()messagebox.showinfo("Save Successful", "The grid has been saved successfully.")print("Save Successful", "The grid has been saved successfully.")except Exception as e:Tk().withdraw()messagebox.showerror("Save Error", f"Error saving grid: {e}")print(f"Error saving grid: {e}")def load_grid(grid, filename="map.json"):try:with open(filename, "r", encoding='utf-8') as f:data = json.load(f)for row in grid:for spot in row:spot.reset()if data["start"]:start_row, start_col = data["start"]grid[start_row][start_col].make_start()if data["end"]:end_row, end_col = data["end"]grid[end_row][end_col].make_end()for barrier in data["barriers"]:barrier_row, barrier_col = barriergrid[barrier_row][barrier_col].make_barrier()Tk().withdraw()messagebox.showinfo("Load Successful", "The grid has been loaded successfully.")print('Load Successful", "The grid has been loaded successfully.')except (FileNotFoundError, KeyError, json.JSONDecodeError):Tk().withdraw()messagebox.showerror("Load Error", "Error loading grid: Invalid or missing map file.")print("Error loading grid: Invalid or missing map file.")# A* Algorithm
def a_star(draw, grid, start, end):if not start or not end or start == end:print("Error: Invalid start or end node.")return Falsecount = 0open_set = PriorityQueue()open_set.put((0, count, start))came_from = {}g_score = {spot: float("inf") for row in grid for spot in row}g_score[start] = 0f_score = {spot: float("inf") for row in grid for spot in row}f_score[start] = h((start.row, start.col), (end.row, end.col))open_set_hash = {start}while not open_set.empty():for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()current = open_set.get()[2]open_set_hash.remove(current)if current == end:current.make_end()reconstruct_path(came_from, end, draw)return Truefor neighbor in current.neighbors:temp_g_score = g_score[current] + 1if temp_g_score < g_score[neighbor]:came_from[neighbor] = currentg_score[neighbor] = temp_g_scoref_score[neighbor] = temp_g_score + h((neighbor.row, neighbor.col), (end.row, end.col))if neighbor not in open_set_hash:count += 1open_set.put((f_score[neighbor], count, neighbor))open_set_hash.add(neighbor)neighbor.make_open()draw()if current != start:current.make_closed()return False# Main function
def main(win):grid = make_grid()start = Noneend = Nonerunning = Truewhile running:draw(win, grid)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif pygame.mouse.get_pressed()[0]:  # Left mouse buttonpos = pygame.mouse.get_pos()row, col = get_clicked_pos(pos)if row is not None and col is not None:spot = grid[row][col]if not start and spot != end:start = spotstart.make_start()elif not end and spot != start:end = spotend.make_end()elif spot != start and spot != end:spot.make_barrier()elif pygame.mouse.get_pressed()[2]:  # Right mouse buttonpos = pygame.mouse.get_pos()row, col = get_clicked_pos(pos)if row is not None and col is not None:spot = grid[row][col]spot.reset()if spot == start:start = Noneelif spot == end:end = Noneif event.type == pygame.KEYDOWN:print(f"KEYDOWN")if event.key == pygame.K_SPACE and start and end:clear_grid(grid)for row in grid:for spot in row:spot.update_neighbors(grid)a_star(lambda: draw(win, grid), grid, start, end)if event.key == pygame.K_c:print("press ctrl+c")clear_grid(grid)if event.key == pygame.K_s:print("press ctrl+s")save_grid(grid)if event.key == pygame.K_l:print("press ctrl+l")load_grid(grid)pygame.quit()main(WIN)

http://www.lryc.cn/news/518641.html

相关文章:

  • 12_Redis发布订阅
  • 归并排序:数据排序的高效之道
  • 【redis初阶】浅谈分布式系统
  • CatLog的使用
  • 头歌python实验:网络安全应用实践-恶意流量检测
  • 大模型WebUI:Gradio全解11——Chatbots:融合大模型的多模态聊天机器人(2)
  • 如何用 Python 实现简单的 AI 模型?
  • 单片机-直流电机实验
  • python【数据结构】
  • 详解Sonar与Jenkins 的集成使用!
  • 《笔记》青蛙跳台阶——斐波那契数列
  • SpringBoot3动态切换数据源
  • OSPF - 特殊区域
  • Linux 系统下磁盘相关指令:df、du、fdisk、lsblk
  • 基于单片机的肺功能MVV简单测算
  • 如何用Python编程实现自动整理XML发票文件
  • 腾讯云AI代码助手编程挑战赛-百事一点通
  • Spring学习笔记1
  • LeetCode 2185. Counting Words With a Given Prefix
  • 图漾相机基础操作
  • 前端开发中页面优化的方法
  • Qt QDockWidget详解以及例程
  • 机器学习之贝叶斯分类器和混淆矩阵可视化
  • 关于大数据的基础知识(一)——定义特征结构要素
  • 2025 GitCode 开发者冬日嘉年华:AI 与开源的深度交融之旅
  • 【MyBatis-Plus 进阶功能】开发中常用场景剖析
  • 【C++/控制台】2048小游戏
  • Linux 中 top 命令的使用与实例解读
  • C++ STL 中的 `unordered_map` 和 `unordered_set` 总结
  • 机器学习基础-概率图模型