Python+Turtle动态绘制一棵树实例分享
作者:wangsiting123 发布时间:2021-10-03 00:33:20
标签:python,turtle,实例
本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码:
# drawtree.py
from turtle import Turtle, mainloop
def tree(plist, l, a, f):
""" plist is list of pens
l is length of branch
a is half of the angle between 2 branches
f is factor by which branch is shortened
from level to level."""
if l > 5: #
lst = []
for p in plist:
p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
p.left(a) #Turn turtle left by angle units
q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
lst.append(p)#将元素增加到列表的最后
lst.append(q)
tree(lst, l*f, a, f)
def main():
p = Turtle()
p.color("green")
p.pensize(5)
#p.setundobuffer(None)
p.hideturtle() #Make the turtle invisible. It's a good idea to do this while you're in the middle of doing some complex drawing,
#because hiding the turtle speeds up the drawing observably.
#p.speed(10)
# p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.
p.speed(10)
#TurtleScreen methods can then be called for that object.
p.left(90)# Turn turtle left by angle units. direction 调整画笔
p.penup() #Pull the pen up – no drawing when moving.
p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle's orientation.
p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
#否则turtle一移动就会自动的把线画出来
#t = tree([p], 200, 65, 0.6375)
t = tree([p], 200, 65, 0.6375)
main()
实现效果:
来源:https://www.cnblogs.com/wangsiting/p/7236327.html


猜你喜欢
- 使用shutil.move(src, dst),src为要移动的文件的路径,dst为目的路径,路径必须是绝对路径import osimpor
- 关于怎么获得,我想其实网上有很多答案。list.index( )获得值的索引值,但是如果list中含有的值一样,例如含有两个11,22,这样
- 本文实例讲述了Python单例模式的使用方法。分享给大家供大家参考。具体如下:方法一import threading  
- 之前写了一个matlab的,越用越觉得麻烦,如果不同数据集要改类别数目,而且运行速度慢。所以重新写了一个Python的,直接读取xml文件夹
- 如果您想详细了解eval和JSON请参考以下链接:eval :https://developer.mozilla.org/En/
- 实例如下所示:# -*- coding: utf-8 -*-#to find where use the table on xxxxx xx
- 一、安装1、安装git Windows 客户端—GitHub Desktop具有git功能的Windows客户端软件有很
- 池化层定义在tensorflow/python/layers/pooling.py.有最大值池化和均值池化。1、tf.layers.max_
- 本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下目录简介实现过程结语简介使用python实现pygame版的五
- 我们已经了解到MySQL可以通过 LIKE ...% 来进行模糊匹配。MySQL 同样也支持其他正则表达式的匹配, MySQL中使用 REG
- 1.使用jobsName.ini文件保存要创建job的名字jobs1jobs2jobs32.使用Jenkins创建job时自动生成的conf
- 迷宫问题问题描述:迷宫可用方阵 [m, n] 表示,0 表示可通过,1 表示不能通过。若要求左上角 (0, 0) 进入,设计算法寻求一条能从
- 服务器系统变量服务器将维护许多表示其配置的系统变量。所有变量均有默认值。可以在命令行中或选项文件设置选项在服务器启动时对它们进行设置。大多数
- Mysql 查询缓存 查询缓存的作用就是当查询接收到一个和之前同样的查询,服务器将会从查询缓存种检索结果,而不是再次分析和执行上次的查询。这
- 前言PyTorch作为一款深度学习框架,已经帮助我们实现了很多很多的功能了,包括数据的读取和转换了,那么这一章节就介绍一下PyTorch内置
- 目录主页分析代码:修改视图函数返回值修改 URL 规则修改视图函数名总结追溯到最初,Flask 诞生于 Armin Ronacher 在 2
- 触发器权限和所有权CREATE TRIGGER 权限默认授予定义触发器的表所有者、sysadmin 固定服务器角色成员以及 db_owner
- Python虚拟机注:本篇是根据教程学习记录的笔记,部分内容与教程是相同的,因为转载需要填链接,但是没有,所以填的原创,如果侵权会直接删除。
- 导语嘿!下午好,木子来上新啦~期待今天的内容嘛?挠头.jpg 日常等更新的小可爱们我来了。看看给大家带来了什么好东西💦💦💦💦💦💦💦💦💦💦💦💦
- 前言最近一直在研究 GoFrame 框架,经过一段时间的使用、总结、思考,发现确实不失为一款非常值得使用的企业级开发框架。在我初识GoFra