你不知道的python内置绘图库:turtle高级例子
1 说明
====
1.1 turtle库:海龟库 , 是python3的内置库 。
【你不知道的python内置绘图库:turtle高级例子】1.2 我已经做了初步介绍:
《Turtle海龟库:python3内置简单却很强大的绘图库》
1.3 今天来介绍几个高级例子 , 顺带学习turtle绘图相关知识和python的相关知识 , 代码讲解清楚 , 注释仔细 , 小白秒懂 , 干货满满 , 适合收藏 。
文章插图
2 内容
=====
2.1 转动的太极
文章插图
2.2 贪食蛇
文章插图
2.3 国画:
文章插图
2.4 国际象棋棋盘
文章插图
3 转动太极的代码
#文章来源##旋转的太极import turtle as t t.bgcolor('gray')t.title('转动的太极')# radius可以看作半径 , extent是角度# fc是填充的颜色 , 默认为白色 。 # 每次画完圆之后都会回到原点 。 #定义函数圆def yuan(radius, extent=None, fc='white'):# setheading是保证当次循环是同一朝向 。t.setheading(dd)t.forward(radius)t.left(90)t.down()t.fillcolor(fc)t.begin_fill()t.circle(radius, extent)t.end_fill()t.up()t.right(90)t.back(radius) a = 8dd = a#隐藏笔头t.hideturtle()while True:t.goto(0, 0)t.tracer(0, 0)# 画个黑半圆 ,yuan(200, extent=180, fc='black')# 画个白半圆 。yuan(200, extent=-180)# 向前100 , 画个黑半圆 。t.forward(100)yuan(100, extent=-180, fc='black')# 向后200 , 画个白半圆 。t.back(200)yuan(100, extent=180)# 顺便画个小黑圆 , 比白圆大点是因为白圆还有边框 , 显得大 。yuan(33, fc='black')# 向后200 , 画个小白圆 。 要问我为什么不是向前200 , 因为我试出来的233333t.back(200)yuan(30)# 延时10毫秒 , 不加这句的话 , 因为tracer(0,0)是画完才显示 , 会导致一直啥都画不出来 。t.tracer(1, 10)# 然后下次循环的goto在tracer(0,0)之前这个很有讲究 。 这相当于是tracerdd += aif dd > 360:dd -= 360
4 贪食蛇的代码
#文章来源#from turtle import *import timeimport randomtracer(10000000,0)#正方形框大小与蛇大小、水果大小绑定size=1000#蛇的速度snakeSpeed=5stepNum=20stepSize=size/stepNumpoints=0direction=0snakeLen=4snake="0,0|-1,0|-2,0|-3,0|"fruit=[1,1]getFruit=FalsesleepTime=1/snakeSpeed#窗口大小设置setup(1200,1200)def getSnake(barNum): barCount=0 STR="" x=0;y=0 for i in range(len(snake)):if barCount==barNum:j=iwhile snake[j]!=',':STR+=snake[j]j+=1x=int(STR)j+=1STR=""while snake[j]!='|':STR+=snake[j]j+=1y=int(STR)breakif snake[i]=='|':barCount+=1 return [x,y]def move(): newSnake="" if direction==0:readReturn=getSnake(0)newSnake+=str(readReturn[0]+1); newSnake+=","; newSnake+=str(readReturn[1]); newSnake+="|"for i in range(snakeLen-1):readReturn=getSnake(i)newSnake+=str(readReturn[0]); newSnake+=","; newSnake+=str(readReturn[1]); newSnake+="|" if direction==1:readReturn=getSnake(0)newSnake+=str(readReturn[0]); newSnake+=","; newSnake+=str(readReturn[1]-1); newSnake+="|"for i in range(snakeLen-1):readReturn=getSnake(i)newSnake+=str(readReturn[0]); newSnake+=","; newSnake+=str(readReturn[1]); newSnake+="|" if direction==2:readReturn=getSnake(0)newSnake+=str(readReturn[0]-1); newSnake+=","; newSnake+=str(readReturn[1]); newSnake+="|"for i in range(snakeLen-1):readReturn=getSnake(i)newSnake+=str(readReturn[0]); newSnake+=","; newSnake+=str(readReturn[1]); newSnake+="|" if direction==3:readReturn=getSnake(0)newSnake+=str(readReturn[0]); newSnake+=","; newSnake+=str(readReturn[1]+1); newSnake+="|"for i in range(snakeLen-1):readReturn=getSnake(i)newSnake+=str(readReturn[0]); newSnake+=","; newSnake+=str(readReturn[1]); newSnake+="|" if getFruit==True:readReturn=getSnake(snakeLen-2)newSnake+=str(readReturn[0]); newSnake+=","; newSnake+=str(readReturn[1]); newSnake+="|" return newSnakedef drawSnake(clr): pensize(stepSize-2) readReturn=getSnake(0) penup() goto(readReturn[0]*stepSize,readReturn[1]*stepSize,) pendown() for i in range(snakeLen):readReturn=getSnake(i)goto(readReturn[0]*stepSize,readReturn[1]*stepSize,) penup()def check(): readReturn=getSnake(0) global getFruit getFruit=False snkl=snakeLen for i in range(snakeLen-1):readReturn1=getSnake(i+1)if readReturn[0]==readReturn1[0] and readReturn[1]==readReturn1[1]:#hit selftime.sleep(2)bye() if readReturn[0]>stepNum/2 or readReturn[1]>stepNum/2 or readReturn[0]5 国画的代码
"""author:魏振东data:2019.10.25func:绘制创意图形"""#画梅花诗画from turtle import *from random import *title('国画')#画雪花def drawSnow():hideturtle()pensize(2)for i in range(20):pencolor("white")penup()setx(randint(-640,640))sety(randint(-400,400))pendown()dens=randint(8,12)snowsize=randint(10,14)for j in range(dens):forward(snowsize)backward(snowsize)right(360/dens)# 位移函数def gotopos(x, y):up()goto(x, y)down()ht()def apply_rules(path, rules):L = [_ for _ in path]for i in range(len(L)):symbol = L[i]if symbol == 'F':L[i] = rules[symbol]if symbol == 'X':L[i] = rules[symbol]path = ''.join(L)return pathdef draw_path(path):posList, angleList = [], []for symbol in path:if symbol == 'F':forward(length)elif symbol == '+':left(angle)elif symbol == '-':rt(angle)elif symbol == '[':posList.append(pos())angleList.append(heading())elif symbol == 'a':pensize(3)color("black")elif symbol == 'b':pensize(2)color("black")elif symbol == 'c':pensize(2)color("black")elif symbol == ']':up()home()goto(posList.pop())left(angleList.pop())down()# 写字def writez(x, y, str_, size=16, font="Arial"):gotopos(x, y)write(str_, font=(font, size))# 画布setup(1280, 800)speed(5)bgcolor("Silver")# 题字color("black")writez(-213, -210, "梅", 16)writez(-50, 100, "宋")writez(80, 20, "王")writez(33, -30, "安", 16)writez(-18, -95, "石", 16)# 写诗color("black")s1 = "墙角数枝梅"s2 = "凌寒独自开"s3 = "遥知不是雪"s4 = "为有暗香来"for i in range(len(s1)):writez(560, 350 - i * 50, s1[i], 16)for i in range(len(s2)):writez(510, 320 - i * 50, s2[i], 16)for i in range(len(s3)):writez(460, 290 - i * 50, s3[i], 16)for i in range(len(s4)):writez(410, 260 - i * 50, s4[i], 16)# 画雪drawSnow()# 画梅gotopos(249, -26)speed(0)gotopos(-650, -100)length = 6path = 'F'angle = 27rules = {'F': 'aFF[b-F++F][c+F--F]c++F--F','X': 'aFF+[b+F]+[c-F]'}for _ in range(4):path = apply_rules(path, rules)draw_path(path)done()
- 麒麟|荣耀新款,麒麟810+4800万超清像素,你还在犹豫什么呢?
- 智能手机市场|华为再拿第一!27%的份额领跑全行业,苹果8%排在第四名!
- 会员|美容院使用会员管理软件给顾客更好的消费体验!
- 行业|现在行业内客服托管费用是怎么算的
- 人民币|天猫国际新增“服务大类”,知舟集团提醒入驻这些类目的要注意
- 国外|坐拥77件专利,打破国外的垄断,造出中国最先进的家电芯片
- 技术|做“视频”绿厂是专业的,这项技术获人民日报评论点赞
- 面临|“熟悉的陌生人”不该被边缘化
- 中国|浅谈5G移动通信技术的前世和今生
- 页面|如何简单、快速制作流程图?上班族的画图技巧get