Python 启蒙 · 参考资料

Python Turtle
函数速查手册

涵盖本课程全部 8 节所用指令 · 附常用颜色 / 角度 / 按键速查 · 适合课堂参考与自学

课程用到进阶扩展注意事项

移动控制

8 个函数
t.forward(distance)

向当前朝向前进 distance 个像素。别名:fd()

t.forward(100)  # 前进 100 像素
t.fd(50)       # 等同 forward(50)
第1节起
t.backward(distance)

向当前朝向的反方向后退 distance 个像素。别名:back() / bk()

t.backward(50)  # 后退 50 像素
t.left(angle)

向左(逆时针)旋转 angle 度,不移动位置。别名:lt()

t.left(90)   # 左转 90 度(朝上)
t.left(120)  # 三角形用
第1节起
t.right(angle)

向右(顺时针)旋转 angle 度。别名:rt()

t.right(90)  # 右转 90 度
t.goto(x, y)

直接跳到坐标 (x, y)。原点 (0, 0) 在屏幕中心,右/上为正方向。笔放下时会画线,需配合 penup() 使用。别名:setposition() / setpos()

t.penup()
t.goto(0, 100)   # 跳到上方 100 像素
t.goto(-200, 50)  # 跳到左侧
t.pendown()
第3节起
t.setx(x) / t.sety(y)

只改变 x 或只改变 y 坐标,另一个轴不变。游戏移动控制常用。

t.sety(t.ycor() + 20)  # 向上 20 像素
t.setx(t.xcor() - 20)  # 向左 20 像素
第7节起
t.setheading(angle)

设置绝对朝向角度。0=右,90=上,180=左,270=下。别名:seth()

t.setheading(0)    # 朝右
t.setheading(90)   # 朝上
t.setheading(180)  # 朝左
t.setheading(270)  # 朝下
第3节起
t.home()

回到原点 (0, 0) 并将朝向重置为 0°(朝右)。

t.home()  # 回到屏幕中心,朝向归零

画笔开关

5 个函数
t.penup()

抬起画笔——移动时不留痕迹。别名:pu() / up()

t.penup()
t.goto(100, 100)  # 移动不画线
第3节起
t.pendown()

放下画笔——移动时开始画线。别名:pd() / down()

t.pendown()
t.forward(100)  # 画一条线
第3节起
t.pensize(width)

设置画笔宽度(像素)。默认为 1。别名:width()

t.pensize(3)   # 粗线条
t.pensize(1)   # 细线条(默认)
t.clear()

清除该 turtle 画的所有内容(包括写的文字),但不移动位置。

pen.clear()         # 清除旧分数再重写
pen.write(f"分数:{score}")
第8节
t.reset()

清除该 turtle 画的所有内容,并将其重置到初始状态(原点、朝右)。

t.reset()  # 清除 + 回到原点

颜色 · 样式

5 个函数
t.color(color)

同时设置画笔颜色和填充颜色。支持颜色名(英文)或十六进制字符串。

t.color("red")       # 颜色名
t.color("#ff4444")   # 十六进制
t.color("pen_c", "fill_c")  # 分别设置
第1节起
t.pencolor(color)

只设置画笔(线条)颜色,不影响填充色。

t.pencolor("blue")    # 线蓝色
t.fillcolor("yellow")  # 填黄色
t.speed(speed)

设置绘图速度。0 = 瞬间完成(最快),1 = 最慢,10 = 很快。

t.speed(0)   # 瞬间完成(推荐游戏用)
t.speed(1)   # 最慢,可以看过程
t.speed(5)   # 中等速度
第1节起
t.shape(name)

设置 turtle 的外观形状。可选:"turtle" "arrow" "circle" "square" "triangle" "classic"

player.shape("turtle")  # 小海龟形状
player.shape("arrow")   # 箭头
player.shape("circle")  # 圆形
第7节起
t.shapesize(stretch_wid, stretch_len)

缩放 turtle 的形状大小。默认为 1.0。0.5 = 缩小一半,2 = 放大两倍。

star.shapesize(0.8)     # 稍微缩小
player.shapesize(1.5)  # 放大 1.5 倍
第8节

填色

3 个函数
用法固定:填色三步走:① fillcolor(颜色) → ② begin_fill() → ③ 画图形 → ④ end_fill()
t.fillcolor(color)

设置填充颜色(与画笔颜色独立)。必须在 begin_fill() 之前调用。

t.fillcolor("lightyellow")  # 设置填色
第3节起
t.begin_fill()

开始记录填色区域。从此之后画的封闭图形会被填色。

t.fillcolor("tomato")
t.begin_fill()       # 开始记录
for _ in range(4): t.forward(100); t.left(90)
t.end_fill()         # 填充完成
第3节起
t.end_fill()

结束填色区域记录,将封闭区域用 fillcolor 的颜色填满。必须与 begin_fill() 配对使用。

t.end_fill()  # 和 begin_fill 配对
第3节起

坐标 · 信息查询

5 个函数
t.xcor() → float

返回 turtle 当前的 x 坐标(水平位置)。

x = player.xcor()
player.setx(x + 20)  # 向右移动
第7节起
t.ycor() → float

返回 turtle 当前的 y 坐标(垂直位置)。

y = player.ycor()
player.sety(y + 20)  # 向上移动
第7节起
t.pos() → (x, y)

返回当前位置的 (x, y) 元组。别名:position()

x, y = t.pos()
print(t.pos())  # (100.0, 50.0)
t.heading() → float

返回当前朝向角度(0–360)。

angle = t.heading()  # 当前角度
t.distance(other) → float

返回与另一个 turtle 的像素距离。用于碰撞检测:距离 < 阈值 = 碰到了。

if player.distance(star) < 20:
    # 碰到了!
第8节

文字写入

2 个函数
t.write(arg, move=False, align="left", font=("Arial",8,"normal"))

在当前位置写文字。font 参数格式为 (字体名, 大小, 样式),样式可选 "normal" "bold" "italic"。align 可选 "left" "center" "right"。

t.write("你好!", font=("Arial", 20, "bold"))
pen.write(f"分数: {score}", font=("Arial", 16, "bold"))
pen.write("🏆 你赢了!", align="center", font=("Arial", 36, "bold"))
第8节
t.hideturtle() / showturtle()

隐藏或显示 turtle 箭头本身。画背景的画笔画完后建议隐藏,画面更干净。别名:ht() / st()

t.hideturtle()  # 画完背景后隐藏
pen.hideturtle() # 计分笔也隐藏
第7节起

键盘事件

4 个函数
必须按顺序:① 定义函数 → ② onkey(函数名, "键名") → ③ listen() → ④ mainloop()。缺少 listen() 则按键完全无响应。
fn 不加括号!
turtle.onkey(fn, "key")

将函数 fn 绑定到按键松开事件。fn 后不加括号!同一按键可重复绑定新函数覆盖旧的。传 None 解除绑定。

turtle.onkey(move_up, "Up")    # ✅ 不加括号
turtle.onkey(move_up(), "Up")  # ❌ 错误写法
turtle.onkey(None, "Up")     # 解除绑定
第7节起
turtle.onkeypress(fn, "key")

按住不放时持续触发(onkey 只在松开时触发一次)。角色连续移动用这个更流畅。

turtle.onkeypress(move_up, "Up")
# 按住↑键,角色持续向上移动
必须写!
turtle.listen()

让窗口开始接收键盘输入。必须在 onkey 之后调用,否则按键事件不会被捕获。

turtle.onkey(move_up, "Up")
turtle.listen()    # 开启监听,缺了就没反应
第7节起
turtle.ontimer(fn, ms)

延迟 ms 毫秒后调用 fn 一次。1000ms = 1 秒。常用于倒计时、自动更新。

def countdown():
    # 更新倒计时
    turtle.ontimer(countdown, 1000)  # 1秒后再次调用
countdown()  # 启动

窗口 · 程序控制

7 个函数
turtle.done() / mainloop()

保持窗口不关闭。done():等待用户关闭窗口。mainloop():进入事件循环,可响应 onkey 等事件。需要键盘交互时必须用 mainloop()。

turtle.done()      # 静态程序用这个
turtle.mainloop()  # 有键盘交互用这个
第1节 / 第7节
turtle.bgcolor(color)

设置整个画布的背景颜色。

turtle.bgcolor("lightyellow")
turtle.bgcolor("midnightblue")  # 夜晚
turtle.bgcolor("black")
第4节起
turtle.title(titlestring)

设置窗口标题栏的文字。

turtle.title("我的村子游戏")
turtle.setup(width, height)

设置窗口大小(像素)。默认大约 600×600。

turtle.setup(800, 600)  # 宽800 高600
turtle.Turtle() → Turtle

创建一个新的 turtle 对象。可以有多个(如画图用 t,角色用 player,计分用 pen)。

t      = turtle.Turtle()  # 画背景
player = turtle.Turtle()  # 角色
pen    = turtle.Turtle()  # 计分板
star   = turtle.Turtle()  # 收集物
第1节起
进阶优化
turtle.tracer(n, delay)

控制动画更新频率。tracer(0) 关闭自动更新(配合 update() 手动刷新),用于加速复杂绘图。

turtle.tracer(0)        # 关闭自动更新
# ... 大量绘图代码 ...
turtle.update()        # 手动刷新一次
turtle.screensize() → (w, h)

返回或设置画布大小。坐标范围由画布大小决定。

w, h = turtle.screensize()
print(w, h)  # 当前画布宽高

常用颜色速查

所有颜色名均为英文字符串,大小写不敏感。可直接填入 color() / fillcolor() / bgcolor()。

基础色

red
blue
green
yellow
orange
purple
pink
black
white
gray

课程常用色

tomato
lightyellow
lightblue
lightgreen
lavender
peachpuff
gold
sienna
royalblue
ivory

扩展色

crimson
coral
salmon
hotpink
magenta
violet
indigo
cyan
teal
turquoise
lime
darkgreen
olive
khaki
tan
peru
chocolate
maroon
midnightblue
navy
skyblue
steelblue
slategray
dimgray
silver
beige

角度速查

setheading 绝对朝向

Right90°Up180°Left270°Down
t.setheading(角度)

正多边形转角公式

转角 = 360 ÷ 边数

图形边数转角循环写法
等边三角形3120°range(3), left(120)
正方形490°range(4), left(90)
正五边形572°range(5), left(72)
正六边形660°range(6), left(60)
正八边形845°range(8), left(45)
近似圆360range(360), left(1)

按键名速查

填入 turtle.onkey(fn, "按键名") 的第二个参数。

方向键

按键字符串
↑ 上"Up"
↓ 下"Down"
← 左"Left"
→ 右"Right"

特殊键

按键字符串
空格键"space"
回车键"Return"
退格键"BackSpace"
Escape"Escape"

字母键

按键字符串
w / a / s / d"w" / "a" / "s" / "d"
任意小写字母"字母本身"

数字键

按键字符串
0 – 9"0" 到 "9"

形状速查

填入 t.shape("形状名")

"turtle"

海龟形

"arrow"

箭头形

"circle"

圆形

"square"

正方形

"triangle"

三角形

"classic"

经典箭头

全函数速查表

所有函数一览,适合打印贴在桌旁。

函数作用示例课程
移动控制
t.forward(n)向前走 n 像素t.forward(100)第1节
t.backward(n)向后走 n 像素t.backward(50)
t.left(angle)左转 angle 度t.left(90)第1节
t.right(angle)右转 angle 度t.right(90)
t.goto(x, y)跳到坐标 (x, y)t.goto(0, 100)第3节
t.setx(x)只改 x 坐标t.setx(t.xcor()+20)第7节
t.sety(y)只改 y 坐标t.sety(t.ycor()+20)第7节
t.setheading(a)设置绝对朝向角度t.setheading(90)第3节
t.home()回到原点,朝向归零t.home()
画笔开关
t.penup()抬笔(移动不画线)t.penup()第3节
t.pendown()落笔(移动画线)t.pendown()第3节
t.pensize(w)设置线条宽度t.pensize(3)
t.clear()清除该 turtle 的绘制内容pen.clear()第8节
颜色 · 样式
t.color(c)设置画笔+填充颜色t.color("red")第1节
t.pencolor(c)只设置画笔颜色t.pencolor("blue")
t.fillcolor(c)只设置填充颜色t.fillcolor("gold")第3节
t.speed(s)设置速度(0最快,1最慢)t.speed(0)第1节
t.shape(name)设置 turtle 外观t.shape("turtle")第7节
t.shapesize(s)缩放 turtle 大小t.shapesize(0.8)第8节
填色(三步配合)
t.begin_fill()开始记录填色区域t.begin_fill()第3节
t.end_fill()结束并填充t.end_fill()第3节
坐标 · 信息查询
t.xcor()获取当前 x 坐标x = t.xcor()第7节
t.ycor()获取当前 y 坐标y = t.ycor()第7节
t.pos()获取当前 (x, y) 元组t.pos()
t.heading()获取当前朝向角度t.heading()
t.distance(other)与另一个 turtle 的距离t.distance(star)第8节
文字 · 外观
t.write(text, ...)在当前位置写文字t.write("你好")第8节
t.hideturtle()隐藏 turtle 箭头t.hideturtle()第7节
t.showturtle()显示 turtle 箭头t.showturtle()
键盘事件
turtle.onkey(fn,"k")按键松开时触发函数turtle.onkey(f,"Up")第7节
turtle.onkeypress(...)按住时持续触发
turtle.listen()开始监听键盘(必须)turtle.listen()第7节
turtle.ontimer(fn,ms)N 毫秒后调用函数
窗口 · 程序控制
turtle.Turtle()创建新 turtle 对象t = turtle.Turtle()第1节
turtle.done()保持窗口(静态)turtle.done()第1节
turtle.mainloop()进入事件循环(交互)turtle.mainloop()第7节
turtle.bgcolor(c)设置背景颜色turtle.bgcolor("sky")第4节
turtle.title(s)设置窗口标题turtle.title("游戏")
turtle.setup(w,h)设置窗口大小turtle.setup(800,600)
turtle.tracer(n)控制动画更新频率turtle.tracer(0)
Python Turtle 函数速查手册 · 少儿编程课程配套文档 · Python 3.x turtle 标准库