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
0
投稿
猜你喜欢
- <base href="http://digi.tech.qq.com/images/ld/2007/1022/
- 前言python使用中多线程、多进程、多协程使用是比较常见的。那么如果在多线程等的使用,我们这个时候我们想从外部强制杀掉该线程请问如何操作?
- 前言HTML 5如同一场革命,正在Web2.0后时代轰轰烈烈的进行着。HTML 5是什么,无须我在这里赘述了。对于HTML 5的革新,按我的
- 在编写程序时,数据库结构会经常变化,所以经常需要编写一些数据库脚本,编写完成后需发往现场执行,如果已经存在或者重复执行,有些脚本会报错,所以
- 一、利用ASP和ADO实现数据库操作的工作流程ASP内嵌了五个对象Resquest、Response、Server、Seesion、Appl
- 演示:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
- 前言ThinkPHP出于安全的考虑增加了表单令牌Token,由于通过Ajax异步更新数据仅仅部分页面刷新数据,就导致了令牌Token不能得到
- PDOStatement::nextRowsetPDOStatement::nextRowset — 在一个多行集语句句柄中推进到下一个行集
- Entity Framework 4.0 也可以支持大名鼎鼎的MySql,这篇POST将向展示如何实现EF+MyS
- 1、ul标签在Mozilla中默认是有padding值的,而在IE中只有margin有值。 2、同一个的class选择符可以在一个
- 网上考试设计思路是怎样的?为了运行这个应用程序,我们需要在global.asa文件里进行设置数据库的连接。global.asa <&n
- 让我们看看如何在 Python 中复制数组。 有 3 种复制数组的方法:只需使用赋值运算符。浅拷贝深拷贝1 使用赋值运算符我们可以使用赋值运
- 让我们重温一下JavaScript的一些基础知识,请先写出以下代码中问号处的答案,再运行比较!<script type=&q
- 目前两个客户端扩展库连接超时可以设置选项来操作,比如mysqli: <?php //创建对象 $mysqli = mysqli_ini
- 1.安装Apachea) 双击文件Apach_2.2.8_win32-x86-no_ssl.msi,弹出欢迎界面。单NEXT按钮,进入到Li
- 用dicompyler软件打开dicom图像,头文件如图所示:当然也可以直接读取:ds = dicom.read_file('H:\
- 对想要在可视化的环境下制作复杂网页的专业网页制作者来说,Dreamweaver 已经渐渐在网页编辑工具市场中展露头角,成为专业人士
- 00 小编的问题小编向我们反馈,从微信里复制出来的图片,会被微信屏蔽,无法显示我们后天采用的是百度编辑器,而且已经做了远程图片本地化,于是检
- 本文实例讲述了PHP实现逐行删除文件右侧空格的方法。分享给大家供大家参考,具体如下:在编辑整理代码的过程中发现网上的一些代码经常会有不少的右
- 原文:http://research.microsoft.com/~helenw/papers/subspace.pdfwindow.nam