时间:2021-05-22
本文实例主要是对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()实现效果:
总结
以上就是本文关于Python+Turtle动态绘制一棵树实例分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Turtle库是Python中一个强大的绘制图像的函数库,灵活使用Turtle库可以绘制各种好看的图像。下面介绍使用Turtle库绘制一棵西兰花。绘制一棵西兰花
一、树树与线性表、栈、队列等线性结构不同,树是一种非线性结构。一棵树只有一个根节点,如果一棵树有了多个根节点,那它已经不再是一棵树了,而是多棵树的集合,也被称为
用纯js画一棵树。思路:1、一棵树的图片,作为页面背景;2、通过html5中的canvas画布进行遮罩;3、定时每隔10ms,从下往上清除1px的遮罩;MyJS
今天为大家介绍几个Python“装逼”实例代码,python绘制樱花、玫瑰、圣诞树代码实例,主要使用了turtle库Python绘制樱花代码实例动态生成樱花效果
区块链农业软件开发应该要具备的功能特点有: 1、做公益:这种模式其实已经有一些企业在做了,即用户通过平台认购一棵树,就等于在山上种下一棵树,用这种方式让用