用 Python 画小猪佩奇

在微信上看到一个未完成的小猪佩奇,使用 python 内置的 turtle 绘制,代码是我整理完善的,直接献上。

# coding:utf-8
import turtle as tur

def nose(x,y):#鼻子
   tur.penup()#提起笔
   tur.goto(x,y)#定位
   tur.pendown()#落笔,开始画
   tur.setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)
   tur.begin_fill()#准备开始填充图形
   a=0.4
   for i in range(120):
       if 0<=i<30 or 60<=i<90:
           a=a+0.08
           tur.left(3) #向左转3度
           tur.forward(a) #向前走a的步长
       else:
           a=a-0.08
           tur.left(3)
           tur.forward(a)
   tur.end_fill()#填充完成

   tur.penup()
   tur.setheading(90)
   tur.forward(25)
   tur.setheading(0)
   tur.forward(10)
   tur.pendown()
   tur.pencolor(255,155,192)#画笔颜色
   tur.setheading(10)
   tur.begin_fill()
   tur.circle(5)
   tur.color(160,82,45)#返回或设置pencolor和fillcolor
   tur.end_fill()

   tur.penup()
   tur.setheading(0)
   tur.forward(20)
   tur.pendown()
   tur.pencolor(255,155,192)
   tur.setheading(10)
   tur.begin_fill()
   tur.circle(5)
   tur.color(160,82,45)
   tur.end_fill()

def head(x,y):#头
   tur.color((255,155,192),"pink")
   tur.penup()
   tur.goto(x,y)
   tur.setheading(0)
   tur.pendown()
   tur.begin_fill()
   tur.setheading(180)
   tur.circle(300,-30)
   tur.circle(100,-60)
   tur.circle(80,-100)
   tur.circle(150,-20)
   tur.circle(60,-95)
   tur.setheading(161)
   tur.circle(-300,15)
   tur.penup()
   tur.goto(-100,100)
   tur.pendown()
   tur.setheading(-30)
   a=0.4
   for i in range(60):
       if 0<=i<30 or 60<=i<90:
           a=a+0.08
           tur.lt(3) #向左转3度
           tur.forward(a) #向前走a的步长
       else:
           a=a-0.08
           tur.lt(3)
           tur.forward(a)
   tur.end_fill()

def ears(x,y): #耳朵
   tur.color((255,155,192),"pink")
   tur.penup()
   tur.goto(x,y)
   tur.pendown()
   tur.begin_fill()
   tur.setheading(100)
   tur.circle(-50,50)
   tur.circle(-10,120)
   tur.circle(-50,54)
   tur.end_fill()

   tur.penup()
   tur.setheading(90)
   tur.forward(-12)
   tur.setheading(0)
   tur.forward(30)
   tur.pendown()
   tur.begin_fill()
   tur.setheading(100)
   tur.circle(-50,50)
   tur.circle(-10,120)
   tur.circle(-50,56)
   tur.end_fill()

def eyes(x,y):#眼睛
   tur.color((255,155,192),"white")
   tur.penup()
   tur.setheading(90)
   tur.forward(-20)
   tur.setheading(0)
   tur.forward(-95)
   tur.pendown()
   tur.begin_fill()
   tur.circle(15)
   tur.end_fill()

   tur.color("black")
   tur.penup()
   tur.setheading(90)
   tur.forward(12)
   tur.setheading(0)
   tur.forward(-3)
   tur.pendown()
   tur.begin_fill()
   tur.circle(3)
   tur.end_fill()

   tur.color((255,155,192),"white")
   tur.penup()
   tur.seth(90)
   tur.forward(-25)
   tur.seth(0)
   tur.forward(40)
   tur.pendown()
   tur.begin_fill()
   tur.circle(15)
   tur.end_fill()

   tur.color("black")
   tur.penup()
   tur.setheading(90)
   tur.forward(12)
   tur.setheading(0)
   tur.forward(-3)
   tur.pendown()
   tur.begin_fill()
   tur.circle(3)
   tur.end_fill()

def cheek(x,y):#腮
   tur.color((255,155,192))
   tur.penup()
   tur.goto(x,y)
   tur.pendown()
   tur.setheading(0)
   tur.begin_fill()
   tur.circle(30)
   tur.end_fill()

def mouth(x,y): #嘴
   tur.color(239,69,19)
   tur.penup()
   tur.goto(x,y)
   tur.pendown()
   tur.setheading(-80)
   tur.circle(30,40)
   tur.circle(40,80)

def body(x,y): #身子
   tur. color("red", (255, 99, 71))
   tur. penup()
   tur. goto(x,y)
   tur. pendown()
   tur. begin_fill()
   tur. seth(-130)
   tur. circle(100, 10)
   tur. circle(300, 30)
   tur. seth(0)
   tur. forward(230)
   tur. seth(90)
   tur. circle(300, 30)
   tur. circle(100, 3)
   tur. color((255, 155, 192), (255, 100, 100))
   tur. seth(-135)
   tur. circle(-80, 63)
   tur. circle(-150, 24)
   tur. end_fill()

def hands(x,y): #手
   tur. color((255, 155, 192))
   tur. penup()
   tur. seth(90)
   tur. forward(-40)
   tur. seth(0)
   tur. forward(-27)
   tur. pendown()
   tur. seth(-160)
   tur. circle(300, 15)
   tur. penup()
   tur. seth(90)
   tur. forward(15)
   tur. seth(0)
   tur. forward(0)
   tur. pendown()
   tur. seth(-10)
   tur. circle(-20, 90)
   tur. penup()
   tur. seth(90)
   tur. forward(30)
   tur. seth(0)
   tur. forward(237)
   tur. pendown()
   tur. seth(-20)
   tur. circle(-300, 15)
   tur. penup()
   tur. seth(90)
   tur. forward(20)
   tur. seth(0)
   tur. forward(0)
   tur. pendown()
   tur. seth(-170)
   tur. circle(20, 90)

def feet(x,y): #脚
   tur. pensize(10)
   tur. color((240, 128, 128))
   tur. penup()
   tur. seth(90)
   tur. forward(-75)
   tur. seth(0)
   tur. forward(-180)
   tur. pendown()
   tur. seth(-90)
   tur. forward(40)
   tur. seth(-180)
   tur. color("black")
   tur. pensize(15)
   tur. forward(20)
   tur. pensize(10)
   tur. color((240, 128, 128))
   tur. penup()
   tur. seth(90)
   tur. forward(40)
   tur. seth(0)
   tur. forward(90)
   tur. pendown()
   tur. seth(-90)
   tur. forward(40)
   tur. seth(-180)
   tur. color("black")
   tur. pensize(15)
   tur. forward(20)

def tail(x,y): #尾巴
   tur. pensize(4)
   tur. color((255, 155, 192))
   tur. penup()
   tur. seth(90)
   tur. forward(70)
   tur. seth(0)
   tur. forward(95)
   tur. pendown()
   tur. seth(0)
   tur. circle(70, 20)
   tur. circle(10, 330)
   tur. circle(70, 30)   

def setting():          #参数设置
   tur.pensize(4)
   tur.hideturtle()        #使乌龟无形(隐藏)
   tur.colormode(255)      #将其设置为1.0或255.随后 颜色三元组的r,g,b值必须在0 .. cmode范围内
   tur.color((255,155,192),"pink")
   tur.setup(840,500)
   tur.speed(10)

def main():
   setting()           #画布、画笔设置
   nose(-100,100)      #鼻子
   head(-69,167)       #头
   ears(0,160)         #耳朵
   eyes(0,0)         #眼睛
   cheek(80,10)        #腮
   mouth(-20,30)       #嘴
   body(-32,-8)
   hands(0,0)
   feet(0,0)
   tail(0,0)
   tur.done()

if __name__ == '__main__':
 main()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,861评论 25 708
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,003评论 3 119
  • 91. 如果一个类中有一个方法是抽象方法,而这个类没有定义成抽象类,会报错吗? 答:会,"Fatal error:...
    Chting阅读 209评论 0 2
  • 回顾人生的23年,一直波澜不惊的活着,普通的不能再平凡,你所想过的出格和精彩我都没有经历过,所以生活真的很无聊,无...
    来都来了看剧阅读 220评论 0 0
  • 想起来昨天去德邦物流面试,因为岗位正好在我家旁边,所以认为这个职位理所当然是我的了。所以简历写的简简单单被面试的小...
    小小番茄君阅读 163评论 0 0