python代码画樱花
时间: 2023-07-03 08:14:13 浏览: 80
好的,下面是一份简单的 Python 代码,可以画出樱花:
```python
import turtle
import random
def tree(branch_len, t):
if branch_len > 3:
if 8 <= branch_len <= 12:
if random.randint(0, 2) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branch_len / 3)
elif branch_len < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branch_len / 2)
else:
t.color('sienna')
t.pensize(branch_len / 10)
t.forward(branch_len)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
tree(branch_len - 10 * b, t)
t.left(40 * a)
tree(branch_len - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch_len)
t.down()
def petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.goto(b, a)
t.down()
t.color('lightcoral')
t.begin_fill()
c = 2 * random.random() + 2
d = 1.5 * random.random() + 1.5
for j in range(12):
t.forward(d)
t.left(c)
t.forward(d)
t.right(2 * c)
t.end_fill()
def main():
t = turtle.Turtle()
myWin = turtle.Screen()
t.getscreen().tracer(5, 0)
myWin.bgcolor('white')
t.left(90)
t.up()
t.backward(200)
t.down()
t.color('sienna')
tree(60, t)
petal(200, t)
myWin.exitonclick()
main()
```
运行以上代码,即可在 Turtle 绘图窗口中看到樱花的画作。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)