import pygame import sys import random import time from pygame.locals import * from random import randint, choice # 定义颜色常量 class color: White = (255, 255, 255) Black = (0, 0, 0) Red = (255, 0, 0) # 设置迷宫的大小 room_m = 50 room_n = 35 room_size = 15 # 每个小房间的大小 steps = 0 # 设置屏幕宽度和高度为全局变量 screen_width = 800 screen_height = 600 # 定义房间类 class room(): def __init__(self, x, y): self.walls = [True, True, True, True] # 初始化地图,小房间四周都是墙 self.visited = False # 初始化小房间都未被访问过 self.x = x self.y = y # 画迷宫 @staticmethod def draw_room(screen, begin_point, walls, size, r_color): n = 0 # 一个小房间的四面墙 for wall in walls: x = begin_point[0] # 迷宫起点的 x 坐标 y = begin_point[1] # 迷宫起点的 y 坐标 n += 1 if n == 1 and wall: # x → x + size 是墙 pygame.draw.line(screen, r_color, (x, y), (x + size, y)) if n == 2 and wall: pygame.draw.line(screen, r_color, (x + size, y), (x + size, y + size)) if n == 3 and wall: pygame.draw.line(screen, r_color, (x + size, y + size), (x, y + size)) if n == 4 and wall: pygame.draw.line(screen, r_color, (x, y + size), (x, y)) # 生成迷宫地图 @staticmethod def creat_map(m, n): room_list = [[0 for col in range(n)] for row in range(m)] # 二维数组: m*n for i in range(m): for j in range(n): room_list[i][j] = room(i, j) return room_list # 获取下一个房间 @staticmethod def get_next_room(room_list, room): temp_rooms = {1: None, 2: None, 3: None, 4: None} temp_room_count = 0 # 判断上下左右四个方向的小房间有没有被访问,没有的话就加入 temp_rooms[] if (not room.y - 1 < 0) and (not room_list[room.x][room.y - 1].visited): # 上边没被访问 temp_rooms[1] = room_list[room.x][room.y - 1] temp_room_count += 1 if (not room.x + 1 > room_m - 1) and (not room_list[room.x + 1][room.y].visited): # 右边 temp_rooms[2] = room_list[room.x + 1][room.y] temp_room_count += 1 if (not room.y + 1 > room_n - 1) and (not room_list[room.x][room.y + 1].visited): # 下边 temp_rooms[3] = room_list[room.x][room.y + 1] temp_room_count += 1 if (not room.x - 1 < 0) and (not room_list[room.x - 1][room.y].visited): # 左边 temp_rooms[4] = room_list[room.x - 1][room.y] temp_room_count += 1 if temp_room_count > 0: do = True while do: room_id = randint(1, 4) # 随机生成,指定某一边没有墙 if temp_rooms[room_id]: do = False next_room = temp_rooms[room_id] if room_id == 1: room.walls[0] = 0 # 当前房间的上边没有墙 next_room.walls[2] = 0 # 上面房间的下边没有墙 if room_id == 2: room.walls[1] = 0 next_room.walls[3] = 0 if room_id == 3: room.walls[2] = 0 next_room.walls[0] = 0 if room_id == 4: room.walls[3] = 0 next_room.walls[1] = 0 return next_room else: return None # 创建迷宫 @staticmethod def creat_migong(room_list, next_room, temp_yes_rooms=[]): while True: if next_room: # 下一房间未被访问 if not next_room.visited: next_room.visited = True temp_yes_rooms.append(next_room) next_room = room.get_next_room(room_list, next_room) else: next_room = temp_yes_rooms.pop() # 否则出栈 if len(temp_yes_rooms) == 0: break # 输出文本信息 def print_text(font, x, y, text, color, shadow=True): if shadow: imgText = font.render(text, True, (0, 0, 0)) screen.blit(imgText, (x - 2, y - 2)) imgText = font.render(text, True, color) screen.blit(imgText, (x, y)) # 处理角色移动的函数 def handle_movement(r_list, roomx, roomy, x, y, event): new_x = x new_y = y new_roomx = roomx new_roomy = roomy if event.key == pygame.K_RIGHT: # 右边无墙 if not r_list[roomx][roomy].walls[1]: new_x += room_size new_roomx += 1 # 计房间数 # 右边有墙 elif event.key == pygame.K_LEFT: # 左边无墙 if not r_list[roomx][roomy].walls[3]: new_x -= room_size new_roomx -= 1 elif event.key == pygame.K_UP: # 上边无墙 if not r_list[roomx][roomy].walls[0]: new_y -= room_size new_roomy -= 1 elif event.key == pygame.K_DOWN: # 下边无墙 if not r_list[roomx][roomy].walls[2]: new_y += room_size new_roomy += 1 return new_x, new_y, new_roomx, new_roomy # 游戏开始 if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode([screen_width, screen_height]) pygame.display.set_caption('Maze_AI——by Wonz') # 游戏标题 font1 = pygame.font.Font(None, 32) font2 = pygame.font.Font(None, 32) font3 = pygame.font.Font(None, 32) font4 = pygame.font.Font(None, 32) # 文字 clock = pygame.time.Clock() fps = 20 screen.fill(color.White) r_list = room.creat_map(room_m, room_n) begin_point = [0, 0] begin_room = r_list[0][0] room.creat_migong(r_list, begin_room) # 画出去起点和终点的其他点 for i in range(room_m): for j in range(room_n): begin_point[0] = 25 + i * room_size begin_point[1] = 25 + j * room_size r_color = color.Black room.draw_room(screen, begin_point, r_list[i][j].walls, room_size, r_color) # 画起点 room.draw_room(screen, [25, 25], [0, 0, 0, 1], room_size, color.White) # 画终点 room.draw_room(screen, [25 + (room_m - 1) * room_size, 25 + (room_n - 1) * room_size], [0, 1, 0, 0], room_size, color.White) # 绘制角色(用矩形代替图片) x = 27 y = 30 roomx = 0 roomy = 0 character_width = 8 character_height = 8 pygame.draw.rect(screen, color.Black, (x, y, character_width, character_height)) # 键盘控制角色移动 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # 按 Esc 键退出游戏 elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: sys.exit() # 走到终点 elif roomx == room_m - 1 and roomy == room_n - 1: print_text(font4, 350, 350, "Win", color.Red) # 键盘响应 elif event.type == pygame.KEYDOWN: x, y, roomx, roomy = handle_movement(r_list, roomx, roomy, x, y, event) # 清除角色原来的位置 screen.fill(color.White) # 重新绘制迷宫 for i in range(room_m): for j in range(room_n): begin_point[0] = 25 + i * room_size begin_point[1] = 25 + j * room_size r_color = color.Black room.draw_room(screen, begin_point, r_list[i][j].walls, room_size, r_color) # 画起点 room.draw_room(screen, [25, 25], [0, 0, 0, 1], room_size, color.White) # 画终点 room.draw_room(screen, [25 + (room_m - 1) * room_size, 25 + (room_n - 1) * room_size], [0, 1, 0, 0], room_size, color.White) # 绘制新位置的角色 pygame.draw.rect(screen, color.Black, (x, y, character_width, character_height)) # 更新屏幕 pygame.display.flip() clock.tick(fps)