下列turtle库的简单常用指令

forward(distance) #将箭头移到某一指定坐标

left(angel)  right(angel)

penup() #提起笔,用于另起一个地方绘制时用,与pendown()配对使用

goto(x,y)

home()

circle(radius)

speed()

 #五角星图形
from turtle import Turtle p = Turtle()
p.speed(3)
p.pensize(5)
p.color("black", 'yellow')
#p.fillcolor("red")
p.begin_fill()
for i in range(5):
p.forward(200) #将箭头移到某一指定坐标
p.right(144) #当前方向上向右转动角度
p.end_fill()

树的绘制

观察:对称树, 从主杆出发以一定角度向左向右生成对称的枝丫, 且每一棵枝杈上以相同的角度生成更小的左右枝杈,如此往复。

    联系:所学内容,易想到利用递归程序实现;

 # 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.getscreen().tracer(10,0)
#Return the TurtleScreen object the turtle is drawing on.
#TurtleScreen methods can then be called for that object.
#p.speed(10)
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()

森林的绘制 

如何画出多棵树,甚至整片森林呢?

答案很简单,只要在画每棵树之前调整画笔的位置,调用画树程序,就可以从新位置生成一颗新树了。

利用模块化的函数思想,调整代码:

将每棵树的绘制以maketree函数封装,参数x,y为画树的起点位置即树根位置。在main函数中只要以

 不同的参数设置来调用maketree函数就可以完成多棵树的绘制了

 # 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 maketree(x, y):
p = Turtle()
p.color("green")
p.pensize(5)
p.hideturtle()
p.getscreen().tracer(30, 0)
p.left(90) p.penup()
p.goto(x, y)
p.pendown() t = tree([p], 110, 65, 0.6375)
print(len(p.getscreen().turtles())) #用了多少个turtle绘制 def main():
maketree(-200, -200)
maketree(0, 0)
maketree(200, -200) main()

  图像:

    

Turtle库的更多相关文章

  1. Turtle库的学习积累

    1.什么是turtle库 Python的Turtle库是一个直观有趣的图形绘制函数库,Turtle英文翻译过来是乌龟的意思,在绘图时可以想象成一只乌龟在移动. 2.绘图坐标体系 海龟的移动方向 3.绘 ...

  2. turtle库的学习

          Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动, ...

  3. 【Python 14】分形树绘制2.0(重复五角星+Turtle库文档)

    1.案例描述 加入循环操作绘制重复不同大小的图形 2.案例分析 3.turtle库补充 # 画笔控制函数 turtle.penup() # 抬起画笔,之后移动画笔不绘制图形 turtle.pendow ...

  4. 【Python 13】分形树绘制1.0--五角星(turtle库)

    1.案例描述 2.案例分析 引入绘制图形的turtle库,利用库中函数进行编程. 3.turtle库 没有显示的input()和output(),没有赋值语句.调用形式大部分如下: import tu ...

  5. Python使用turtle库与random库绘制雪花

    记录Python使用turtle库与random库绘制雪花,代码非常容易理解,画着玩玩还是可以的. 完整代码如下:   效果图如下:  

  6. python中关于汉诺塔问题和使用turtle库实现其搬运过程

    一.汉诺塔问题 汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下面开始按 ...

  7. python turtle库的几个小demo

    一.先上图 一个同切圆和五角星 上代码 import turtle #同切圆 turtle.pensize(2) turtle.circle(10) turtle.circle(40) turtle. ...

  8. [Python学习笔记] turtle库的基本使用

    turtle库常用函数 引入turtle模块 import turtle turtle的绘图窗体 #setup()设置窗口大小及位置#setup()可省略turtle.setup(width,heig ...

  9. 用turtle库实现汉诺塔问题~~~~~

    汉诺塔问题 问题描述和背景: 汉诺塔是学习"递归"的经典入门案例,该案例来源于真实故事.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬ ...

随机推荐

  1. JS--图片轮播效果

    搞了很长时间才弄清楚图片轮播效果的原理,理解各个事件发生的原因,浪费了这么长的时间,只怪自己的知识太过于薄弱.现将代码写下,供大家参看,如有不妥之处还望指出,大家一起学习. 功能: 1.点击左右两边的 ...

  2. 一个简单LINUX程序的逆向

    开始之前的准备: 反汇编:IDA 十六进制编辑器: Hexworkshop LINUX环境: KALI LINUX 调试: EDB (KALI自带的) 一个简单的动态追码, 大牛们就略过吧…… 用16 ...

  3. favicon支持的图片格式

    为网站设置favicon有两种方式: 1.网站根目录下放置名为favicon.ico的图片,浏览器就会自动获取: 2.在页面中通过<link rel="shortcut icon&qu ...

  4. [布局]bootstrap基本标签总结2

    缩略图 <div class="container"> <div class="row"> <div class="co ...

  5. Android 中如何使用动画

    1:首先在res/anim/文件夹下建立动画xml文件. 2:在java代码中对UI控件使用动画. //加载动画 Animation myAnim=AnimationUtils.loadAnimati ...

  6. 一句话输出网站404页面,REFER及相关排序

    cat www.log|awk '$9~/404/ {print $7"," $11}'|sort|uniq -c|sort -nr > ./www404.csv

  7. java中jsoup框架解析html

    今天遇到对网页内容进行操作,思考了一下,先获取连接后的html内容,然后对html文档进行操作呗.思路没有问题,但是问题还是不少.于是便找到了jsoup这个神器了... 1.什么是jsoup? 百度百 ...

  8. BZOJ3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者

    3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 47  Solve ...

  9. PC格局已改变 联想未来的短板在哪里?

    PC格局已改变 联想未来的短板在哪里? 作者:孙永杰2013-11-13 11:34评论:15   [PConline 品科技](原文标题:PC格局已改:联想未来的短板在哪里?)近日,联想集团公布了截 ...

  10. storm高级原语-Transactional topology

    参考: http://xumingming.sinaapp.com/736/twitter-storm-transactional-topolgoy/ http://xumingming.sinaap ...