赞
踩
本文采用Python中的绘图turtle程序包 绘制动漫人物
使用pip进行安装的时候会返回错误:
在网上查阅资料各种说法都有
有的是升级两个包,有的是安装Visual C++ 14.0
在这里我采用的是在公众号上找到的一种方法(亲测很好用~~)
1,从网上下载 turtle 程序包,下载链接为:
https://files.pythonhosted.org/packages/ff/f0/21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f/turtle-0.0.2.tar.gz
2,解压 1 中的压缩包,修改其中的 setup.py 脚本:修改方法在40行处的 ValueError,ve
except ValueError, ve:
改为
except (ValueError, ve):
3, 使用 pip 命令对本地下载文件进行安装
pip install D:\turtle-0.0.2
**
from turtle import* def nose(x,y):#鼻子 pu() goto(x,y) pd() seth(-30) begin_fill() a=0.4 for i in range(120): if 0<=i<30 or 60<=i<90: a=a+0.08 lt(3) #向左转3度 fd(a) #向前走a的步长 else: a=a-0.08 lt(3) fd(a) end_fill() pu() seth(90) fd(25) seth(0) fd(10) pd() pencolor(255,155,192) seth(10) begin_fill() circle(5) color(160,82,45) end_fill() pu() seth(0) fd(20) pd() pencolor(255,155,192) seth(10) begin_fill() circle(5) color(160,82,45) end_fill() def head(x,y):#头 color((255,155,192),"pink") pu() goto(x,y) seth(0) pd() begin_fill() seth(180) circle(300,-30) circle(100,-60) circle(80,-100) circle(150,-20) circle(60,-95) seth(161) circle(-300,15) pu() goto(-100,100) pd() seth(-30) a=0.4 for i in range(60): if 0<=i<30 or 60<=i<90: a=a+0.08 lt(3) #向左转3度 fd(a) #向前走a的步长 else: a=a-0.08 lt(3) fd(a) end_fill() def ears(x,y): #耳朵 color((255,155,192),"pink") pu() goto(x,y) pd() begin_fill() seth(100) circle(-50,50) circle(-10,120) circle(-50,54) end_fill() pu() seth(90) fd(-12) seth(0) fd(30) pd() begin_fill() seth(100) circle(-50,50) circle(-10,120) circle(-50,56) end_fill() def eyes(x,y):#眼睛 color((255,155,192),"white") pu() seth(90) fd(-20) seth(0) fd(-95) pd() begin_fill() circle(15) end_fill() color("black") pu() seth(90) fd(12) seth(0) fd(-3) pd() begin_fill() circle(3) end_fill() color((255,155,192),"white") pu() seth(90) fd(-25) seth(0) fd(40) pd() begin_fill() circle(15) end_fill() color("black") pu() seth(90) fd(12) seth(0) fd(-3) pd() begin_fill() circle(3) end_fill() def cheek(x,y):#腮 color((255,155,192)) pu() goto(x,y) pd() seth(0) begin_fill() circle(30) end_fill() def mouth(x,y): #嘴 color(239,69,19) pu() goto(x,y) pd() seth(-80) circle(30,40) circle(40,80) def body(x,y):#身体 color("red",(255,99,71)) pu() goto(x,y) pd() begin_fill() seth(-130) circle(100,10) circle(300,30) seth(0) fd(230) seth(90) circle(300,30) circle(100,3) color((255,155,192),(255,100,100)) seth(-135) circle(-80,63) circle(-150,24) end_fill() def hands(x,y):#手 color((255,155,192)) pu() goto(x,y) pd() seth(-160) circle(300,15) pu() seth(90) fd(15) seth(0) fd(0) pd() seth(-10) circle(-20,90) pu() seth(90) fd(30) seth(0) fd(237) pd() seth(-20) circle(-300,15) pu() seth(90) fd(20) seth(0) fd(0) pd() seth(-170) circle(20,90) def foot(x,y):#脚 pensize(10) color((240,128,128)) pu() goto(x,y) pd() seth(-90) fd(40) seth(-180) color("black") pensize(15) fd(20) pensize(10) color((240,128,128)) pu() seth(90) fd(40) seth(0) fd(90) pd() seth(-90) fd(40) seth(-180) color("black") pensize(15) fd(20) def tail(x,y):#尾巴 pensize(4) color((255,155,192)) pu() goto(x,y) pd() seth(0) circle(70,20) circle(10,330) circle(70,30) def setting(): #参数设置 pensize(4) hideturtle() colormode(255) color((255,155,192),"pink") setup(840,500) 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) #尾巴 done() #结束 main()
三、**
简介:
turtle名称含义为“海龟”,我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形。
海龟的运动是由程序控制的,它可以变换颜色,改变大小(宽度)等。
**
Func(函数) | Para type(参数类型) | Desc(描述) |
---|---|---|
turtle.screensize(a,b,bg) | int;int;colorstring /tuple | 创建一个 a*b 大小的画布,bg为背景颜色,为 r,g,b 组成的元组; |
turtle.title(name) | string | 画布窗口名字设为 name |
turtle.mainloop() | -(表示无参数) | 启动事件循环,该语句使用时必须放在最后一句 |
turtle.Turtle() | - | RawTurtle()的子类,是画笔的对象,l来设置画笔相关属性 |
turtle.Turtle().tracer(bool) | bool | bool = False ,表示绘制开始前调用;bool=True 表示绘制开始后调用 |
turtle.Turtle().pensize(size) | int | 把画笔大小设置为 size ; |
turtle.Turtle().seth(to_angle) /turtle.Turtle().sething(to_angle) | float/int | turtle 方向角度设为 to_angle |
turtle.Turtle().setx(x) | int/float | 设置 turtle 的横坐标 x, y保持不变 |
turtle.Turtle().sety(y) | int/float | 设置 turtle 的纵坐标 y, x保持不变 |
turtle.Turtle().forward(distance)/turtle.Turtle().fd(distance) | int/float | 画笔按原来方向向前移动 distance 个单位 |
turtle.Turtle().backward(distance)/turtle.Turtle().back(distance)/turtle.Turtle().bk(distance) | int/float | 画笔按原来方向向后移动 distance 个单位 |
turtle.Turtle().lt(distance),turtle.Turtle().left(distance) | int/float | 画笔按原来方向向左移动 distance 个单位 |
turtle.Turtle().rt(distance)/turtle.Turtle().right(distance) | int/float | 画笔按原来方向向右移动 distance 个单位 |
turtle.Turtle().goto(x,y) | float/int,float/int | 画笔跳到指定坐标位置(x,y) |
turtle.Turtle().circle(radius,extends) | int/float,int/float/None | 绘制一个指定半径(radius)的圆,圆心根据半径、画笔方向,绘制圆角度三者决定;extends 为绘制的角度(小于360 表示绘制部分圆弧),extend为正值绘制方向为逆时针方向,否则顺时针; |
turtle.Turtle().speed(num) | int/float | 设定画笔绘制速度为 num |
turtle.Turtle().up()/turtle.Turtle().penup() | - | 抬起画笔,停止绘制 |
turtle.Turtle().pendown()/turtle.Turtle().down() | - | 放下画笔,开始绘制 |
turtle.Turtle().fillcolor(color) | string/tuple/colorname | 设置画笔填充颜色 color ,输入格式支持四种:颜色代码字符串(’#000000’),rgb元组((r,g,b)),tk指定颜色字符串(“红色”) |
turtle.Turtle().begin_fill() | - | 调用后开始对绘制的形状填充颜色 |
turtle.Turtle().end_fill() | - | 调用后停止填充颜色 |
注 : func 中换行表示或的意思,每种函数名有它的缩写,所以存在函数 有2-3 种 不同写法,但函数功能是一样的, - 表示无参数;
官方文档:https://docs.python.org/3.7/library/turtle.html?highlight=screensize#turtle.seth
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。