import pygame
import random
import sys

# 初始化
pygame.init()
WIDTH, HEIGHT = 600, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Maze Game")
clock = pygame.time.Clock()
FPS = 60

# 颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (70, 70, 70)
GREEN = (0, 200, 0)
RED = (220, 20, 20)
BLUE = (30, 130, 255)

# 迷宫设置
CELL_SIZE = 30
cols = WIDTH // CELL_SIZE
rows = HEIGHT // CELL_SIZE

# 墙壁数据 [上,右,下,左]
walls = [[[True, True, True, True] for _ in range(cols)] for _ in range(rows)]
visited = [[False for _ in range(cols)] for _ in range(rows)]

# 玩家坐标
px, py = 0, 0
end_x, end_y = cols - 1, rows - 1
is_win = False

# DFS生成迷宫
def create_maze(x, y):
    visited[y][x] = True
    dirs = [
        (0, -1, 0, 2),
        (1, 0, 1, 3),
        (0, 1, 2, 0),
        (-1, 0, 3, 1)
    ]
    random.shuffle(dirs)
    for dx, dy, wall_self, wall_nei in dirs:
        nx = x + dx
        ny = y + dy
        if 0 <= nx < cols and 0 <= ny < rows and not visited[ny][nx]:
            walls[y][x][wall_self] = False
            walls[ny][nx][wall_nei] = False
            create_maze(nx, ny)

# 生成初始迷宫
create_maze(0, 0)

# 判断是否可以移动
def check_move(x, y, direction):
    return not walls[y][x][direction]

# 绘制画面
def draw_scene():
    screen.fill(BLACK)
    # 绘制墙壁
    for y in range(rows):
        for x in range(cols):
            pos_x = x * CELL_SIZE
            pos_y = y * CELL_SIZE
            if walls[y][x][0]:
                pygame.draw.line(screen, GRAY, (pos_x, pos_y), (pos_x + CELL_SIZE, pos_y), 3)
            if walls[y][x][1]:
                pygame.draw.line(screen, GRAY, (pos_x + CELL_SIZE, pos_y), (pos_x + CELL_SIZE, pos_y + CELL_SIZE), 3)
            if walls[y][x][2]:
                pygame.draw.line(screen, GRAY, (pos_x, pos_y + CELL_SIZE), (pos_x + CELL_SIZE, pos_y + CELL_SIZE), 3)
            if walls[y][x][3]:
                pygame.draw.line(screen, GRAY, (pos_x, pos_y), (pos_x, pos_y + CELL_SIZE), 3)
    # 起点、终点方块
    pygame.draw.rect(screen, GREEN, (2, 2, CELL_SIZE - 4, CELL_SIZE - 4))
    pygame.draw.rect(screen, RED, (end_x*CELL_SIZE + 2, end_y*CELL_SIZE + 2, CELL_SIZE - 4, CELL_SIZE - 4))
    # 玩家圆点
    player_center_x = px * CELL_SIZE + CELL_SIZE // 2
    player_center_y = py * CELL_SIZE + CELL_SIZE // 2
    pygame.draw.circle(screen, BLUE, (player_center_x, player_center_y), CELL_SIZE // 3)

# 游戏主循环
running = True
while running:
    clock.tick(FPS)

    # 事件循环
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            # ESC退出
            if event.key == pygame.K_ESCAPE:
                running = False
            # R重置迷宫
            if event.key == pygame.K_r:
                walls = [[[True, True, True, True] for _ in range(cols)] for _ in range(rows)]
                visited = [[False for _ in range(cols)] for _ in range(rows)]
                create_maze(0, 0)
                px, py = 0, 0
                is_win = False

            # 方向键移动
            if not is_win:
                if event.key == pygame.K_UP and check_move(px, py, 0):
                    py -= 1
                if event.key == pygame.K_RIGHT and check_move(px, py, 1):
                    px += 1
                if event.key == pygame.K_DOWN and check_move(px, py, 2):
                    py += 1
                if event.key == pygame.K_LEFT and check_move(px, py, 3):
                    px -= 1

    # 检测通关
    if px == end_x and py == end_y:
        is_win = True

    draw_scene()

    # 通关文字（使用系统默认字体，百分百不会报错）
    if is_win:
        win_font = pygame.font.Font(None, 36)
        text = win_font.render("YOU WIN! Press R to restart", True, GREEN)
        screen.blit(text, (WIDTH//2 - text.get_width()//2, HEIGHT//2))

    pygame.display.update()

pygame.quit()
sys.exit()