import random
import tkinter as tk

CELL = 30
WIDTH = 21
HEIGHT = 21

# 古风配色
COLOR_WALL = "#4A3F35"
COLOR_ROAD = "#F3E9D7"
COLOR_PLAYER = "#C84242"
COLOR_END = "#D4AF37"


class AncientMaze:
    def __init__(self, root):
        self.root = root
        self.canvas = tk.Canvas(root, width=CELL * WIDTH, height=CELL * HEIGHT, bg="#2c241c")
        self.canvas.pack()
        self.maze = [[1 for _ in range(WIDTH)] for _ in range(HEIGHT)]
        self.px, self.py = 1, 1
        self.end_x, self.end_y = WIDTH - 2, HEIGHT - 2
        self.create_maze()
        self.draw_map()
        root.bind("<Key>", self.key_move)

    def create_maze(self):
        stack = [(1, 1)]
        self.maze[1][1] = 0
        dirs = [(0, -2), (2, 0), (0, 2), (-2, 0)]
        while stack:
            x, y = stack[-1]
            random.shuffle(dirs)
            ok = False
            for dx, dy in dirs:
                nx = x + dx
                ny = y + dy
                if 0 < nx < WIDTH - 1 and 0 < ny < HEIGHT - 1 and self.maze[ny][nx] == 1:
                    self.maze[y + dy // 2][x + dx // 2] = 0
                    self.maze[ny][nx] = 0
                    stack.append((nx, ny))
                    ok = True
                    break
            if not ok:
                stack.pop()

    def draw_map(self):
        self.canvas.delete("all")
        for y in range(HEIGHT):
            for x in range(WIDTH):
                x1 = x * CELL
                y1 = y * CELL
                x2 = x1 + CELL
                y2 = y1 + CELL
                if self.maze[y][x] == 1:
                    self.canvas.create_rectangle(x1, y1, x2, y2, fill=COLOR_WALL)
                else:
                    self.canvas.create_rectangle(x1, y1, x2, y2, fill=COLOR_ROAD)
        # 终点宝物
        ex = self.end_x * CELL
        ey = self.end_y * CELL
        self.canvas.create_oval(ex + 3, ey + 3, ex + CELL - 3, ey + CELL - 3, fill=COLOR_END)
        # 玩家
        self.player = self.canvas.create_oval(
            self.px * CELL + 4, self.py * CELL + 4,
            self.px * CELL + CELL - 4, self.py * CELL + CELL - 4,
            fill=COLOR_PLAYER
        )

    def key_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
        else:
            return
        nx = self.px + dx
        ny = self.py + dy
        if 0 <= nx < WIDTH and 0 <= ny < HEIGHT and self.maze[ny][nx] == 0:
            self.px, self.py = nx, ny
            self.canvas.move(self.player, dx * CELL, dy * CELL)
        if self.px == self.end_x and self.py == self.end_y:
            self.canvas.create_text(WIDTH * CELL // 2, HEIGHT * CELL // 2,
                                   text="寻宝成功！", font=("宋体", 30), fill="#D4AF37")


if __name__ == "__main__":
    win = tk.Tk()
    win.title("古风秘境迷宫")
    app = AncientMaze(win)
    win.mainloop()