import tkinter as tk
import random

# 迷宫基础配置
CELL_SIZE = 30  # 每个格子像素大小
MAZE_WIDTH = 21  # 迷宫列数（必须奇数）
MAZE_HEIGHT = 21  # 迷宫行数（必须奇数）

class MazeGame:
    def __init__(self, root):
        self.root = root
        self.root.title("迷宫小游戏")
        # 画布尺寸
        canvas_w = CELL_SIZE * MAZE_WIDTH
        canvas_h = CELL_SIZE * MAZE_HEIGHT
        self.canvas = tk.Canvas(root, width=canvas_w, height=canvas_h, bg="white")
        self.canvas.pack(padx=10, pady=10)

        # 玩家坐标（起点）
        self.player_x = 1
        self.player_y = 1
        # 终点坐标
        self.end_x = MAZE_WIDTH - 2
        self.end_y = MAZE_HEIGHT - 2

        # 生成迷宫地图
        self.maze_map = self.create_maze()
        self.draw_maze()
        self.draw_player()

        # 绑定键盘方向键
        root.bind("<Up>", self.move_up)
        root.bind("<Down>", self.move_down)
        root.bind("<Left>", self.move_left)
        root.bind("<Right>", self.move_right)

    # 深度优先随机生成迷宫
    def create_maze(self):
        # 初始化全墙：1=墙，0=通路
        maze = [[1 for _ in range(MAZE_WIDTH)] for _ in range(MAZE_HEIGHT)]

        def dfs(x, y):
            maze[y][x] = 0
            # 四个方向打乱顺序随机走
            directions = [(0, -2), (0, 2), (-2, 0), (2, 0)]
            random.shuffle(directions)
            for dx, dy in directions:
                nx = x + dx
                ny = y + dy
                if 0 < nx < MAZE_WIDTH - 1 and 0 < ny < MAZE_HEIGHT - 1 and maze[ny][nx] == 1:
                    # 打通中间墙体
                    maze[y + dy//2][x + dx//2] = 0
                    dfs(nx, ny)

        dfs(1, 1)
        return maze

    # 绘制整个迷宫墙壁、终点
    def draw_maze(self):
        self.canvas.delete("all")
        for y in range(MAZE_HEIGHT):
            for x in range(MAZE_WIDTH):
                x1 = x * CELL_SIZE
                y1 = y * CELL_SIZE
                x2 = x1 + CELL_SIZE
                y2 = y1 + CELL_SIZE
                if self.maze_map[y][x] == 1:
                    # 黑色墙
                    self.canvas.create_rectangle(x1, y1, x2, y2, fill="#333333")
                else:
                    # 白色通路
                    self.canvas.create_rectangle(x1, y1, x2, y2, fill="white", outline="#cccccc")
        # 绘制终点（绿色方块）
        ex1 = self.end_x * CELL_SIZE
        ey1 = self.end_y * CELL_SIZE
        ex2 = ex1 + CELL_SIZE
        ey2 = ey1 + CELL_SIZE
        self.canvas.create_rectangle(ex1, ey1, ex2, ey2, fill="#4cd964", tags="end")

    # 绘制玩家（红色圆形）
    def draw_player(self):
        px1 = self.player_x * CELL_SIZE + 3
        py1 = self.player_y * CELL_SIZE + 3
        px2 = px1 + CELL_SIZE - 6
        py2 = py1 + CELL_SIZE - 6
        self.canvas.delete("player")
        self.canvas.create_oval(px1, py1, px2, py2, fill="#ff3b30", tags="player")

    # 检测是否撞墙
    def can_move(self, x, y):
        if 0 <= x < MAZE_WIDTH and 0 <= y < MAZE_HEIGHT:
            return self.maze_map[y][x] == 0
        return False

    # 移动逻辑
    def move_up(self, event):
        if self.can_move(self.player_x, self.player_y - 1):
            self.player_y -= 1
            self.draw_player()
            self.check_win()

    def move_down(self, event):
        if self.can_move(self.player_x, self.player_y + 1):
            self.player_y += 1
            self.draw_player()
            self.check_win()

    def move_left(self, event):
        if self.can_move(self.player_x - 1, self.player_y):
            self.player_x -= 1
            self.draw_player()
            self.check_win()

    def move_right(self, event):
        if self.can_move(self.player_x + 1, self.player_y):
            self.player_x += 1
            self.draw_player()
            self.check_win()

    # 判断通关
    def check_win(self):
        if self.player_x == self.end_x and self.player_y == self.end_y:
            tk.messagebox.showinfo("通关成功", "恭喜！成功走出迷宫！\n关闭弹窗会自动重新生成新迷宫")
            # 重置迷宫
            self.reset_game()

    # 重置游戏，生成新迷宫
    def reset_game(self):
        self.player_x = 1
        self.player_y = 1
        self.maze_map = self.create_maze()
        self.draw_maze()
        self.draw_player()

if __name__ == "__main__":
    import tkinter.messagebox as messagebox
    main_window = tk.Tk()
    game = MazeGame(main_window)
    main_window.mainloop()