中国大学 MOOC 课程 《Python 语言程序设计》第五周绘制树
来源:互联网 发布:淘宝满1000减50怎么用 编辑:程序博客网 时间:2024/05/16 06:18
中国大学 MOOC 课程 《Python 语言程序设计》第五周绘制树
# drawtree.pyfrom turtle import Turtle, mainloopdef 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: #与之前的基例不同,这次递归,采用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()#克隆一个画笔在当前位置(克隆一个乌龟) 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) #再次调用tree函数,这是递归的关键!!!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(200) #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()
阅读全文
0 0
- 中国大学 MOOC 课程 《Python 语言程序设计》第五周绘制树
- 中国大学 MOOC 课程 《Python 语言程序设计》第五周课后习题
- 中国大学 MOOC 课程 《Python 语言程序设计》第六周
- 中国大学 MOOC 课程 《Python 语言程序设计》第六周课后习题
- 中国大学MOOC课程《Python语言程序设计》课后练习第一周
- 中国大学 MOOC 课程 《Python 语言程序设计》第七周课上练习
- 中国大学 MOOC 课程 《Python 语言程序设计》 课后练习(第 2 周)
- 中国大学 MOOC 课程 《Python 语言程序设计》 课后练习(第 4 周)
- 中国大学MOOC-翁恺-C语言程序习题第五周
- 中国大学mooc编程题在线测试第五周
- 中国大学MOOC-翁恺-C语言程序设计习题集(学习笔记)
- PAT|中国大学MOOC-翁恺-C语言程序设计习题集01-04
- PAT|中国大学MOOC-翁恺-C语言程序设计习题集05-08
- PAT|中国大学MOOC-翁恺-C语言程序设计习题集09-12
- PAT|中国大学MOOC-翁恺-C语言程序设计习题集13-16
- PAT|中国大学MOOC-翁恺-C语言程序设计习题集17-20
- PAT|中国大学MOOC-翁恺-C语言程序设计习题集21-24
- PAT|中国大学MOOC-翁恺-C语言程序设计习题集25-28
- Java学习笔记——容器之Set
- python串口使用
- 数据结构和算法(1)数据结构的逻辑结构和物理结构
- Standard I/O 后续实验部分
- 宿主访问虚拟机的简单方式之一
- 中国大学 MOOC 课程 《Python 语言程序设计》第五周绘制树
- codeblocks编译时出错误
- Linux内核内存压缩技术
- Spring事务的传播和隔离级别
- linux常用命令2
- SpringCloud学习
- 个人学习笔记-操作系统银行家算法题目
- 【Scikit-Learn 中文文档】流形学习
- JavaWeb基础(7)—— Filter 过滤器