用 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()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,200评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,526评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,321评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,601评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,446评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,345评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,753评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,405评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,712评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,743评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,529评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,369评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,770评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,026评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,301评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,732评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,927评论 2 336

推荐阅读更多精彩内容

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