酷代码 AI
菜单

用python编写一个高级的栩栩如生的彼岸花

```python import turtle import math import random def draw_petal(t, radius, angle, color): t.fillcolor(color) t.begin_fill() t.circle(radius, angle) t.left(180 - angle) t.circle(radius, angle) t.left(180 - angle) t.end_fill() def draw_stamen(t, length, color): t.pencolor(color) t.pensize(2) t.forward(length) t.backward(length) def draw_flower(): screen = turtle.Screen() screen.bgcolor("black") screen.setup(800, 800) t = turtle.Turtle() t.speed(0) t.hideturtle() # 绘制花瓣 petal_colors = ["#FF0000", "#FF4500", "#DC143C"] num_petals = 6 petal_radius = 100 petal_angle = 60 for i in range(num_petals): t.penup() t.goto(0, 0) t.setheading(i * (360 / num_petals)) t.pendown() color = random.choice(petal_colors) draw_petal(t, petal_radius, petal_angle, color) # 绘制花蕊 t.penup() t.goto(0, 0) t.setheading(0) t.pendown() t.fillcolor("#8B0000") t.begin_fill() t.circle(15) t.end_fill() # 绘制花蕊细丝 num_stamens = 12 stamen_length = 30 for i in range(num_stamens): t.penup() t.goto(0, 0) t.setheading(i * (360 / num_stamens)) t.pendown() draw_stamen(t, stamen_length, "#8B0000") # 绘制花茎 t.penup() t.goto(0, -petal_radius) t.setheading(270) t.pendown() t.pensize(5) t.pencolor("#228B22") t.forward(200) # 绘制叶子 leaf_length = 80 t.penup() t.goto(0, -150) t.setheading(240) t.pendown() t.fillcolor("#32CD32") t.begin_fill() t.forward(leaf_length) t.left(120) t.forward(leaf_length) t.left(120) t.forward(leaf_length) t.end_fill() screen.exitonclick() if __name__ == "__main__": draw_flower() ``` 这段代码使用Python的turtle模块绘制一朵彼岸花。花瓣采用红色系随机颜色,花蕊为深红色,花茎和叶子为绿色。运行后会显示一个图形窗口,点击可关闭。 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: - turtle坐标系统和角度方向容易混淆,需仔细调试位置和方向 - 填充颜色时确保图形闭合,避免颜色泄漏 - 多次绘制时注意画笔状态,及时抬笔和落笔 [2025-11-22 20:19:30 | AI写代码神器 | 737点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多