斐波那契数列的话我之前做过两期,现在做第三期了,若喜欢我的,请三连,谢谢!
import turtle
import random
from math import *
引入turtle、random和math的所有东西
def Fibonacci_Recursion_tool(n): #斐波那契数列方法
if n <= 0:
return 0
elif n == 1:
return 1
else:
return Fibonacci_Recursion_tool(n - 1) + Fibonacci_Recursion_tool(n - 2)
def Fibonacci_Recursion(n): #生成斐波那契数列,并存入列表
result_list = []
for i in range(1, n + 3):
result_list.append(Fibonacci_Recursion_tool(i))
return result_list
yu = Fibonacci_Recursion(10) #生成斐波契那数列
print(yu)
生成斐波那契数列,并打印出来(之前讲过)
def leaf(x, y, node):#定义画叶子的方法
til = turtle.heading()
i = random.random()
an = random.randint(10, 180)
ye = random.randint(6, 9)/10
turtle.color(ye, ye*0.9, 0)
turtle.fillcolor(ye+0.1, ye+0.05, 0)
turtle.pensize(1)
turtle.pendown()
turtle.setheading(an + 90)
turtle.forward(8*i)
px = turtle.xcor()
py = turtle.ycor()
turtle.begin_fill()
turtle.circle(7.5*i, 120) # 画一段120度的弧线
turtle.penup() # 抬起笔来
turtle.goto(px, py) # 回到圆点位置
turtle.setheading(an + 90) # 向上画
turtle.pendown() # 落笔,开始画
turtle.circle(-7.5*i, 120) # 画一段120度的弧线
turtle.setheading(an + 100)
turtle.circle(10.5*i, 150)
turtle.end_fill() # 画一段150度的弧线
turtle.penup()
turtle.goto(x, y)
turtle.setheading(til)
turtle.pensize(node / 2 + 1)
til就是当前的角度,i就是一个0到1之间的随机数,an就是10到180之间的随机小数,ye是一个0.6到0.9之间的十位小数。
color就是使用RGB的(red,green,blue),fillcolor一样。
p.xcor就是当前turtle的x坐标,p.ycor 就是当前turtle的y坐标。
def draw(node, length, level, yu, button): #定义画树的方法
turtle.pendown()
t = cos(radians(turtle.heading()+5)) / 8 + 0.25
turtle.pencolor(t*1.6, t*1.2, t*1.4) #(r, g, b)颜色对应的RGB值
turtle.pensize(node/1.2) #画笔的尺寸
x = random.randint(0, 10) #生成随机数决定要画树枝还是画飘落的叶子
t = cos(radians(turtle.heading()+5)) / 8 + 0.25是生成一个小数
def draw(node, length, level, yu, button): #定义画树的方法
turtle.pendown()
t = cos(radians(turtle.heading()+5)) / 8 + 0.25
turtle.pencolor(t*1.6, t*1.2, t*1.4) #(r, g, b)颜色对应的RGB值
turtle.pensize(node/1.2) #画笔的尺寸
x = random.randint(0, 10) #生成随机数决定要画树枝还是画飘落的叶子
if level == top and x > 10: #此时画飘落的叶子,x范围太大会导致树太秃
turtle.forward(length) # 画树枝,length=120
yu[level] = yu[level] - 1
c = random.randint(2, 10)
for i in range(1, c):
leaf(turtle.xcor(), turtle.ycor(), node)
# 添加0.3倍的飘落叶子
if random.random() > 0.3:
#print("随机数是",random.random())
turtle.penup() #可以用down()测试一下,当大于0.3的时候,会有和落叶的连线。
#turtle.down()
# 飘落
t1 = turtle.heading()
print("turtle.heading()",t1)# 可以用print看一下t1的值。
an1 = -40 + random.random() * 40 #random.random()产生0,1之间的浮点数
turtle.setheading(an1)
dis = int(800 * random.random() * 0.5 + 400 * random.random() * 0.3 + 200 * random.random() * 0.2)
#上面的800 * random.random() * 0.5是
turtle.forward(dis)
turtle.setheading(t1)
turtle.right(90)
# 画叶子
leaf(turtle.xcor(), turtle.ycor(), node)
turtle.left(90)
# 返回
t2 = turtle.heading()
turtle.setheading(an1)
turtle.backward(dis)