帮我用Python画个玫瑰花
时间: 2023-05-20 11:03:01 浏览: 112
python画一朵玫瑰花程序.zip
当然可以,以下是Python代码:
```python
import turtle
def draw_petal(t, r, angle):
"""绘制花瓣"""
for i in range(2):
t.circle(r, angle)
t.left(180-angle)
def draw_flower(t, n, r, angle):
"""绘制花朵"""
for i in range(n):
draw_petal(t, r, angle)
t.left(360/n)
def draw_stem(t, height):
"""绘制花茎"""
t.right(90)
t.forward(height)
def draw_leaf(t, height, width):
"""绘制叶子"""
t.right(135)
t.forward(height)
t.right(135)
t.forward(width)
t.right(135)
t.forward(height)
def main():
# 初始化画布
t = turtle.Turtle()
t.speed(0)
t.hideturtle()
t.penup()
t.goto(0, -200)
t.pendown()
# 绘制花朵
draw_flower(t, 10, 100, 20)
# 绘制花茎
draw_stem(t, 200)
# 绘制叶子
t.right(90)
draw_leaf(t, 50, 30)
t.left(90)
draw_leaf(t, 50, 30)
# 关闭画布
turtle.done()
if __name__ == '__main__':
main()
```
运行以上代码,即可在画布上绘制出一朵玫瑰花。
阅读全文