python canvas画弧度_python|小猪佩奇身上纹,掌声送给社会人!

这两年,在中国人气最高的女生是哪位?

大家不用再猜其他女明星了,答案只有一个,社会我一姐,小猪佩奇

a4b3a0507d73e732f38ace5b722421e2.png

既然提到这只猪,自然就要说她的同名动画。2015年,国内引进了英国动画《小猪佩奇》,从此这只猪就开始在国内疯狂地圈粉。

先是孩子,然后是家长,最后一传十、十传百,最后成为了全网红人,不对,是红猪。

嘿嘿嘿嘿嘿嘿嘿嘿,但我们今天的目的不是让你去看这个动画,我们是要把它画出来。

ee7335bfe8e89608793836f4e4e93c34.png

小猪佩奇在构图基本是各种曲线,类抛物线、类圆、类椭圆、类二次贝塞尔曲线。

因为画图画曲线不是 Python 擅长的事情,所以用纯粹的 Python 来做,会更有挑战,也更有趣。

63dae76beef0b267e813beee43c95057.png

在前端技术选型上,画图首先想到的是svg、canvas,但它们本身就擅长画图,这就没意思了,我想佩奇也不会答应的。于是用纯粹的Python来做,这样更有挑战,因为画图画曲线不是 Python 擅长的事情。

基本思路:选好画板大小、设置好画笔颜色、粗细、定位好位置、依次画鼻子、头、耳朵、眼睛、腮、嘴、身体、手脚、尾巴、完事。

在看代码之前,我们还是先看一下实现效果:

turtle 是 Python 内置的一个比较有趣味的模块,俗称海龟作图,它提供一些简单的绘图工具。下面我们就看一下它怎么使用。

☝?一、导入

import turtle

?温馨提示:如果导入时报错,请先安装

pip install turtle

✌?二、方法

本篇文章目前先介绍绘制佩奇的相关方法,如各位客官对其感兴趣,可通过评论告诉我,我将介绍其他内容。

turtle.pensize(width) 设置线条的粗细。参数:width一个正数turtle.hideturtle() 删隐藏乌龟的形状,在做复杂绘图的时候隐藏的时候有利于提高绘图速度。turtle.colormode(n) 查看色彩模式,缺省1.0,即RGB范围在0-1 ,切换值 255 ,即RGB范围在0-255turtle.color(pencolor,fillcolor) 同时设置pencolor和fillcolor,值为RGB颜色元组或颜色名称turtle.setup(width,height,startx,starty) 设置窗体大小和位置,4个参数中后两个可选,前两个参数是指窗口的长度与宽度,后两个参数指窗体的左上角的位置的坐标。turtle.up() 将提起笔,移动时无图。turtle.goto(x,y) 移动到绝对位置,如果笔落下,画线,不改变方向。参数:x-一个数字或一对数字。y-一个数字或None。turtle.down() 放下笔,移动的时候绘图。turtle.setheading(to_angle) 将方向设置为to_angle。就是东西南北方向。具体如下:标准模式:0 - 东、 90 - 北、 180 - 西、 270 - 南turtle.speed(n) 将速度设置为0至10范围内整数。如果没有参数,则返回当前速度。如果大于10或者小于0.5,则速度设置为0 。turtle.begin_fill() 在绘制要填充的形状前调用。当然在调用完还需要end_fill()。turtle.end_fill() 填充完成turtle.left(n) 以角度单位向左转动。参数:一个数字。单位默认为度数turtle.forward(n) 向前移动指定的距离。参数:一个数字。turtle.pencolor(R,G,B) 设置笔的颜色。值为0至255之间的整数turtle.circle(radius) 绘制一个给定半径的圆。参数:radius-一个数字(半径,如果值为正则逆时针,负数为顺时针)turtle.done() 画完之后不关闭窗口

??三、完整代码

# @Datetime : 2020/6/12 上午11:16# @Author   : Meakelra# @File     : peiqi.pyimport turtledef nose(x,y):#鼻子    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.setheading(-30)    turtle.begin_fill()    a=0.4    for i in range(120):        if 0<=i<30 or 60<=i<90:            a=a+0.08            turtle.left(3)             turtle.forward(a)         else:            a=a-0.08            turtle.left(3)            turtle.forward(a)        turtle.end_fill()    turtle.up()    turtle.setheading(90)    turtle.forward(25)    turtle.setheading(0)    turtle.forward(10)    turtle.down()    turtle.pencolor(255,155,192)    turtle.setheading(10)    turtle.begin_fill()    turtle.circle(5)    turtle.fillcolor(160,82,45)    turtle.end_fill()    turtle.up()    turtle.setheading(0)    turtle.forward(20)    turtle.down()    turtle.pencolor(255,155,192)    turtle.setheading(10)    turtle.begin_fill()    turtle.circle(5)    turtle.fillcolor(160,82,45)    turtle.end_fill()def head(x,y):#头    turtle.color((255,155,192),"pink")    turtle.up()    turtle.goto(x,y)    turtle.setheading(0)    turtle.down()    turtle.begin_fill()    turtle.setheading(180)    turtle.circle(300,-30)    turtle.circle(100,-60)    turtle.circle(80,-100)    turtle.circle(150,-20)    turtle.circle(60,-95)    turtle.setheading(161)    turtle.circle(-300,15)    turtle.up()    turtle.goto(-100,100)    turtle.down()    turtle.setheading(-30)    a=0.4    for i in range(60):        if 0<=i<30 or 60<=i<90:            a=a+0.08            turtle.left(3)             turtle.forward(a)         else:            a=a-0.08            turtle.left(3)            turtle.forward(a)    turtle.end_fill()def ears(x,y): #耳朵    turtle.color((255,155,192),"pink")    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.begin_fill()    turtle.setheading(100)    turtle.circle(-50,50)    turtle.circle(-10,120)    turtle.circle(-50,54)    turtle.end_fill()    turtle.up()    turtle.setheading(90)    turtle.forward(-12)    turtle.setheading(0)    turtle.forward(30)    turtle.down()    turtle.begin_fill()    turtle.setheading(100)    turtle.circle(-50,50)    turtle.circle(-10,120)    turtle.circle(-50,56)    turtle.end_fill()def eyes(x,y):#眼睛    turtle.color((255,155,192),"white")    turtle.up()    turtle.setheading(90)    turtle.forward(-20)    turtle.setheading(0)    turtle.forward(-95)    turtle.down()    turtle.begin_fill()    turtle.circle(15)    turtle.end_fill()    turtle.fillcolor("black")    turtle.up()    turtle.setheading(90)    turtle.forward(12)    turtle.setheading(0)    turtle.forward(-3)    turtle.down()    turtle.begin_fill()    turtle.circle(3)    turtle.end_fill()    turtle.color((255,155,192),"white")    turtle.up()    turtle.setheading(90)    turtle.forward(-25)    turtle.setheading(0)    turtle.forward(40)    turtle.down()    turtle.begin_fill()    turtle.circle(15)    turtle.end_fill()    turtle.fillcolor("black")    turtle.up()    turtle.setheading(90)    turtle.forward(12)    turtle.setheading(0)    turtle.forward(-3)    turtle.down()    turtle.begin_fill()    turtle.circle(3)    turtle.end_fill()def cheek(x,y):#腮    turtle.fillcolor((255,155,192))    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.setheading(0)    turtle.begin_fill()    turtle.circle(30)    turtle.end_fill()def mouth(x,y): #嘴    turtle.fillcolor(239,69,19)    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.setheading(-80)    turtle.circle(30,40)    turtle.circle(40,80)def body(x,y):#身体    turtle.color("red",(255,99,71))    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.begin_fill()    turtle.setheading(-130)    turtle.circle(100,10)    turtle.circle(300,30)    turtle.setheading(0)    turtle.forward(230)    turtle.setheading(90)    turtle.circle(300,30)    turtle.circle(100,3)    turtle.color((255,155,192),(255,100,100))    turtle.setheading(-135)    turtle.circle(-80,63)    turtle.circle(-150,24)    turtle.end_fill()def hands(x,y):#手    turtle.color((255,155,192))    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.setheading(-160)    turtle.circle(300,15)    turtle.up()    turtle.setheading(90)    turtle.forward(15)    turtle.setheading(0)    turtle.forward(0)    turtle.down()    turtle.setheading(-10)    turtle.circle(-20,90)    turtle.up()    turtle.setheading(90)    turtle.forward(30)    turtle.setheading(0)    turtle.forward(237)    turtle.down()    turtle.setheading(-20)    turtle.circle(-300,15)    turtle.up()    turtle.setheading(90)    turtle.forward(20)    turtle.setheading(0)    turtle.forward(0)    turtle.down()    turtle.setheading(-170)    turtle.circle(20,90)def foot(x,y):#脚    turtle.pensize(10)    turtle.color((240,128,128))    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.setheading(-90)    turtle.forward(40)    turtle.setheading(-180)    turtle.color("black")    turtle.pensize(15)    turtle.forward(20)    turtle.pensize(10)    turtle.color((240,128,128))    turtle.up()    turtle.setheading(90)    turtle.forward(40)    turtle.setheading(0)    turtle.forward(90)    turtle.down()    turtle.setheading(-90)    turtle.forward(40)    turtle.setheading(-180)    turtle.color("black")    turtle.pensize(15)    turtle.forward(20)def tail(x,y):#尾巴    turtle.pensize(4)    turtle.color((255,155,192))    turtle.up()    turtle.goto(x,y)    turtle.down()    turtle.setheading(0)    turtle.circle(70,20)    turtle.circle(10,330)    turtle.circle(70,30)def setting():          #参数设置    turtle.pensize(4)    turtle.hideturtle()            turtle.colormode(255)         turtle.color((255,155,192),"pink")    turtle.setup(840,500)    turtle.speed(10)def main():    setting()           #画布、画笔设置    nose(-100,100)      #鼻子    head(-69,167)       #头    ears(0,160)         #耳朵    eyes(0,140)         #眼睛    cheek(80,10)        #腮    mouth(-20,30)       #嘴    body(-32,-8)        #身体    hands(-56,-45)      #手    foot(2,-177)        #脚    tail(148,-155)      #尾巴    turtle.done()if __name__ == '__main__':    main()

相关知识及代码就在文章里面,快来试试吧。???

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值