pycharm樱花代码
时间: 2023-11-17 12:01:45 浏览: 156
以下是一个使用PyCharm编写的绘制樱花树的Python代码,它包括绘制樱花的落叶花瓣、掉落的花瓣、给樱花树添加树枝、添加绘画背景颜色的绘制和选取各种样式的颜色等功能。你可以在PyCharm中打开并运行它。
```python
import turtle
import random
# 绘制樱花的落叶花瓣
def petal(t, petal_radius, petal_length):
angle = 360 / petal_number
for i in range(petal_number):
t.circle(petal_radius, angle)
t.left(180 - angle)
t.circle(petal_radius, angle)
t.right(180 + angle)
t.right(90)
t.forward(petal_length)
# 绘制掉落花瓣功能
def petal_fall(t, petal_radius, petal_length):
t.penup()
t.setpos(random.randint(-200, 200), 200)
t.pendown()
petal(t, petal_radius, petal_length)
# 给樱花树添加树枝
def branch(t, branch_length):
if branch_length > 10:
angle = random.randint(20, 40)
scale = random.uniform(0.6, 0.8)
t.pensize(branch_length / 10)
t.forward(branch_length)
t.right(angle)
branch(t, branch_length * scale)
t.left(angle * 2)
branch(t, branch_length * scale)
t.right(angle)
t.backward(branch_length)
# 给樱花树添加绘画背景颜色的绘制
def draw_sky(t):
t.penup()
t.setpos(-300, 300)
t.pendown()
t.begin_fill()
t.fillcolor("#87CEEB")
for i in range(4):
t.forward(600)
t.right(90)
t.end_fill()
# 选取各种样式的颜色
def random_color():
r = random.randint(128, 255)
g = random.randint(0, 128)
b = random.randint(0, 128)
return (r, g, b)
# 主函数
if __name__ == '__main__':
petal_radius = 30
petal_length = 60
petal_number = 8
branch_length = 100
turtle.speed(0)
turtle.hideturtle()
draw_sky(turtle)
turtle.penup()
turtle.setpos(0, -200)
turtle.pendown()
turtle.left(90)
turtle.color("brown")
branch(turtle, branch_length)
for i in range(20):
petal_fall(turtle, petal_radius, petal_length)
turtle.done()
```
阅读全文