import tkinter as tk
from tkinter import messagebox
import random

# --- 1. 配置参数 ---
CELL_SIZE = 30  # 每个格子的像素大小
ROWS = 15       # 迷宫行数
COLS = 20       # 迷宫列数
WIDTH = COLS * CELL_SIZE
HEIGHT = ROWS * CELL_SIZE

class MazeGame:
    def __init__(self, root):
        self.root = root
        self.root.title("随机迷宫大冒险")
        
        # 游戏状态
        self.maze = []
        self.player_pos = [0, 0] # [行, 列]
        
        # 界面初始化
        self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
        self.canvas.pack(padx=10, pady=10)
        
        # 绑定按键
        self.root.bind("<KeyPress>", self.move_player)
        
        # 底部控制栏
        btn_frame = tk.Frame(root)
        btn_frame.pack(fill=tk.X, pady=5)
        ttk_btn = tk.Button(btn_frame, text="生成新迷宫", command=self.new_game)
        ttk_btn.pack()

        self.new_game()

    def generate_maze(self, rows, cols):
        """使用DFS算法生成随机迷宫 (1代表墙, 0代表路)"""
        # 初始化全是墙
        maze = [[1] * cols for _ in range(rows)]
        
        def walk(r, c):
            maze[r][c] = 0 # 标记当前点为路
            
            # 随机打乱四个方向
            dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
            random.shuffle(dirs)
            
            for dr, dc in dirs:
                nr, nc = r + dr*2, c + dc*2 # 检查隔壁的隔壁
                if 0 <= nr < rows and 0 <= nc < cols and maze[nr][nc] == 1:
                    maze[r + dr][c + dc] = 0 # 打通中间的墙
                    walk(nr, nc)

        walk(0, 0)
        # 确保终点是开通的
        maze[rows-1][cols-1] = 0
        return maze

    def draw_maze(self):
        self.canvas.delete("all")
        for r in range(ROWS):
            for c in range(COLS):
                if self.maze[r][c] == 1:
                    x1, y1 = c * CELL_SIZE, r * CELL_SIZE
                    x2, y2 = x1 + CELL_SIZE, y1 + CELL_SIZE
                    self.canvas.create_rectangle(x1, y1, x2, y2, fill="#2c3e50", outline="#34495e")
        
        # 画终点
        ex1, ey1 = (COLS-1) * CELL_SIZE + 5, (ROWS-1) * CELL_SIZE + 5
        ex2, ey2 = (COLS) * CELL_SIZE - 5, (ROWS) * CELL_SIZE - 5
        self.canvas.create_oval(ex1, ey1, ex2, ey2, fill="#e74c3c", outline="")
        self.canvas.create_text((COLS-0.5)*CELL_SIZE, (ROWS-0.5)*CELL_SIZE, text="终点", fill="white", font=("微软雅黑", 8))

        # 画玩家
        self.player_img = self.canvas.create_oval(5, 5, CELL_SIZE-5, CELL_SIZE-5, fill="#3498db", outline="")

    def new_game(self):
        self.maze = self.generate_maze(ROWS, COLS)
        self.player_pos = [0, 0]
        self.draw_maze()

    def move_player(self, event):
        r, c = self.player_pos
        if event.keysym == "Up":    nr, nc = r - 1, c
        elif event.keysym == "Down":  nr, nc = r + 1, c
        elif event.keysym == "Left":  nr, nc = r, c - 1
        elif event.keysym == "Right": nr, nc = r, c + 1
        else: return

        # 碰撞检测
        if 0 <= nr < ROWS and 0 <= nc < COLS and self.maze[nr][nc] == 0:
            self.player_pos = [nr, nc]
            # 更新玩家位置图形
            x1, y1 = nc * CELL_SIZE + 5, nr * CELL_SIZE + 5
            x2, y2 = (nc + 1) * CELL_SIZE - 5, (nr + 1) * CELL_SIZE - 5
            self.canvas.coords(self.player_img, x1, y1, x2, y2)
            
            # 胜利检测
            if self.player_pos == [ROWS-1, COLS-1]:
                messagebox.showinfo("恭喜", "你成功逃出了迷宫！")
                self.new_game()

# --- 启动 ---
if __name__ == "__main__":
    root = tk.Tk()
    game = MazeGame(root)
    root.mainloop()