turtle作图:用turtle画一个小猪佩奇(详解!)

时间:2022-06-16
本文章向大家介绍turtle作图:用turtle画一个小猪佩奇(详解!),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

之前的一篇文章大致说了一下turtle这个模块的基本操作,不知道的朋友可以去看看,真的超级简单:python:turtle作图基础。

准备用turtle来画一个网红猪--小猪佩奇。

在这之前,我们先聊一聊circle()这个方法和色彩的填充。

circle()

circle的基本用法是:turtle.circle(radius, extent=None, steps=None)

它有三个参数,第一个是半径,第二个extent是弧度,steps表示的是圆的n阶多边形。

在这里需要注意的一点是,比如,turtle.circle(10),它画圆的方式是:以“小海龟”的方向为初始方向,然后逆时针画出一个圆形;如果你这样写turtle.circle(10,-30),它将会以海龟的方向的反方向画出一个弧度为30的弧,也就是逆时针的。

所以,如果你要控制圆的方向,需要注意“海龟”的方向和弧度的方向,以及画圆时,是以顺时针的方向画圆的,这三点。

举个例子:

import turtle as t t.circle(-10) t.circle(10) t.mainloop() 它的结果是

它是先画顺时针的圆,然后画逆时针的圆。

再如:

import turtle as t t.seth(180)#海龟初始的方向为西 t.circle(-10) t.circle(10) t.mainloop() 结果为:

它会先画上圆,然后再画下圆。因为带负号,所以为顺时针。

那么,如果我们想让它变为顺时针画下圆,应该怎么做呢?代码如下:

import  turtle  as  t
t.seth(180)#海龟初始的方向为西
t.circle(-10)
t.circle(10,-360)
t.mainloop()

,这样的话,就会在画下圆的时候,用顺时针的方向来画。

色彩的填充

在使用turtle.begin_fill()和turtle.end_fill()进行色彩填充的时候,需要注意turtle会自动确定封闭图形对一个封闭图形进行填充

完成上面这一部分,以及之前的那篇文章的内容,就可以开始画小猪佩奇了。 绘制小猪佩奇

import  turtle  as  t
t.pensize(4)
t.hideturtle()
t.colormode(255)#设置画笔大小为0-255
t.color((255,155,192),"pink")
t.setheading(-30)
t.pu()
t.goto(-100,100)
t.begin_fill()
t.pd()
a=0.4
for  i  in  range(120):
    if  0<=i<30  or  60<=i<90:
        a=a+0.08
        t.lt(3)
        t.fd(a)
    else:
        a=a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
t.pu()
t.seth(90)
t.fd(25)
t.setheading(0)
t.fd(10)
t.begin_fill()
t.pd()
t.circle(5)
t.color(160,82,45)
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255,155,192)
t.begin_fill()
t.circle(5)
t.color(160,82,45)
t.end_fill()
#头
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.pd()
t.begin_fill()
t.seth(0)
t.circle(-300,30)
t.circle(-100,60)
t.circle(-80,100)
t.circle(-150,20)
t.circle(-60,95)
t.seth(161)
t.circle(-300,15)
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
a=0.4
for  i  in  range(60):
    if  0<=i<30:
        a=a+0.08
        t.lt(3)
        t.fd(a)
    else:
        a=a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
#耳机
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,56)
t.end_fill()
#眼睛
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
#腮
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
#嘴
t.color(239,69,19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30,40)
t.circle(40,80)
#身体
t.color("red",(255,99,71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100,10)
t.circle(300,30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300,30)
t.circle(100,3)
t.color((255,155,192),(255,100,100))
t.seth(-135)
t.circle(-80,63)
t.circle(-150,24)
t.end_fill()
#手
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300,15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20,90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300,15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20,90)
#脚
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
#尾巴
t.pensize(4)
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70,20)
t.circle(10,330)
t.circle(70,30)
t.mainloop()

最终结果:

其实这个挺简单的,把之前基本的语法知识了解一下就能掌握了。

全文结束,欢迎在评论区讨论~