网站首页页面设计,微信直接转wordpress,怎么创建微信公众号免费,专门发广告的app前言 
2020年4月#xff0c;我写了一篇用turtle绘制《小清新风格的树》#xff0c;反响挺好。现在打算使用turtle修改一下绘制方式#xff0c;因为线条的绘制太过考虑因素过多#xff0c;如果使用方块进行堆叠#xff0c;绘制出来的形状可以如马赛克一样#xff0c;既符合…前言 
2020年4月我写了一篇用turtle绘制《小清新风格的树》反响挺好。现在打算使用turtle修改一下绘制方式因为线条的绘制太过考虑因素过多如果使用方块进行堆叠绘制出来的形状可以如马赛克一样既符合IT也较为建议又方便一些低龄段的孩子学习turtle毕竟turtle的文档上说的很清楚turtle是为了提升青少年学习python的乐趣而开发的那我也为这个乐趣舔一份彩吧。 虽然这个工具由于时间关系还没写好只实现了其中一部分也就是核心的线条部分代码也没优化。计划是之后打算写一个图像库直接调用即可绘制不同种类的字母、数字、人以及各类物体。 有始有终既然我正式连续性的写博客是从小清新树这篇文开始那么即将到春节也草草的做个总结吧。 
计划 
我们先看几张马赛克风格的图片   
我们的马赛克风格一般是由一些颜色的小方块组合而成并且使用了阶梯式来呈现出弧度的效果但基本元素是小方块。那么我们第一步就先绘制出一个基本小方块为core吧。 
一、绘制小方块 
我们创建一个函数为point点以后绘制小方块都使用这个函数即可。在turtle中绘制一个正方形的小方块很简单for循环4次left或者right并且进行fd画线即可新建一个文件为core代码如下: 
from turtle import *class Core:def point(self):for i in range(0,4):fd(10)right(90)接下来我们创建一个文件为drawdemo在其中进行测试 
from core import CorepxtoolCore()
pxtool.point()
input()成功绘制  
在此需要注意要统一标注以后绘制一个点都需要从这个方法进行出发。接下来开始给这个方法增加参数这样可以动态的绘制指定大小增加了参数后还需要有一个填充颜色使用turtle的填充方法begin_fill与end_fill并且需要给予颜色值fillcolor再添加一个笔杆颜色设置默认与填充颜色一致。我们可以创建2个方法分别设置填充颜色与笔杆颜色 
from turtle import *class Core:def point(self,plenght10,fcolorblack,pcolor1,psize1):self.fillcolor_set(fcolor)self.pencolor_set(fcolor,pcolor)self.pensize_set(psize)begin_fill()for i in range(0,4):fd(plenght)right(90)end_fill()#填充颜色色值def fillcolor_set(self,fcolorblack):fillcolor(fcolor)#笔杆颜色设置def pencolor_set(self,fcolorblack,pcolor1):if pcolor1:pcolorfcolorpencolor(pcolor)else:pencolor(pcolor)#笔杆尺寸def pensize_set(self,psize1):pensize(psize)这个时候就可以传值随意的设置颜色和大小了 
pxtoolCore()
pxtool.point(30,fcolorred,pcolorblue,psize10)二、绘制线 
我们从已有的点上开始出发直接绘制线段线段绘制的方法很简单也就是平移点到下一个绘制坐标进行点的绘制即可。首先我们查看绘制完一个点后的小乌龟位置  
小乌龟回到了初始点这个时候我们默认往右开始绘制线段那么这个时候就应该是当前绘制向前移动一个单位我们在这里统一不使用fd使用goto进行跳转 
def loc_goto(self,plenght):penup()goto(pos()(plenght,0))pendown()def line(self,lenght1,plenght10,fcolorblack,pcolor1,psize1):for i in range(0,lenght):self.point(plenghtplenght,fcolorfcolor,pcolorpcolor,psizepsize)if i!(lenght-1):self.loc_goto(plenght)以上写了2个方法一个是line绘制线一个是跳转到下一个单位的goto方法但是在line方法中我们只在有需要跳转时跳转不需要跳转时最后一笔时则不跳转方便之后的扩展少改动只做有限的事情绝不画蛇添足。 效果如下  编写好向右绘制线段好我们接下来需要向左绘制线段。向左绘制线段需要使用loc_goto方法减去当前的x坐标点向左移动即可并且在line方法中添加参数判断方向 
def line(self,lenght1,plenght10,directionright,fcolorblack,pcolor1,psize1):for i in range(0,lenght):self.point(plenghtplenght,fcolorfcolor,pcolorpcolor,psizepsize)if i!(lenght-1):if directionright:self.loc_goto(plenght)elif directionleft:self.loc_goto(-plenght)elif i(lenght-1):if directionleft:self.loc_goto(plenght)以上代码为了保证一个标准型所有的绘制方法在最后一个矩形绘制时需要进行移动所以在方向扥估left时向右移动了一个单位的距离。同理接下来向上绘制以及向下绘制代码如下并且修改loc_goto方法loc_goto方法只做移动不复再值拼接再设置一个方法用于控制line绘制的移动 
def loc_goto(self,movepos):penup()goto(pos()movepos)pendown()
#line方法的绘图跳转控制
def line_func_draw_move(self,cout_i,direction,lenght,plenght):if cout_i!(lenght-1):if directionright:self.loc_goto((plenght,0))elif directionleft:self.loc_goto((-plenght,0))elif directionup:self.loc_goto((0,plenght))elif directiondown:self.loc_goto((0,-plenght))elif cout_i(lenght-1):if directionleft:self.loc_goto((plenght,0))if directionup:self.loc_goto((0,-plenght))def line(self,lenght1,plenght10,directionright,fcolorblack,pcolor1,psize1):for i in range(0,lenght):self.point(plenghtplenght,fcolorfcolor,pcolorpcolor,psizepsize)self.line_control_func_draw_move(i,direction,lenght,plenght)三、阶梯绘制 
阶梯在马赛克绘画中是当作弧来使用阶梯有每个阶梯的长以及每个阶梯的高长我们可以使用横线绘制高我们使用横线往上绘制即可完成。 
def step(self,lenght1,height1,blenght1,plenght10,directionright,fcolorblack,pcolor1,psize1):for i in range(0,lenght):self.line(lenghtblenght,plenghtplenght,directiondirection,fcolorfcolor,pcolorpcolor,psizepsize)self.loc_goto((plenght,plenght))self.line(lenghtblenght,plenghtplenght,directionup,fcolorfcolor,pcolorpcolor,psizepsize)if i!(lenght-1):self.loc_goto((plenght,plenght*2))以上代码绘制方法一样首先绘制横线然后跳转到下一个绘制点由于要绘制成马赛克锯齿状所以跳转的位置的高度会提高一个单位self.loc_goto((plenght,plenght*2))。其中lenght是总长度height是每个分段位的高度blenght是每个分段的长度。意思就说lenght表示有多少个分段blenght每个分段位有多少长度height为每个分段的高度。代码调用 
pxtool.step(2,2,2,10,directionright,fcolorred,pcolorblue)结果如下  接着我们绘制起始绘制方向为左边、左下、右下的阶梯这时只需要更改绘制时的跳转坐标以及绘制方向以及写一个控制跳转的方法即可 
#step方法的绘图跳转控制
#lenght总长
#blenghtbit一个位长度
#plenght点长度
#direction1横线绘制方向
#direction2竖线绘制方向
#fcolor填充颜色
#pcolor笔颜色
#psize笔大小
#gotopos如何跳转pos位置
#cout_i循环控制变量i
def step_control_func_draw_move(self,lenght,blenght,plenght,direction1,direction2,fcolor,pcolor,psize,gotopos1,gotopos2,cout_i):self.line(lenghtblenght,plenghtplenght,directiondirection1,fcolorfcolor,pcolorpcolor,psizepsize)self.loc_goto(gotopos1)self.line(lenghtblenght,plenghtplenght,directiondirection2,fcolorfcolor,pcolorpcolor,psizepsize)if cout_i!(lenght-1):self.loc_goto(gotopos2)def step(self,lenght1,height1,blenght1,plenght10,directionright,fcolorblack,pcolor1,psize1):for i in range(0,lenght):if directionright:self.step_control_func_draw_move(lenght,blenght,plenght,direction,up,fcolor,pcolor,psize,(plenght,plenght),(plenght,plenght*2),i)elif directionleft:self.step_control_func_draw_move(lenght,blenght,plenght,left,up,fcolor,pcolor,psize,(-plenght*2,plenght),(-plenght,plenght*2),i)elif directionrightdown:self.step_control_func_draw_move(lenght,blenght,plenght,right,down,fcolor,pcolor,psize,(plenght,-plenght),(plenght,-plenght),i)elif directionleftdown:self.step_control_func_draw_move(lenght,blenght,plenght,left,down,fcolor,pcolor,psize,(-plenght*2,-plenght),(-plenght,-plenght),i)三、对称绘制 
在绘制马赛克时很多绘制内容都是左右对称这时我们可以对我们绘制的线段进行对称控制。我们可以考虑从起始绘制点开始到左右某一点的距离对称。我们暂且定位右边与当前绘制的起始点对称若我们设为右边10个单位与当前起始点对称则是当前起始点位置pos(10*一个单位的值,0)为对称坐标得到对称坐标后计算对称坐标与与起始点的差将对称坐标加上差则等于右侧对称内容的起始绘制点从而进行反向绘制即可对称。 
这时我们可以从point方法入手在point绘制点时就记录每次绘制的坐标方便之后选取目标点作为起始绘制点 
def goto_(self,pos_):penup()goto(pos_)pendown()
def point(self,plenght10,fcolorblack,pcolor1,psize1):poslist[]self.fillcolor_set(fcolor)self.pencolor_set(fcolor,pcolor)self.pensize_set(psize)begin_fill()for i in range(0,4):poslist.append(pos())fd(plenght)right(90)end_fill()return poslist
def line(self,lenght1,plenght10,directionright,fcolorblack,pcolor1,psize1,symmetricalf,symmetrical_directionright):posdict{}symmetrical_pointfif symmetrical!f:if symmetrical_directionright:symmetrical_pointpos()(int(symmetrical)*plenght,0)elif symmetrical_directionleft:symmetrical_pointpos()(-int(symmetrical)*plenght,0)elif symmetrical_directionup:symmetrical_pointpos()(0,int(symmetrical)*plenght)elif symmetrical_directiondown:symmetrical_pointpos()(0,-int(symmetrical)*plenght)for i in range(0,lenght):posdict[pointstr(i)]self.point(plenghtplenght,fcolorfcolor,pcolorpcolor,psizepsize)self.line_control_func_draw_move(i,direction,lenght,plenght)if symmetrical!f:self.goto_(symmetrical_point)posdict[symmetrical_point]self.line(lenghtlenght,plenghtplenght,directiondirection,fcolorfcolor,pcolorpcolor,psizepsize,symmetricalf)self.goto_(posdict[pointstr(lenght-1)][3])return posdict   这时我们所绘制的线段就会产生对称 
pos_listpxtool.line(4,10,directionleft,fcolorred,pcolorblue,symmetrical5,symmetrical_directiondown)symmetrical_direction为对称方向symmetrical为对称单位。  返回的结果为每个点坐标 接下来再把阶梯对称的给写好 
def step(self,lenght1,height1,blenght1,plenght10,directionright,fcolorblack,pcolor1,psize1,symmetricalf,symmetrical_directionright):posdict{}symmetrical_pointfsymmetrical_draw_directionif symmetrical!f:if symmetrical_directionright:symmetrical_pointpos()(int(symmetrical)*plenght,0)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownelif symmetrical_directionleft:symmetrical_pointpos()(-int(symmetrical)*plenght,0)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownelif symmetrical_directionrightdown:symmetrical_pointpos()(0,int(symmetrical)*plenght)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownelif symmetrical_directionleftdown:symmetrical_pointpos()(0,-int(symmetrical)*plenght)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownfor i in range(0,lenght):if directionright:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,direction,up,fcolor,pcolor,psize,(plenght,plenght),(plenght,plenght*2),i)elif directionleft:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,left,up,fcolor,pcolor,psize,(-plenght*2,plenght),(-plenght,plenght*2),i)elif directionrightdown:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,right,down,fcolor,pcolor,psize,(plenght,-plenght),(plenght,-plenght),i)elif directionleftdown:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,left,down,fcolor,pcolor,psize,(-plenght*2,-plenght),(-plenght,-plenght),i)print(posdict)#对称if symmetrical!f:self.goto_(symmetrical_point)posdict[symmetrical_step]self.step(lenghtlenght,heightheight,blenghtblenght,plenghtplenght,directionsymmetrical_draw_direction,fcolorfcolor,pcolorpcolor,psizepsize)self.goto_(posdict[stepstr(lenght-1)][vlinestr(blenght-1)][pointstr(height-1)][0])return posdict使用方法 
pos_listpxtool.step(2,2,2,10,directionleftdown,fcolorred,pcolorblue,symmetrical5,symmetrical_directionleft)结果  
祝大家新年快乐CSDN牛气冲天 
以下代码由《小清新风格的树》组成。  
完整代码如下 core.py文件 
from turtle import *class Core:设置#填充颜色色值#fcolor点填充颜色def fillcolor_set(self,fcolorblack):fillcolor(fcolor)#笔杆颜色设置#fcolor点填充颜色#pcolor线颜色def pencolor_set(self,fcolorblack,pcolor1):if pcolor1:pcolorfcolorpencolor(pcolor)else:pencolor(pcolor)#笔杆尺寸#psize线尺寸def pensize_set(self,psize1):pensize(psize)other func #跳转def loc_goto(self,movepos):penup()goto(pos()movepos)pendown()def goto_(self,pos_):penup()goto(pos_)pendown()#line方法的绘图跳转控制def line_control_func_draw_move(self,cout_i,direction,lenght,plenght):if cout_i!(lenght-1):if directionright:self.loc_goto((plenght,0))elif directionleft:self.loc_goto((-plenght,0))elif directionup:self.loc_goto((0,plenght))elif directiondown:self.loc_goto((0,-plenght))elif cout_i(lenght-1):if directionleft:self.loc_goto((plenght,0))if directionup:self.loc_goto((0,-plenght))#step方法的绘图跳转控制#lenght总长#blenghtbit一个位长度#plenght点长度#direction1横线绘制方向#direction2竖线绘制方向#fcolor填充颜色#pcolor笔颜色#psize笔大小#gotopos如何跳转pos位置#cout_i循环控制变量idef step_control_func_draw_move(self,lenght,blenght,plenght,direction1,direction2,fcolor,pcolor,psize,gotopos1,gotopos2,cout_i,height):print(-----------,blenght)posdict{}posdict[linestr(cout_i)]self.line(lenghtblenght,plenghtplenght,directiondirection1,fcolorfcolor,pcolorpcolor,psizepsize)self.loc_goto(gotopos1)posdict[vlinestr(cout_i)]self.line(lenghtheight,plenghtplenght,directiondirection2,fcolorfcolor,pcolorpcolor,psizepsize)if cout_i!(lenght-1):self.loc_goto(gotopos2)return posdict绘制#绘制点#plenght点长度#fcolor点填充颜色#pcolor线颜色#psize线尺寸def point(self,plenght10,fcolorblack,pcolor1,psize1):poslist[]self.fillcolor_set(fcolor)self.pencolor_set(fcolor,pcolor)self.pensize_set(psize)begin_fill()for i in range(0,4):poslist.append(pos())fd(plenght)right(90)end_fill()return poslist#绘制线段#lenght线长度#plenght点长度#fcolor点填充颜色#pcolor线颜色#psize线尺寸def line(self,lenght1,plenght10,directionright,fcolorblack,pcolor1,psize1,symmetricalf,symmetrical_directionright):posdict{}symmetrical_pointfif symmetrical!f:if symmetrical_directionright:symmetrical_pointpos()(int(symmetrical)*plenght,0)elif symmetrical_directionleft:symmetrical_pointpos()(-int(symmetrical)*plenght,0)elif symmetrical_directionup:symmetrical_pointpos()(0,int(symmetrical)*plenght)elif symmetrical_directiondown:symmetrical_pointpos()(0,-int(symmetrical)*plenght)for i in range(0,lenght):posdict[pointstr(i)]self.point(plenghtplenght,fcolorfcolor,pcolorpcolor,psizepsize)self.line_control_func_draw_move(i,direction,lenght,plenght)if symmetrical!f:self.goto_(symmetrical_point)posdict[symmetrical_point]self.line(lenghtlenght,plenghtplenght,directiondirection,fcolorfcolor,pcolorpcolor,psizepsize,symmetricalf)self.goto_(posdict[pointstr(lenght-1)][3])return posdict    #绘制线段#lenght线长度#height线高度#blenghtbit一个位长度#plenght点长度#fcolor点填充颜色#pcolor线颜色#psize线尺寸def step(self,lenght1,height1,blenght1,plenght10,directionright,fcolorblack,pcolor1,psize1,symmetricalf,symmetrical_directionright):posdict{}symmetrical_pointfsymmetrical_draw_directionif symmetrical!f:if symmetrical_directionright:symmetrical_pointpos()(int(symmetrical)*plenght,0)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownelif symmetrical_directionleft:symmetrical_pointpos()(-int(symmetrical)*plenght,0)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownelif symmetrical_directionrightdown:symmetrical_pointpos()(0,int(symmetrical)*plenght)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownelif symmetrical_directionleftdown:symmetrical_pointpos()(0,-int(symmetrical)*plenght)if directionright:symmetrical_draw_directionleftelif directionleft:symmetrical_draw_directionrightelif directionrightdown:symmetrical_draw_directionleftdownelif directionleftdown:symmetrical_draw_directionrightdownfor i in range(0,lenght):if directionright:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,direction,up,fcolor,pcolor,psize,(plenght,plenght),(plenght,plenght*2),i,height)elif directionleft:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,left,up,fcolor,pcolor,psize,(-plenght*2,plenght),(-plenght,plenght*2),i,height)elif directionrightdown:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,right,down,fcolor,pcolor,psize,(plenght,-plenght),(plenght,-plenght),i,height)elif directionleftdown:posdict[stepstr(i)]self.step_control_func_draw_move(lenght,blenght,plenght,left,down,fcolor,pcolor,psize,(-plenght*2,-plenght),(-plenght,-plenght),i,height)#对称if symmetrical!f:self.goto_(symmetrical_point)posdict[symmetrical_step]self.step(lenghtlenght,heightheight,blenghtblenght,plenghtplenght,directionsymmetrical_draw_direction,fcolorfcolor,pcolorpcolor,psizepsize)print(posdict)self.goto_(posdict[stepstr(lenght-1)][vlinestr(lenght-1)][pointstr(height-1)][0])return posdictfrom core import Core
from turtle import *
import random
tracer(False)
setworldcoordinates(-1000,-750,1000,750)  
def drawTree(length):if length1:if length30 and length14:#缩小一下树干pensize(4)elif length15 and length5:#长度这个范围内那么就是绿叶color(#04B486)#pensize(3)elif length5 and length1:#红花color(#FE2E9A)pensize(2)else:color(#5E5E5E)#其他范围就是正常的树干pensize(5)#随机角度与长度randangle2*random.random()randlen2*random.random()#每次使用函数先绘制线段再调整角度这里是向右的角度转动fd(length)right(20*randangle)drawTree(length - 10*randlen)#这里是向左的角度转动left(40 * randangle)drawTree(length - 10*randlen)#为什么需要再向右转20度那是因为我一共向左转了40度使用backward后退必须是相同的角度不然退回去角度就不同了位置就不会对right(20 * randangle)up()backward(length)down()
def fallingFlowers(m):x,y-1000,-750for i in range(30):up()goto(x,y)x100down()yval50for i in range(m):a  100*random.random()b  2*random.random()print(a)if a59:color(#FE2E9A)else:color(#04B486)circle(5)up()goto(x,y(yval*b))fd(a)yval50down()      def treemain():               fallingFlowers(10)#绘制落叶bgcolor(#F5F6CE)color(#5E5E5E)pensize(5)up()goto(0,-700)#跳到绘制起始点down()left(80)fd(140)drawTree(120)pxtoolCore()
def degoto(posv):penup()goto(posv)pendown()weight_v20
#牛头
def cattle():#pxtool.line(4,10,directionright,fcolorred,pcolorblue)pos_listpxtool.step(1,4,9,30,directionleft,fcolorred,pcolorblue,symmetrical1,symmetrical_directionright)penup()goto(pos_list[step0][line0][point5][2])pendown()for i in range(10):pxtool.line(6,30,directiondown,fcolorred,pcolorblue)degoto(pos()(30,30*5))
def cwrite():#Cpxtool.line(4,weight_v,directionleft,fcolorred,pcolorblue)degoto(pos()(-weight_v,0))pxtool.step(2,1,2,weight_v,directionleftdown,fcolorred,pcolorblue)pxtool.line(8,weight_v,directiondown,fcolorred,pcolorblue)pxtool.step(1,1,2,weight_v,directionrightdown,fcolorred,pcolorblue)pxtool.line(7,weight_v,directionright,fcolorred,pcolorblue)
def swrite():#spxtool.step(1,5,5,weight_v,directionleftdown,fcolorred,pcolorblue)pxtool.step(1,5,5,weight_v,directionrightdown,fcolorred,pcolorblue)pxtool.line(6,weight_v,directionleft,fcolorred,pcolorblue)
def dwrite():#dpxtool.line(11,weight_v,directiondown,fcolorred,pcolorblue)pxtool.line(6,weight_v,directionright,fcolorred,pcolorblue)pxtool.step(1,2,2,weight_v,directionright,fcolorred,pcolorblue)pxtool.line(10,weight_v,directionup,fcolorred,pcolorblue)degoto(pos()(0,weight_v))pxtool.line(8,weight_v,directionleft,fcolorred,pcolorblue)
def nwrite():#npxtool.line(10,weight_v,directionup,fcolorred,pcolorblue)degoto(pos()(weight_v,0))pxtool.step(4,1,1,weight_v,directionrightdown,fcolorred,pcolorblue)pxtool.line(10,weight_v,directionup,fcolorred,pcolorblue)    
treemain()degoto((-weight_v*11,0))
cwrite()
degoto((-weight_v*11,0))
degoto(pos()(weight_v*8,0))
swrite()
degoto((-weight_v*11,0))
degoto(pos()(weight_v*11,-weight_v))
dwrite()
degoto(pos()(weight_v*11,-weight_v*9))
nwrite()degoto((0,-weight_v*21))
cattle()
#pos_listpxtool.line(4,10,directionleft,fcolorred,pcolorblue,symmetrical5,symmetrical_directiondown)#goto(pos_list[point3][0])
#goto((0,0))
#fd(30)
input()