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

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

pip install pygame

  • 空格键:运行 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 = row
        self.col = col
        self.x = row * CELL_SIZE
        self.y = col * CELL_SIZE
        self.color = WHITE
        self.neighbors = []

    def is_closed(self):
        return self.color == RED

    def is_open(self):
        return self.color == YELLOW

    def is_barrier(self):
        return self.color == BLACK

    def is_start(self):
        return self.color == BLUE

    def is_end(self):
        return self.color == GREEN

    def reset(self):
        self.color = WHITE

    def make_start(self):
        self.color = BLUE

    def make_closed(self):
        self.color = RED

    def make_open(self):
        self.color = YELLOW

    def make_barrier(self):
        self.color = BLACK

    def make_end(self):
        self.color = GREEN

    def make_path(self):
        if not self.is_start() and not self.is_end():
            self.color = GREY

    def 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()
        ):  # Down
            self.neighbors.append(grid[self.row + 1][self.col])
        if self.row > 0 and not grid[self.row - 1][self.col].is_barrier():  # Up
            self.neighbors.append(grid[self.row - 1][self.col])
        if (
            self.col < COLS - 1 and not grid[self.row][self.col + 1].is_barrier()
        ):  # Right
            self.neighbors.append(grid[self.row][self.col + 1])
        if self.col > 0 and not grid[self.row][self.col - 1].is_barrier():  # Left
            self.neighbors.append(grid[self.row][self.col - 1])

    def __lt__(self, other):
        return False


# Utility functions
def h(p1, p2):
    x1, y1 = p1
    x2, y2 = p2
    return 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 = pos
    row = y // CELL_SIZE
    col = x // CELL_SIZE
    if 0 <= row < ROWS and 0 <= col < COLS:
        return row, col
    return None, None


def 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 = barrier
            grid[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 False

    count = 0
    open_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] = 0

    f_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 True

        for neighbor in current.neighbors:
            temp_g_score = g_score[current] + 1

            if temp_g_score < g_score[neighbor]:
                came_from[neighbor] = current
                g_score[neighbor] = temp_g_score
                f_score[neighbor] = temp_g_score + h(
                    (neighbor.row, neighbor.col), (end.row, end.col)
                )

                if neighbor not in open_set_hash:
                    count += 1
                    open_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 = None
    end = None

    running = True
    while running:
        draw(win, grid)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            if pygame.mouse.get_pressed()[0]:  # Left mouse button
                pos = 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 = spot
                        start.make_start()
                    elif not end and spot != start:
                        end = spot
                        end.make_end()
                    elif spot != start and spot != end:
                        spot.make_barrier()

            elif pygame.mouse.get_pressed()[2]:  # Right mouse button
                pos = 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 = None
                    elif spot == end:
                        end = None

            if 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.kler.cn/a/500316.html

相关文章:

  • TypeScript 爬虫项目实战:抓取豆瓣电影 Top 250(TypeScript简单应用)
  • 油猴支持阿里云自动登陆插件
  • 【物流管理系统 - IDEAJavaSwingMySQL】基于Java实现的物流管理系统导入IDEA教程
  • CNN Test Data
  • spring boot发送邮箱,java实现邮箱发送(邮件带附件)3中方式【保姆级教程一,代码直接用】
  • C++中的unordered_set,unordered_map,哈希表/散列表以及哈希表的模拟实现
  • SqlServer: An expression services limit has been reached异常处理
  • 如何让QPS提升20倍
  • 【学习路线】Python爬虫 详细知识点学习路径(附学习资源)
  • [程序设计]—代理模式
  • 单例模式-如何保证全局唯一性?
  • 【github】向右箭头文件打不开,下载也是空白
  • 【西北工业大学主办 | EI检索稳定 | 高H值专家与会报告】2025年航天航空工程与材料技术国际会议(AEMT 2025)
  • 单例模式5种写法
  • mysql根据表的统计信息核算一下表成本
  • Elasticsearch入门篇
  • 丢帧常见的几种处理方法
  • python+pdfplumber:提取和分析PDF中的表格、文本等数据,实现pdf转图片、CSV、JSON、dict
  • 解决Edge打开PDF总是没有焦点
  • Homestyler 和 Tripo AI 如何利用人工智能驱动的 3D 建模改变定制室内设计
  • Kubernetes集群架构
  • EasyCVR视频汇聚平台如何配置webrtc播放地址?
  • 车载数据结构 --- ARXML VS JSON
  • 【面试题】技术场景 6、Java 生产环境 bug 排查
  • 代码随想录刷题day05|(数组篇)59.螺旋矩阵 II