python turtle绘制一只可爱的小海龟

import turtle


t = turtle   # 给turtle取别名
t.pensize(2)  # 设置画笔粗细
t.hideturtle() # 隐藏 turtle
t.colormode(255) # 切换RGB色彩模式
t.color((0, 0, 0), "Green") # 设置画笔颜色和填充颜色
t.setup(500, 500) # 设置主窗体的大小和位置
t.speed(5)  # 定义绘图的速度

t.penup()  # 抬起笔
t.goto(0, -100)  # 设置海龟绝对坐标
t.pendown() # 落下笔
t.circle(100) # 绘制100像素的圆

t.penup()  # 抬起笔
t.goto(-20, 35)  # 设置海龟绝对坐标
t.pendown()  # 落下笔
t.begin_fill()   # 准备填充形状
t.forward(40)  # 向前移动40像素
t.seth(-60) # 海龟转向-60
t.forward(40) # 向前移动40像素
t.seth(-120)  # 设置窗体的位置和大小
t.forward(40)  # 向前移动40像素
t.seth(-180) # 海龟转向-180
t.forward(40)  # 向前移动40像素
t.seth(120)  # 海龟转向120
t.forward(40)  # 向前移动40像素
t.seth(60)   # 海龟转向60
t.forward(40) # 向前移动40像素
t.end_fill()  # 填充完成

t.seth(120)
t.color((0, 0, 0), (29, 184, 130))

for i in range(6):
    t.begin_fill()
    t.forward(60)
    t.right(90)  # 基于当前角度向右转90度
    t.circle(-100, 60)
    t.right(90)
    t.forward(60)
    t.right(180)
    t.end_fill()

t.penup()
t.goto(-15, 100)
t.seth(90) #  行进方向角度的绝对坐标
t.pendown()
t.forward(15)
t.circle(-15, 180)
t.forward(15)

for i in range(4):
    t.penup()
    t.goto(0, 0)
    if i == 0:
        t.seth(35);
    if i == 1:
        t.seth(-25)
    if i == 2:
        t.seth(-145)
    if i == 3:
        t.seth(-205)
    t.forward(100)
    t.right(5)
    t.pendown()
    t.forward(10)
    t.circle(-10, 180) # 圆心在海龟左侧10像素的圆,180角度
    t.forward(10)

t.penup()
t.goto(10, -100)
t.seth(-90)
t.pendown()
t.forward(10)
t.circle(-30, 60)
t.right(150)
t.circle(30, 60)
t.goto(-10, -100)
t.done() # 画面一直停留

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当使用Python中的turtle库来绘制海底世界时,我们可以通过以下代码开始: ```python import turtle # 创建画布和画笔 screen = turtle.Screen() pen = turtle.Turtle() # 设置画布背景色为蓝色表示海洋 screen.bgcolor("blue") # 绘制海底世界 def draw_sea(): pen.speed(2) # 设置画笔速度为2 pen.penup() # 抬起画笔 pen.goto(-200, -200) # 将画笔移动到起始位置 pen.pendown() # 放下画笔 # 绘制海底沙滩 pen.color("yellow") pen.begin_fill() for _ in range(2): pen.forward(400) pen.left(90) pen.forward(100) pen.left(90) pen.end_fill() # 绘制海底植物 pen.penup() pen.goto(-150, -150) pen.pendown() pen.color("green") pen.pensize(10) pen.setheading(90) # 设置画笔朝向为向上 pen.forward(200) # 绘制海底动物 pen.penup() pen.goto(0, -150) pen.pendown() pen.color("red") pen.begin_fill() pen.circle(50) # 绘制海星的头部 pen.end_fill() pen.penup() pen.goto(-30, -220) pen.pendown() pen.color("red") pen.begin_fill() pen.goto(30, -220) pen.goto(0, -270) # 绘制海星的身体 pen.goto(-30, -220) pen.end_fill() # 结束绘制 pen.hideturtle() # 调用绘制函数 draw_sea() # 点击关闭画布 screen.exitonclick() ``` 上述代码使用turtle库创建了一个海底世界的绘图,并绘制了海底沙滩、海底植物和海星。你可以根据自己的需求修改这些绘制元素,并添加更多的海底生物和景观。运行代码后,会弹出一个窗口展示绘制的海底世界,单击窗口即可关闭。 希望这个示例能帮助你开始绘制属于自己的海底世界!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值