<>python turtle 绘制樱花树

<>1、樱花树

import turtle
from random import *
from math import *
def tree(n, l):
turtle.pendown() # 下笔
# 阴影效果
t = cos(radians(turtle.heading() + 45))/8 + 0.25
turtle.pencolor(t, t, t)
turtle.pensize(n/3)
turtle.forward(l) # 画树枝
if n > 0:
b = random() * 15 + 10 # 右分支偏转角度
c = random() * 15 + 10 # 左分支偏转角度
d = l * (random() * 0.25 + 0.7) # 下一个分支的长度
# 右转一定角度,画右分支
turtle.right(b)
tree(n - 1, d)
# 左转一定角度,画左分支
turtle.left(b + c)
tree(n - 1, d)
# 转回来
turtle.right©
else:
# 画叶子
turtle.right(90)
n = cos(radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(n, n * 0.8, n * 0.8)
turtle.circle(3)
turtle.left(90)
# 添加0.3倍的飘落叶子
if (random() > 0.7):
turtle.pu()
# 飘落
t = turtle.heading()
an = -40 + random() * 40
turtle.setheading(an)
dis = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)
turtle.forward(dis)
turtle.setheading(t)
# 画叶子
turtle.pendown()
turtle.right(90)
n = cos(radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(n * 0.5 + 0.5, 0.4 + n * 0.4, 0.4 + n * 0.4)
turtle.circle(2)
turtle.left(90)
turtle.pu()
# 返回
t = turtle.heading()
turtle.setheading(an)
turtle.backward(dis)
turtle.setheading(t)
turtle.penup()
turtle.backward(l) # 退回
# 绘图区域
turtle.bgcolor(0.5, 0.5, 0.5) # 背景色
turtle.hideturtle() # 隐藏turtle
turtle.speed(0) # 速度 1-10渐进,0 最快
turtle.tracer(0, 0)
turtle.penup() # 抬笔
turtle.backward(100)
turtle.left(90) # 左转90度
turtle.penup() # 抬笔
turtle.backward(300) # 后退300
tree(12, 100) # 递归7层
turtle.done()

<>暗色樱花

import turtle
from random import *
from math import *
def tree(n, l):
turtle.pendown() # 下笔
t = cos(radians(turtle.heading() + 45)) / 8 + 0.25
turtle.pencolor(t, t, t)
turtle.pensize(n / 4)
turtle.forward(l)
if n > 0:
b = random() * 15 + 10
c = random() * 15 + 10
d = l * (random() * 0.35 + 0.6)
turtle.right(b)
tree(n - 1, d)
turtle.left( b + c)
tree(n - 1, d)
turtle.right©
else:
turtle.right(90)
n = cos(radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(n, n, n)
turtle.circle(2)
turtle.left(90)
turtle.penup()
turtle.backward(l)
turtle.bgcolor(0.5, 0.5, 0.5)
turtle.hideturtle() # 隐藏turtle
turtle.speed(0)
turtle.tracer(0, 0)
turtle.left(90)
turtle.penup()
turtle.backward(300)
tree(13, 100)
turtle.done()

以上代码可直接运行

技术
©2019-2020 Toolsou All rights reserved,
01-C语言之父:丹尼斯·里奇为什么 Python 不是面向未来的编程语言?利用Python处理Excel数据——xlrd,xlwt库# C# 学习笔记(4) 类C语言中四种取整方式、取余/取模的运算以及负数取模的问题java两个对象比较属性值初学者学Python一定要知道他的基础算法有哪些?作用是什么?面试经常被问到-拉链表C++实现简单的通讯录管理系统利用Python处理Excel数据——pandas库