当前位置:首页>python>小学生Python进阶:turtle第5课·坐标与定位

小学生Python进阶:turtle第5课·坐标与定位

  • 2026-10-11 09:32:36
小学生Python进阶:turtle第5课·坐标与定位

上节课我们学会了抬笔,可以跳到新位置再画。但每次跳到哪里,全靠眼力估计。本节课我们学习坐标系统,用精确的数字告诉海龟去哪里!

一、海龟的"地图"长什么样?

turtle 画布是一张二维地图,中心点是原点 (0, 0):

方向规律例子
➡️ 右x 越来越大(100, 0) 在原点的右边
⬅️ 左x 越来越小(-100, 0) 在原点的左边
⬆️ 上y 越来越大(0, 100) 在原点的上边
⬇️ 下y 越来越小(0, -100) 在原点的下边

简单记:x 是"左右",y 是"上下",中间是 (0, 0)。

二、定位命令一览

命令作用示例
t.goto(x, y)跳到坐标 (x, y)t.goto(100, 50)
t.setpos(x, y)同 goto,作用一样t.setpos(0, 200)
t.setx(x)只改 x,y 不变t.setx(100)
t.sety(y)只改 y,x 不变t.sety(80)
t.home()回到原点 (0, 0)t.home()
t.position()查看当前位置print(t.position())
t.xcor()只看 x 坐标print(t.xcor())
t.ycor()只看 y 坐标print(t.ycor())

三、基础练习:画个正方形

用 goto 直接跳到四个角,不用转头:

import turtle

t = turtle.Turtle()
t.pensize(3)

# 从原点出发画正方形
t.goto(100, 0)    # 跳到右边
t.goto(100, 100)  # 跳到右上角
t.goto(0, 100)    # 跳到左上角
t.goto(0, 0)      # 回到原点

turtle.done()

海龟跳到哪画到哪,省去了转头的步骤!🎯

四、实战:画一座小房子 🏠

import turtle

t = turtle.Turtle()
t.pensize(2)
t.speed(5)

# === 画外墙(矩形) ===
t.fillcolor("sandybrown")
t.begin_fill()
t.goto(150, 0)
t.goto(150, 100)
t.goto(0, 100)
t.goto(0, 0)
t.end_fill()

# === 画屋顶(三角形)===
t.fillcolor("firebrick")
t.begin_fill()
t.goto(75, 160)   # 屋顶尖
t.goto(0, 100)    # 左下角
t.goto(150, 100)  # 右下角
t.end_fill()

# === 画门 ===
t.penup()
t.goto(60, 0)
t.pendown()
t.fillcolor("chocolate")
t.begin_fill()
t.goto(60, 50)
t.goto(90, 50)
t.goto(90, 0)
t.end_fill()

# === 画窗户 ===
t.penup()
t.goto(110, 50)
t.pendown()
t.fillcolor("lightskyblue")
t.begin_fill()
t.goto(110, 80)
t.goto(140, 80)
t.goto(140, 50)
t.end_fill()

# === 画烟囱 ===
t.penup()
t.goto(110, 160)
t.pendown()
t.fillcolor("peru")
t.begin_fill()
t.goto(110, 190)
t.goto(130, 190)
t.goto(130, 150)
t.end_fill()

turtle.done()

五、查看当前位置

用 position() 打印坐标,帮你理解海龟在哪:

import turtle

t = turtle.Turtle()

print("起点:", t.position())

t.forward(50)
print("前进50后:", t.position())

t.left(90)
t.forward(50)
print("左转再进50后:", t.position())

t.home()
print("回到原点:", t.position())

turtle.done()

输出示例:

起点: (0.0, 0.0)
前进50后: (50.0, 0.0)
左转再进50后: (50.0, 50.0)
回到原点: (0.0, 0.0)

六、小挑战

  • 🟢 简单:用 goto 画一个长方形(宽100,高60)
  • 🟡 中等:画一座房子 + 一棵树(树用两个圆叠在一起)
  • 🔴 困难:画一个城市天际线(3-5座不同高度的房子,用循环批量画)

七、下期预告

学会精确定位后,我们可以画更复杂的图案。下节课学习画圆和弧线,让海龟画出圆形、弧形、螺旋线!

下期:《turtle第6课:画圆与弧线》

有问题欢迎留言!

最新文章

随机文章