
python做简单的笑脸的编程,python绘制一个笑脸
turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形python简单代码。官方手册-turtle总体来说大概分为几个步骤分解图形:看绘制图案由几部分组成。图形再次分解:一个部分又由几个简单图形组成。准备图形函数:对于特别图形需要做几何
大家好,本文将围绕python画笑脸步骤的中文翻译展开说明,python做简单的笑脸的编程是一个很多人都想弄明白的事情,想搞清楚python绘制一个笑脸需要先了解以下几个事情。
起因
最近在学Python,了解到turtle的绘图库,刚好实践一下,有错误欢迎大家指出。
简单介绍turtle
turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形python简单代码。
对应方向
注意海龟初始方向为水平向右。
海龟默认画面的中心位置为(0,0)。
下图分别是逆时针对应角度和顺时针对应的角度。
基础指令
#设置笔的颜色
turtle.pencolor(颜色)
#设置画笔颜色为,例如 "red"、"yellow" 或 "#33cc8c"。
turtle.pencolor(r, g, b)
#设置画笔颜色为以 r, g, b 表示的 RGB 颜色。r, g, b 的取值范围应为 0..colormode(1-255)
#设置线宽
turtle.width(线宽)
#设置速度
turtle.speed(速度值)
#速度值是1-10逐渐变大;"fastest": 0 最快;"fast": 10 快;"normal": 6 正常;"slow": 3 慢;"slowest": 1 最慢
#设置海龟样式
turtle.hideturtle()/turtle.ht()
#隐藏海龟图标
turtle.showturtle()/turtle.st()
#向前走
turtle.forward(距离)
turtle.fd(距离)
#向后走
turtle.back(距离)
turtle.bk(距离)
#移动到指定位置
turtle.goto(x坐标, y坐标)
turtle.setx(x坐标)
turtle.sety(y坐标)
#回到开始位置
turtle.home()
#可以重置方向
#向左转
turtle.left(角度)
#向右转
turtle.right(角度)
#抬起笔
turtle.up()
#放下笔
turtle.down()
#画一个完整的圆
turtle.circle(半径)
#画圆环的一部分
turtle.circle(半径, 角度)
#默认逆时针
#画点
turtle.dot(直径)
# 设置填充颜色
turtle.fillcolor(颜色)
# 开始填充
turtle.begin_fill()
# 结束填充
turtle.end_fill()
开始绘制
先画第一个笑脸,具体代码如下
import turtle
#设置速度
turtle.speed(2)
#隐藏海龟
turtle.hideturtle()
#黄脸
turtle.up()
turtle.down()
#设置笔的粗细
turtle.pensize(5)
#笔的颜色
turtle.pencolor('black')
#填充的颜色
turtle.fillcolor('yellow')
turtle.begin_fill()
#画圆
turtle.circle(140,360)
turtle.end_fill()
#右眼睛
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
#抬起笔后移动,防止移动路径画上
turtle.goto(50,170)
#到位后开始下笔
turtle.down()
turtle.begin_fill()
turtle.circle(20,360)
turtle.end_fill()
#右眼睛
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.goto(-50,170)
turtle.down()
turtle.begin_fill()
turtle.circle(20,360)
turtle.end_fill()
#嘴巴
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.goto(-60,80)
#设置方向,垂直向下
turtle.setheading(-90)
turtle.down()
#画一个半圆
turtle.circle(60,180)
#画万之后需要手动关闭屏幕
turtle.mainloop()
成果展示:
椭圆绘制
仔细观察,发现表情包的眼睛是椭圆的,那就必须严谨一点画椭圆。
首先需要理解椭圆的结构。
如图蓝色大圆的半径为a,绿色小圆的半径为b。他们中的椭圆是红色椭圆。
以橙点为例可知,
橙点坐标=
x 1 = c o s α ∗ a x1=cosα*a x1=cosα∗a
y 1 = s i n α ∗ b y1=sinα*b y1=sinα∗b
由此可以得出,只要在一个周期2pi的范围内,分割成n份,每一份代表的弧度进行坐标计算,最后把点连成线就是一个椭圆。
当然,我们椭圆的原点位置是不固定的,也需要在坐标的X轴和Y轴各加一个原点位置值。
import turtle, math
p = turtle.Turtle()
def ellipse(a, b, x, y, n=500):
"""
绘制椭圆函数
:param a: 长半轴长度
:param b: 短半轴长度
:param n: 边的数目 -- n越大,越趋近于椭圆
:return:
"""
p.penup()
for i in range(n+1): # 画扁的n边形。当n --> 无穷大,所画出的图形即为椭圆
radian = 2 * math.pi / n # 将2pi弧度分成n份,每份为radian
theta = (i+1)*radian # 每次弧度增加radian
next_point = (a*math.cos(theta)+x, b*math.sin(theta)+y)
p.setpos(next_point)
p.pendown()
ellipse(200, 100, 0, 0)
p.hideturtle()
turtle.mainloop()
详情引用该博主文章,表情包并不需要各个方向的椭圆,只需要竖着的就不再深入研究。
绘制眼睛
重新用椭圆绘制。
#注意此处多调用了math这个数学计算的库
import turtle, math
def ellipse(a, b, x, y, n=500):
turtle.penup()
for i in range(n+1): # 画扁的n边形。当n --> 无穷大,所画出的图形即为椭圆
radian = 2 * math.pi / n # 将2pi弧度分成n份,每份为radian
theta = (i+1)*radian # 每次弧度增加radian
next_point = (a*math.cos(theta)+x, b*math.sin(theta)+y)
turtle.setpos(next_point)
turtle.pendown()
#设置速度
turtle.speed(2)
#隐藏海龟
turtle.hideturtle()
#黄脸
turtle.up()
turtle.down()
#设置笔的粗细
turtle.pensize(5)
#笔的颜色
turtle.pencolor('black')
#填充的颜色
turtle.fillcolor('yellow')
turtle.begin_fill()
#画圆
turtle.circle(140,360)
turtle.end_fill()
#右眼睛
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.begin_fill()
ellipse(10, 30, 50, 170, n=500)
turtle.end_fill()
#左眼睛
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.begin_fill()
ellipse(10, 30, -50, 170, n=500)
turtle.end_fill()
#嘴巴
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.goto(-60,80)
#设置方向,垂直向下
turtle.setheading(-90)
turtle.down()
#画一个半圆
turtle.circle(60,180)
#画万之后需要手动关闭屏幕
turtle.mainloop()
成果展示:
惊讶表情包
#注意此处多调用了math这个数学计算的库
import turtle, math
def ellipse(a, b, x, y, n=500):
turtle.penup()
for i in range(n+1): # 画扁的n边形。当n --> 无穷大,所画出的图形即为椭圆
radian = 2 * math.pi / n # 将2pi弧度分成n份,每份为radian
theta = (i+1)*radian # 每次弧度增加radian
next_point = (a*math.cos(theta)+x, b*math.sin(theta)+y)
turtle.setpos(next_point)
turtle.pendown()
#设置速度
turtle.speed(2)
#隐藏海龟
turtle.hideturtle()
#黄脸
turtle.up()
turtle.down()
#设置笔的粗细
turtle.pensize(5)
#笔的颜色
turtle.pencolor('black')
#填充的颜色
turtle.fillcolor('yellow')
turtle.begin_fill()
#画圆
turtle.circle(140,360)
turtle.end_fill()
#右眼睛
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.begin_fill()
ellipse(10, 30, 40, 170, n=500)
turtle.end_fill()
#左眼睛
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.begin_fill()
ellipse(10, 30, -40, 170, n=500)
turtle.end_fill()
#嘴巴
turtle.up()
turtle.pensize(5)
turtle.goto(-30,80)
#设置方向,垂直向下
turtle.setheading(-90)
turtle.down()
#画一个半圆
turtle.pencolor('black')
turtle.fillcolor('#163856')
turtle.begin_fill()
turtle.circle(30,360)
turtle.end_fill()
#画万之后需要手动关闭屏幕
turtle.mainloop()
成果展示
如果不知道颜色的三原色,可以打开word,选择颜色中的其余颜色,复制16进制的数值即可。
脸红微笑表情包
注意这个表情包的眼睛其实是两个半椭圆形成的。椭圆的函数周期改成一个pi就是半圈。
#注意此处多调用了math这个数学计算的库
import turtle, math
def ellipse(a, b, x, y, n=500):
turtle.penup()
for i in range(n+1): # 画扁的n边形。当n --> 无穷大,所画出的图形即为椭圆
radian = math.pi / n # 将pi弧度分成n份,每份为radian
theta = (i+1)*radian # 每次弧度增加radian
next_point = (a*math.cos(theta)+x, b*math.sin(theta)+y)
turtle.setpos(next_point)
turtle.pendown()
#设置速度
turtle.speed(2)
#隐藏海龟
turtle.hideturtle()
#黄脸
turtle.up()
turtle.down()
#设置笔的粗细
turtle.pensize(5)
#笔的颜色
turtle.pencolor('black')
#填充的颜色
turtle.fillcolor('yellow')
turtle.begin_fill()
#画圆
turtle.circle(140,360)
turtle.end_fill()
#嘴巴
turtle.up()
turtle.pensize(5)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.goto(-60,90)
#设置方向,垂直向下
turtle.setheading(-90)
turtle.down()
#画一个半圆
turtle.circle(60,180)
#左酒窝
turtle.up()
turtle.pensize(5)
turtle.home()
turtle.goto(-70,80)
turtle.down()
#画一个圆
turtle.pencolor('black')
turtle.fillcolor('red')
turtle.begin_fill()
turtle.circle(20,360)
turtle.end_fill()
#右酒窝
turtle.up()
turtle.pensize(5)
turtle.home()
turtle.goto(70,80)
turtle.down()
#画一个圆
turtle.pencolor('black')
turtle.fillcolor('red')
turtle.begin_fill()
turtle.circle(20,360)
turtle.end_fill()
#右眼睛
turtle.up()
turtle.pensize(2)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.goto(40,160)
turtle.begin_fill()
ellipse(20, 50, 40, 160, n=500)
turtle.end_fill()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
turtle.begin_fill()
ellipse(20, 20, 40, 160, n=500)
turtle.end_fill()
#左眼睛
turtle.up()
turtle.pensize(2)
turtle.pencolor('black')
turtle.fillcolor('black')
turtle.goto(-40,160)
turtle.begin_fill()
ellipse(20, 50, -40, 160, n=500)
turtle.end_fill()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
turtle.begin_fill()
ellipse(20, 20, -40, 160, n=500)
turtle.end_fill()
#画万之后需要手动关闭屏幕
turtle.mainloop()
成果有点诡异…
最终总结
总体来说大概分为几个步骤
- 分解图形:看绘制图案由几部分组成。
- 图形再次分解:一个部分又由几个简单图形组成。
- 准备图形函数:对于特别图形需要做几何分析关系式,做成函数方便调用。
- 开始绘制图形。
- 调整位置。
更多推荐
所有评论(0)