python编程:利用函数递归调用和turtle绘制树-3

简介: python编程:利用函数递归调用和turtle绘制树-3

image.png

源代码(还不是太了解~~~囧)

# 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()

相关文章
|
2月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
213 1
|
2月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
329 2
|
2月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
190 0
|
3月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
282 101
|
3月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
234 99
|
3月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
204 98
|
3月前
|
缓存 Python
Python中的装饰器:优雅地增强函数功能
Python中的装饰器:优雅地增强函数功能
|
3月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
319 102
|
3月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
345 104
|
3月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
275 103

推荐镜像

更多