python 最短路径走迷宫--pygame界面可视化

# -*- coding: UTF-8 -*-
# animation.py
 
# 导入需要的模块
import pygame, sys
from pygame.locals import *
 
# 初始化pygame
pygame.init()
 
# 设置帧率(屏幕每秒刷新的次数)
FPS = 5

#import random
pre_route=list()    #宽度搜索得到的节点
q=list()    #队列结构控制循环次数
xx=[0,1,0,-1]   #右移、下移、左移、上移
yy=[1,0,-1,0]
visited=list()  #记录节点是否已遍历
father=list()   #每一个pre_route节点的父节点
route=list()
def bfs(l,x,y,m,n):
    visited=[[0 for i in range(len(l[0]))]for j in range(len(l))]
    visited[x][y]=1 #入口节点设置为已遍历
    q.append([x,y])
    while q:    #队列为空则结束循环
        now=q[0]
        q.pop(0)    #移除队列头结点
        for i in range(4):
            point=[now[0]+xx[i],now[1]+yy[i]]   #当前节点
            if point[0]<0 or point[1]<0 or point[0]>=len(l) or point[1]>=len(l[0]) or visited[point[0]][point[1]]==1 or l[point[0]][point[1]]=='1':
                continue
            father.append(now)
            visited[point[0]][point[1]]=1
            q.append(point)
            pre_route.append(point)
            if point[0]==m and point[1]==n:
                print("success")
                return 1
    print("false")
    return 0

def get_route(father,pre_route):    #输出最短迷宫路径
    route=[pre_route[-1],father[-1]]
    for i in range(len(pre_route)-1,-1,-1):
        if pre_route[i]==route[-1]:
            route.append(father[i])
    route.reverse()
    print("迷宫最短路径为:\n",route)
    print("步长:",len(route)-1)
    return route
    
def prn_map(route,l,m,n):   #打印包含路径的迷宫图
    for i in range(len(l)):
        l[i]=list(l[i])
    for i in range(len(route)):
        l[route[i][0]][route[i][1]]='2'
    for i in range(len(l)):
        for j in range(len(l[0])):
            if l[i][j]=='1':
                print('  ',end='')
            elif l[i][j]=='0':
                print('██',end='')
            else:
                print('░░',end='')
            if i==m and j==n:
                print('☀',end='')
        print()

l=['01010101001011001001010110010110100100001000101010',
   '00001000100000101010010000100000001001100110100101',
   '01111011010010001000001101001011100011000000010000',
   '01000000001010100011010000101000001010101011001011',
   '00011111000000101000010010100010100000101100000000',
   '11001000110101000010101100011010011010101011110111',
   '00011011010101001001001010000001000101001110000000',
   '10100000101000100110101010111110011000010000111010',
   '00111000001010100001100010000001000101001100001001',
   '11000110100001110010001001010101010101010001101000',
   '00010000100100000101001010101110100010101010000101',
   '11100100101001001000010000010101010100100100010100',
   '00000010000000101011001111010001100000101010100011',
   '10101010011100001000011000010110011110110100001000',
   '10101010100001101010100101000010100000111011101001',
   '10000000101100010000101100101101001011100000000100',
   '10101001000000010100100001000100000100011110101001',
   '00101001010101101001010100011010101101110000110101',
   '11001010000100001100000010100101000001000111000010',
   '00001000110000110101101000000100101001001000011101',
   '10100101000101000000001110110010110101101010100001',
   '00101000010000110101010000100010001001000100010101',
   '10100001000110010001000010101001010101011111010010',
   '00000100101000000110010100101001000001000000000010',
   '11010000001001110111001001000011101001011011101000',
   '00000110100010001000100000001000011101000000110011',
   '10101000101000100010001111100010101001010000001000',
   '10000010100101001010110000000100101010001011101000',
   '00111100001000010000000110111000000001000000001011',
   '10000001100111010111010001000110111010101101111000']
if bfs(l,0,0,29,49)==1:
        route=get_route(father,pre_route)
for i in range(len(l)):
    l[i]=list(l[i])
for i in range(len(route)):
    l[route[i][0]][route[i][1]]='2'

# 获得pygame的时钟
fpsClock = pygame.time.Clock()
 
# 设置窗口大小
screen = pygame.display.set_mode((500, 300), 0, 32)
 
# 设置标题
pygame.display.set_caption('迷宫')
 
# 定义颜色
WHITE = (255, 255, 255)
black=(0,0,0)
rect_color=(250,50,55)
 

it=0
while True:
    screen.fill(WHITE)
    route2=list()
    for i in route:
        route2.append([i[0]*10,i[1]*10])
    x=0;y=0
    for i in l:
       for j in i:
           if j=='1':
               pygame.draw.rect(screen,black,[x,y,10,10])
           x+=10
       x=0
       y+=10
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
    pygame.draw.rect(screen,rect_color,[route2[it][1],route2[it][0],10,10])
    if it<len(route2)-1:
        it+=1
    pygame.display.update()
    fpsClock.tick(FPS)

在这里插入图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些代码和思路。 首先,我们需要使用一个迷宫生成算法来创建一个迷宫。这里我们使用“递归分割”算法。该算法通过将迷宫递归地分成四个区域来创建迷宫。对于每个区域,我们随机选择一个墙并将其打开,以连接两个相邻的区域。 以下是一个示例实现: ```python import random def generate_maze(width, height): # Create grid maze = [[1] * width + [0] for _ in range(height)] + [[1] * (width + 1)] # Recursive division divide(maze, 0, 0, width, height) return maze def divide(maze, x, y, width, height): if width < 2 or height < 2: return # Choose orientation horizontal = random.choice([True, False]) # Create wall and opening wx = x + (0 if horizontal else random.randint(0, width - 2)) wy = y + (random.randint(0, height - 2) if horizontal else 0) px, py = wx + (horizontal), wy + (not horizontal) dx, dy = (1, 0) if horizontal else (0, 1) length = (height - 1) * horizontal + (width - 1) * (not horizontal) for i in range(length): if maze[wy][wx]: maze[py][px] = 0 wx += dx wy += dy px += dx py += dy # Recursive calls divide(maze, x, y, wx - x, wy - y) divide(maze, x, wy, wx - x, height - (wy - y)) divide(maze, wx, y, width - (wx - x), wy - y) divide(maze, wx, wy, width - (wx - x), height - (wy - y)) ``` 接下来,我们需要编写一个程序来解决迷宫。这里我们使用“深度优先搜索”算法。该算法从起点开始,不断选择一条未走过的路径,直到到达终点或无法继续前进。在此过程中,我们需要记录所走过的路径,以便在绘制迷宫时使用。 以下是一个示例实现: ```python def solve_maze(maze, start, end): stack = [start] visited = set() path = {} while stack: x, y = stack.pop() if (x, y) == end: break if (x, y) in visited: continue visited.add((x, y)) for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)): nx, ny = x + dx, y + dy if maze[ny][nx] == 0 and (nx, ny) not in visited: stack.append((nx, ny)) path[(nx, ny)] = (x, y) # Reconstruct path x, y = end while (x, y) != start: maze[y][x] = 2 x, y = path[(x, y)] maze[y][x] = 2 ``` 最后,我们需要使用pygame库来绘制迷宫和解决方案。我们将使用黑色矩形表示墙壁,白色矩形表示通路,绿色矩形表示起点,红色矩形表示终点,蓝色矩形表示解决方案。 以下是一个示例实现: ```python import pygame WIDTH, HEIGHT = 800, 600 CELL_SIZE = 20 maze = generate_maze(WIDTH // CELL_SIZE, HEIGHT // CELL_SIZE) solve_maze(maze, (0, 0), (len(maze[0]) - 1, len(maze) - 1)) pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() screen.fill((255, 255, 255)) for y, row in enumerate(maze): for x, cell in enumerate(row): rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE) if cell == 1: pygame.draw.rect(screen, (0, 0, 0), rect) elif cell == 2: pygame.draw.rect(screen, (0, 0, 255), rect) pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(0, 0, CELL_SIZE, CELL_SIZE)) pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(len(maze[0]) * CELL_SIZE - CELL_SIZE, len(maze) * CELL_SIZE - CELL_SIZE, CELL_SIZE, CELL_SIZE)) pygame.display.update() clock.tick(60) ``` 这个程序将生成一个随机的迷宫,然后使用深度优先搜索算法找到从左上角到右下角的最短路径,并用pygame库在屏幕上绘制迷宫和解决方案。 希望这可以帮助您实现您的项目!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值