- 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 = 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)