下列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. 《python基础教程》笔记之 条件语句和循环语句

    布尔变量 下面的值会被解释器看做假(false): False None 0 "" () {} [] 其它的一切都被解释为真. >>> TrueTrue>& ...

  2. yield 生成器例子

    #!/usr/bin/env python #encoding: utf-8 import time def consumer(name): print ('%s 来吃包子了...' % (name) ...

  3. sqoop组件运行出错问题解决--com.mysql.jdbc.Driver

    sqoop list-tables --connect jdbc:mysql://192.168.11.94:3306/huochetoudalian --username xxx -password ...

  4. LeetCode _ Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. 浅析门户网站体育赛事CDN加速解决方案

    经过近些年来的高速发展,互联网已经成为现今媒体传播的主要力量.这一点在大型体育赛事的报道与转播上,体现的尤为明显.更及时的信息内容.更多样的报道形式.更齐全的资料组成.更方便的阅读查询.更具互动性的报 ...

  6. HDU_2052——画矩形

    Problem Description Give you the width and height of the rectangle,darw it.   Input Input contains a ...

  7. 型牌男装施春蕾:分拆让马云对淘宝定位更清晰--互联网 -- CCTIME飞象网

    型牌男装施春蕾:分拆让马云对淘宝定位更清晰--互联网 -- CCTIME飞象网 型牌男装施春蕾:分拆让马云对淘宝定位更清晰          2011年6月17日 13:16  CCTIME飞象网   ...

  8. hdu 4585 Shaolin(STL map)

    Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao ...

  9. 关于数据表命名为mysql保留的时候的操作

    今天操作数据表的时候,发现order数据表无法进行操作,必须加上反单引号才能进行操作,查了一下原因: 反引号是用来区别mysql关键字的,比如,如果你有一个表名叫select,你就必须写成`selec ...

  10. SQL-MICK基础

    /*Select语句完整的执行顺序:1.from子句组装来自不同数据源的数据:2.where子句基于指定的条件对记录行进行筛选:3.group by子句将数据划分为多个分组:4.使用聚集函数进行计算: ...