def draw_flower(x, y, size): """在 (x, y) 处画一朵5瓣的花朵""" colors = ["#FFB7C5", "#FF9AAF", "#FFC0CB", "#FFD1DC", "#FFA6C9"] color = random.choice(colors) # 随机抽出一种颜色作为花瓣颜色 for i in range(5): # 5个花瓣 t.penup() t.goto(x, y) t.setheading(i * 72) # 每瓣间隔72度(360/5) t.pendown() t.color(color) t.begin_fill() t.circle(size, 60) # 画弧线 t.left(120) t.circle(size, 60) # 再画一段弧线 t.end_fill() #两条弧线组成花瓣 # 金色花心 t.penup() t.goto(x, y - size * 0.3) t.pendown() t.color("#FFD700") t.begin_fill() t.circle(size * 0.25) t.end_fill()
"""在每个树枝末端附近画几朵花,使用循环实现"""for tip in branch_tips: count = random.randint(2, 4) # 每个末端2~4朵 for _ in range(count): px = tip[0] + random.uniform(-20, 20) py = tip[1] + random.uniform(-10, 25) draw_flower(px, py, random.uniform(6, 12))