Python Turtle 小项目 6

本次教大家画一只简易龙猫

代码讲解

首先,导入turtle模块

import turtle as t

初始化画笔属性

t.pensize(2)
t.speed(0)

绘制龙猫的身体(用circle函数绘制弧线)

t.pu()
t.goto(-150,0)
t.pd()
t.left(90)
t.circle(-300,30)

t.seth(180)
t.circle(-40,270)

t.seth(20)
t.circle(-150,40)

t.seth(90)
t.circle(-40,270)

t.seth(-60)
t.circle(-300,30)

t.seth(-80)
t.circle(-(t.pos()[0]--150+4)/2,200)

绘制龙猫的两只眼睛,坐标手动计算

t.pu()
t.goto(-84,50)
t.dot(20)
t.goto(47.5,50)
t.dot(20)

绘制龙猫的鼻子,坐标手动计算

t.goto(-18.25,25)
t.dot(30)
t.goto(-18.25,10)
t.pd()
t.seth(-90)
t.circle(-20,90)
t.pu()
t.goto(-18.25,10)
t.seth(-90)
t.pd()
t.circle(20,90)

绘制龙猫的右手(龙猫的视觉)

t.pu()
h=0
pos=-74,-25
for i in range(3):
    t.pu()
    t.goto(pos[0],pos[1])
    t.seth(h)
    t.pd()
    t.circle(-35,40)
    h=t.heading()
    pos=t.pos()
    t.seth(-135)
    t.fd(20)
t.seth(-135)
t.fd(20)

绘制龙猫的左手(龙猫的视觉)

t.pu()
h=180
pos=47.5,-25
for i in range(3):
    t.pu()
    t.goto(pos[0],pos[1])
    t.seth(h)
    t.pd()
    t.circle(35,40)
    h=t.heading()
    pos=t.pos()
    t.seth(-45)
    t.fd(20)
t.seth(-45)
t.fd(20)

隐藏画笔并保持显示

t.hideturtle()
t.done()

最终代码

import turtle as t

t.pensize(2)
t.speed(0)

t.pu()
t.goto(-150,0)
t.pd()
t.left(90)
t.circle(-300,30)

t.seth(180)
t.circle(-40,270)

t.seth(20)
t.circle(-150,40)

t.seth(90)
t.circle(-40,270)

t.seth(-60)
t.circle(-300,30)

t.seth(-80)
t.circle(-(t.pos()[0]--150+4)/2,200)

t.pu()
t.goto(-84,50)
t.dot(20)
t.goto(47.5,50)
t.dot(20)

t.goto(-18.25,25)
t.dot(30)
t.goto(-18.25,10)
t.pd()
t.seth(-90)
t.circle(-20,90)
t.pu()
t.goto(-18.25,10)
t.seth(-90)
t.pd()
t.circle(20,90)

t.pu()
h=0
pos=-74,-25
for i in range(3):
    t.pu()
    t.goto(pos[0],pos[1])
    t.seth(h)
    t.pd()
    t.circle(-35,40)
    h=t.heading()
    pos=t.pos()
    t.seth(-135)
    t.fd(20)
t.seth(-135)
t.fd(20)

t.pu()
h=180
pos=47.5,-25
for i in range(3):
    t.pu()
    t.goto(pos[0],pos[1])
    t.seth(h)
    t.pd()
    t.circle(35,40)
    h=t.heading()
    pos=t.pos()
    t.seth(-45)
    t.fd(20)
t.seth(-45)
t.fd(20)

t.hideturtle()
t.done()

喜欢的话记得点赞关注哦!

关注我,订阅免费的Turtle画图专栏,查看更多的turtle绘图教学吧!(持续更新ing…)


版权声明:本文为leleprogrammer原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。