import tkinter as tk
import random

# 迷宫设置
CELL_SIZE = 30
WIDTH = 15
HEIGHT = 15


class MazeGame:
    def __init__(self, root):
        self.root = root
        self.root.title("Python 迷宫游戏")
        self.root.resizable(False, False)

        # 画布
        self.canvas = tk.Canvas(
            root, width=WIDTH*CELL_SIZE, height=HEIGHT*CELL_SIZE, bg="white")
        self.canvas.pack()

        # 迷宫数组
        self.maze = []
        self.player_x = 1
        self.player_y = 1
        self.end_x = WIDTH - 2
        self.end_y = HEIGHT - 2

        self.create_maze()
        self.draw_maze()

        # 绑定按键
        self.root.bind("<Up>", self.move)
        self.root.bind("<Down>", self.move)
        self.root.bind("<Left>", self.move)
        self.root.bind("<Right>", self.move)

    # 随机生成迷宫(深度优先)
    def create_maze(self):
        # 初始化全墙
        self.maze = [[1 for _ in range(WIDTH)] for _ in range(HEIGHT)]
        stack = [(1, 1)]
        self.maze[1][1] = 0

        while stack:
            x, y = stack[-1]
            # 四个方向
            dirs = [(2, 0), (-2, 0), (0, 2), (0, -2)]
            random.shuffle(dirs)
            find = False

            for dx, dy in dirs:
                nx = x + dx
                ny = y + dy
                if 1 <= nx < WIDTH-1 and 1 <= ny < HEIGHT-1 and self.maze[ny][nx] == 1:
                    self.maze[ny][nx] = 0
                    self.maze[y+dy//2][x+dx//2] = 0
                    stack.append((nx, ny))
                    find = True
                    break
            if not find:
                stack.pop()

    # 绘制迷宫
    def draw_maze(self):
        self.canvas.delete("all")
        # 画墙与路
        for y in range(HEIGHT):
            for x in range(WIDTH):
                if self.maze[y][x] == 1:
                    self.canvas.fill = "black"
                    self.canvas.create_rectangle(
                        x*CELL_SIZE, y*CELL_SIZE,
                        (x+1)*CELL_SIZE, (y+1)*CELL_SIZE,
                        fill="black"
                    )
        # 终点 绿色
        self.canvas.create_rectangle(
            self.end_x*CELL_SIZE, self.end_y*CELL_SIZE,
            (self.end_x+1)*CELL_SIZE, (self.end_y+1)*CELL_SIZE,
            fill="#2ecc71"
        )
        # 玩家 蓝色
        self.player = self.canvas.create_rectangle(
            self.player_x*CELL_SIZE+2, self.player_y*CELL_SIZE+2,
            (self.player_x+1)*CELL_SIZE-2, (self.player_y+1)*CELL_SIZE-2,
            fill="#3498db"
        )

    # 移动
    def move(self, event):
        dx, dy = 0, 0
        if event.keysym == "Up":
            dy = -1
        elif event.keysym == "Down":
            dy = 1
        elif event.keysym == "Left":
            dx = -1
        elif event.keysym == "Right":
            dx = 1

        nx = self.player_x + dx
        ny = self.player_y + dy

        # 不能穿墙
        if 0 <= nx < WIDTH and 0 <= ny < HEIGHT and self.maze[ny][nx] == 0:
            self.player_x = nx
            self.player_y = ny
            self.canvas.move(self.player, dx*CELL_SIZE, dy*CELL_SIZE)

            # 到达终点
            if self.player_x == self.end_x and self.player_y == self.end_y:
                self.canvas.create_text(
                    WIDTH*CELL_SIZE//2, HEIGHT*CELL_SIZE//2,
                    text="恭喜通关！", font=("Arial", 20, "bold"), fill="red"
                )


if __name__ == "__main__":
    win = tk.Tk()
    game = MazeGame(win)
    win.mainloop()
