当前位置:首页>python>折腾2天搞定Python向日葵|极简代码+简短讲解

折腾2天搞定Python向日葵|极简代码+简短讲解

  • 2026-10-11 07:05:41
折腾2天搞定Python向日葵|极简代码+简短讲解

零基础自学Python海龟绘图,这朵简约向日葵足足调试了两天。
从花心跑偏、花瓣零散、叶子位置不协调,一点点校准坐标、调换绘图顺序、放大花瓣尺寸,最终成品贴合参考效果图,花瓣饱满、花杆正中穿出花心,整体干净治愈。

完整精简代码

import turtle
# 窗口设置
win = turtle.Screen()
win.title("草坪上的向日葵")
win.setup(600, 500)
win.bgcolor("#87C8E8")
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()

# 画草坪
pen.penup()
pen.goto(-300, -150)
pen.fillcolor("#48A848")
pen.begin_fill()
pen.goto(300, -150)
pen.goto(300, -250)
pen.goto(-300, -250)
pen.goto(-300, -150)
pen.end_fill()

# 花心参数+绘制
center_x, center_y = 0, 0
core_r = 28
pen.penup()
pen.goto(center_x, center_y - core_r)
pen.fillcolor("#8B4513")
pen.begin_fill()
pen.circle(core_r)
pen.end_fill()

# 画花杆(从花心正中心穿出)
pen.penup()
pen.goto(0, -150)
pen.setheading(90)
pen.pensize(3)
pen.color("#2E7D32")
pen.pendown()
pen.forward(150)

# 超大加宽花瓣
pen.color("#FFD600")
pen.pensize(15)
petal_length = 42
for angle in range(0, 360, 30):
   pen.penup()
   pen.goto(center_x, center_y)
   pen.setheading(angle)
   pen.forward(core_r)
   pen.pendown()
   pen.forward(petal_length)

# 画叶子
def leaf(x, y, angle):
   pen.penup()
   pen.goto(x, y)
   pen.setheading(angle)
   pen.fillcolor("#388E3C")
   pen.begin_fill()
   pen.circle(18, 100)
   pen.left(80)
   pen.circle(18, 100)
   pen.end_fill()
leaf(-14, -40, 168)
leaf(14, -50, 12)

turtle.done()

极简讲解(新手一眼看懂)

1. 基础窗口:设置画布大小、蓝天背景、隐藏画笔箭头

2. 绿色草坪:矩形填充,作为画面底部草地

3. 咖啡色花心:固定画布正中心,先画圆再画杆,保证杆子从圆心穿出

4. 绿色茎干:垂直向上,顶端精准对接花心中心点

5. 黄色花瓣:一圈循环12片,线条粗细15、长度42,紧贴花心外圈生长,宽大饱满

6. 两片绿叶:下移固定位置,分布花杆两侧,构图协调

简易修改小技巧

• 花瓣更大:加大pen.pensize(15)、petal_length=42数值

• 花心大小:修改core_r = 28

• 叶子上下动:改leaf里第二个数字,负数越大位置越下

#Python零基础 #编程小练习

最新文章

随机文章