函数实例-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()

原文地址:https://www.cnblogs.com/Wang-Y/p/8444889.html

时间: 2024-07-30 19:23:28

函数实例-turtle画树的相关文章

*【Python】【demo实验31】【练习实例】【使用turtle画小猪佩奇】

如下图小猪佩奇: 要求使用turtle画小猪佩奇: 源码: # encoding=utf-8 # -*- coding: UTF-8 -*- # 使用turtle画小猪佩奇 from turtle import* def nose(x,y):#鼻子 penup()#提起笔 goto(x,y)#定位 pendown()#落笔,开始画 setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东.90-北.180-西.270-南) begin_fill()#准备开始填充图形 a

(015)实现一个函数检查一棵树是否平衡(keep it up)

实现一个函数检查一棵树是否平衡.对于这个问题而言, 平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1. 这个题我们可以采用暴力搜索,找到叶子节点到根节点的最小值和最大值,然后他们的差如果大于1就不是平衡树,反之 则是平衡树. int MinDepth = std::numeric_limits<int>::max(); int MaxDepth = std::numeric_limits<int>::min(); struct TreeNode { int data; T

Undraw the Trees 字符串数组递归画树

Undraw the Trees 题目抽象:给出一个二维字符数组表示的树,将其表示成一维字符数组表示的树. 思路:直接在二维字符数组中递归画树即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 const int MS=205; 8

PHP加密解密函数实例教程

PHP加密解密函数分享,一个是Discuz!的authcode加密函数(带详细分解),一个是encrypt()函数. 需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理.最常见的应用在用户登录以及一些API数据交换的场景.笔者收录了一些比较经典的PHP加密解密函数代码,分享给大家.加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果.1.非常给力的authcode加密

4.2 access函数实例

int access(const char *filenpath, int mode); 功 能: 确定文件或文件夹的访问权限. mode,要判断的模式 在头文件unistd.h中的预定义如下: #define R_OK 4 /* Test for read permission. */ #define W_OK 2 /* Test for write permission. */ #define X_OK 1 /* Test for execute permission. */ #define

插入函数实例

--插入函数实例 create table stu(    stu_name varchar(100) null,    stu_age int);insert into stu values('张三',88);insert into stu values('asd',66);insert into stu values('[email protected]#',22);insert into stu values('nihao',2);insert into stu values('ss''s

程序4-6 utime函数实例

1 //http://blog.chinaunix.net/uid-24549279-id-71355.html 2 /* 3 ============================================================================ 4 Name : test.c 5 Author : blank 6 Version : 7 Copyright : Your copyright notice 8 Description : 程序4-6 utime函

php 函数strtr 替换函数实例解析 strtr 速度比较快

先来看看这个php字符串替换函数 PHP字符串替换函数strtr()的两种状态 strtr(string,from,to) 或者strtr(string,array) 首先针对PHP字符串替换函数strtr()第一种方式 我们看看下面的举例: <?php echo strtr("I Love you","Lo","lO"); ?> 得到的结果是 I lOve yOu 这个结果提醒我们 1.strtr它是区分大小写的 2.PHP字符串替

鼠绘(3)[一种画树的方法]

最近研究出来用鼠标画树的方法,分享给大家 软件 Photoshop 工具 画笔 画笔属性 硬边的圆形画笔,画笔大小从4-20,如下图: 不透明度调整为80%,如下图: 主要颜色 树干颜色色板如下: 树叶颜色色板如下: 具体步骤 首先,当然是画树干,先新建一个图层(新建图层的方法用PS的人都知道,我就不说了),然后用树干色板中的第一个颜色把树干的轮廓画出来 PS:不是一般的丑,哈哈 然后配合使用树干色板中的三个颜色画出树的明暗关系,画的过程中可以将画布放大以方便画细节(快捷键"Ctrl"