# GESP等级：四级 | GESP Python 四级考点
"""
================================================================
  第一关: 基本画图 (★☆☆☆☆)
  学习目标:
    1. 掌握 turtle 基本绘图指令
    2. 理解事件驱动: 鼠标点击画图
    3. 学会绘制基础图形 (点、线、圆、矩形、三角形)
================================================================
"""

import turtle

# =============================================================
# 一、画布初始化
# =============================================================
def setup_screen():
    """创建并配置画布"""
    screen = turtle.Screen()
    screen.title("画图编辑器 - 第一关: 基本画图")
    screen.bgcolor("white")
    screen.setup(width=800, height=600)
    # 设置坐标范围: 左下 (-400, -300) 到右上 (400, 300)
    screen.setworldcoordinates(-400, -300, 400, 300)
    return screen


def setup_pen():
    """创建并配置画笔"""
    pen = turtle.Turtle()
    pen.shape("circle")
    pen.shapesize(0.5)  # 小光标，不遮挡画面
    pen.speed(0)        # 最快速度
    pen.color("black")
    pen.pensize(2)
    pen.penup()         # 移动时不画线
    return pen


# =============================================================
# 二、基础绘图函数
# =============================================================

def draw_dot(pen, x, y):
    """在 (x, y) 位置画一个实心圆点"""
    pen.goto(x, y)
    pen.dot(10)


def draw_line(pen, x1, y1, x2, y2):
    """从 (x1,y1) 到 (x2,y2) 画一条直线"""
    pen.penup()
    pen.goto(x1, y1)
    pen.pendown()
    pen.goto(x2, y2)
    pen.penup()


def draw_circle(pen, x, y, radius=50):
    """以 (x, y) 为圆心画圆"""
    pen.penup()
    pen.goto(x, y - radius)  # 移到圆的底部起点
    pen.pendown()
    pen.circle(radius)
    pen.penup()


def draw_rectangle(pen, x, y, width=100, height=60):
    """以 (x, y) 为左上角画矩形"""
    pen.penup()
    pen.goto(x, y)
    pen.pendown()
    for _ in range(2):
        pen.forward(width)
        pen.right(90)
        pen.forward(height)
        pen.right(90)
    pen.penup()


def draw_triangle(pen, x, y, side=80):
    """以 (x, y) 为左下角画等边三角形"""
    pen.penup()
    pen.goto(x, y)
    pen.pendown()
    for _ in range(3):
        pen.forward(side)
        pen.left(120)
    pen.penup()


# =============================================================
# 三、事件处理 —— 鼠标点击画图
# =============================================================

def on_click_handler(x, y):
    """
    鼠标点击事件回调函数。
    每关演示一种图形:
    第一关：点击画一个圆（简单直观）
    """
    draw_circle(pen, x, y, radius=40)
    # 在圆心标注点击位置（方便观察）
    pen.goto(x, y - 45)
    pen.write(f"({int(x)},{int(y)})", align="center",
              font=("Arial", 8, "normal"))


# =============================================================
# 四、提示信息绘制
# =============================================================

def show_instructions():
    """在画布上显示操作提示"""
    pen.penup()
    pen.goto(0, 260)
    pen.color("gray")
    pen.write("【第一关】点击画布任意位置 → 画一个圆 (半径40)",
              align="center", font=("SimHei", 14, "bold"))
    pen.goto(0, 230)
    pen.write("按键盘 'c' 清空画布  |  按 'q' 退出程序",
              align="center", font=("SimHei", 12, "normal"))
    pen.color("black")


# =============================================================
# 五、键盘控制
# =============================================================

def clear_canvas():
    """清空画布（保留设置）"""
    pen.clear()
    show_instructions()


def exit_program():
    """退出程序"""
    turtle.bye()


# =============================================================
# 六、主程序入口
# =============================================================

def main():
    global pen
    # 1. 创建画布和画笔
    screen = setup_screen()
    pen = setup_pen()

    # 2. 显示提示
    show_instructions()

    # 3. 绑定事件
    screen.onclick(on_click_handler)    # 鼠标点击
    screen.onkey(clear_canvas, "c")     # 按 c 清空
    screen.onkey(exit_program, "q")     # 按 q 退出
    screen.listen()                     # 监听键盘事件

    # 4. 启动消息循环（保持窗口打开）
    screen.mainloop()


# 程序从这里开始运行
if __name__ == "__main__":
    main()
