有趣python小程序系列之一
w^s^w 时间:2022-10-01
前言
关于学python的初衷,如图,然后清华大佬给我推荐了python,从此开始了三天打鱼两天晒网式的学习之路~
之前学python时在网上找了好多小程序,由于年代久远,已经忘记出自哪里了,给代码加了点注释,再稍微修改了一下,让代码的可读性更好,如有侵权立刻就删~
一、飘落的银杏
引用本科老师的一句话:“樱花最美的时候有两个,一个是樱花一半在空中,一半在树上的时候,空间上的最美;另一个是你们这个年纪可以牵着喜欢的人一起去看的时候,意境上的最美。”
所以,我们接下来就要用python的turtle库来完成一半空中,一半树上的银杏的绘画~
二、代码部分
1.导入所需的库
import turtleimport randomfrom math import *
- 1
- 2
- 3
2.生成斐波那契数列
斐波那契数列是指前两项的和加起来等于后一项的一个数列,例如[0 1 1 2 4 6 10],这里使用了两个函数来生成斐波契那数列。
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
调用函数生成一个数列如下:
yu = Fibonacci_Recursion(top) #生成斐波契那数列print(yu)
- 1
- 2
运行结果如下:
3.定义生成叶子的方法
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
4.定义生成树的方法
这里用x生成随机数,用if条件进行判断来决定要不要继续画分支,要不要画叶子,使树更加自然,无规律性,更好看一点,这样会导致你每次运行时,画出来的树都是不一样的。具体的细节,我已经加上了注释。如果想调整空中叶子的比例,树的分叉程度,修改if判断语句中的x取值范围,以增加概率或减小概率即可。至于如何达到你心中完美的效果就要慢慢去尝试了。
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 > 6: #此时画飘落的叶子,x范围太大会导致树太秃 turtle.forward(length) # 画树枝 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: turtle.penup() # 飘落 t1 = turtle.heading() an1 = -40 + random.random() * 40 turtle.setheading(an1) dis = int(800 * random.random() * 0.5 + 400 * random.random() * 0.3 + 200 * random.random() * 0.2) 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) turtle.setheading(t2) elif level==top and x < 7 : #此时画枝叶,x范围太大会导致飘落的叶子太少 turtle.penup() turtle.forward(length) elif level>3 and (x>6) :#三级树枝以上,有40%的概率执行以下策略 turtle.pendown() turtle.forward(length) c = random.randint(4, 6) for i in range(3, c): leaf(turtle.xcor(), turtle.ycor(),node) leaf(turtle.xcor(), turtle.ycor(),node) button=1# jump""" else: turtle.forward(length) # 画树枝 yu[level] = yu[level] -1 if node > 0 and button == 0: # 计算右侧分支偏转角度,在固定角度偏转增加一个随机的偏移量 right = random.random() * 5 + 17 # 计算左侧分支偏转角度,在固定角度偏转增加一个随机的偏移量 left = random.random() * 20 + 19 # 计算下一级分支的长度 child_length = length * (random.random() * 0.25 + 0.7) # 右转一定角度,画右分支 r=random.randint(0, 1) if r==1: turtle.right(right) level = level + 1 #print("level", level) else: turtle.left(right) level = level + 1 #print("level", level) draw(node - 1, child_length,level,yu,button) yu[level] = yu[level] +1 if yu[level] > 1: # 左转一定角度,画左分支 if r==1: turtle.left(right + left) draw(node - 1, child_length, level, yu,button) # 将偏转的角度,转回 turtle.right(left) yu[level] = yu[level] - 1 else: turtle.right(right + left) draw(node - 1, child_length, level, yu,button) # 将偏转的角度,转回 turtle.left(left) yu[level] = yu[level] - 1 else: if r==1: turtle.left(right + left特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。