当前位置:首页>python>零基础Python绘图|用基础指令画出好看牵牛花

零基础Python绘图|用基础指令画出好看牵牛花

  • 2026-10-11 10:51:49
零基础Python绘图|用基础指令画出好看牵牛花

这段牵牛花代码没有复杂难懂的递归,全部靠之前学过的基础知识点组合而成,新手也能看懂,调试好不容易出现空白、断线问题。

一、代码涉及核心基础知识点

1. 画布色彩模式
screen.colormode(255),开启RGB调色,数字0-255自由调配花朵、叶子的深浅色彩。

2. 画笔基础设置
pen = turtle.Turtle() 创建画笔;speed(0) 最快绘图;hideturtle() 隐藏箭头,画面干净。

3. 抬笔与落笔
penup() 移动画笔不画线,用来定位花朵中心;pendown() 落笔绘制花杆、叶片,避免线条断裂。
goto(x,y) 精准控制画笔坐标,固定花心、叶片位置。

4. 颜色填充(花朵、叶子必备)
fillcolor() 填充色、pencolor() 线条色;
begin_fill() 和 end_fill() 必须成对写,图形才能上色。

5. 圆弧circle()
circle(半径,弧度) 绘制花瓣、叶片、黄色花心,牵牛花波浪喇叭边缘全靠圆弧实现。

6. 角度转向
left() / right() 旋转画笔方向,对称画出两片叶子;setheading(270) 直接设定画笔朝下画花杆。

7. 自定义函数 def
把画叶子、画整朵花的代码分开打包,代码条理清晰,重复调用更省事。

8. 循环for
for _ in range(5) 循环5次,画出五瓣喇叭花边缘,不用重复写代码。

9. 收尾固定语句
turtle.done() 放在最后,防止绘图窗口一闪消失,不会误以为画面空白。

二、完整可直接复制牵牛花代码

import turtle
screen = turtle.Screen()
screen.colormode(255)
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()

# 牵牛花标准心形叶子
def draw_leaf():
   pen.fillcolor(42, 168, 55)
   pen.pencolor(25, 110, 35)
   pen.begin_fill()
   pen.circle(14, 160)
   pen.left(110)
   pen.circle(14, 160)
   pen.end_fill()

# 真正的喇叭花冠(大开口漏斗,牵牛花核心)
def draw_morning_glory():
   # 花心位置
   cx, cy = 0, 40
   pen.penup()
   pen.goto(cx, cy)
   pen.fillcolor(135, 70, 230)
   pen.pencolor(135, 70, 230)
   pen.begin_fill()
   # 画喇叭五片波浪边
   for _ in range(5):
       pen.circle(50, 140)
       pen.left(80)
   pen.end_fill()

   # 黄色小圆花心
   pen.goto(cx, cy)
   pen.fillcolor(255, 225, 0)
   pen.begin_fill()
   pen.circle(10)
   pen.end_fill()

   # 长长的喇叭花筒(向下细管)
   pen.pensize(3)
   pen.pencolor(22, 105, 30)
   pen.setheading(270)
   pen.goto(cx, cy)
   pen.pendown()
   pen.forward(85)
   leaf_point = pen.pos()

   # 两片叶子
   pen.goto(leaf_point)
   pen.left(45)
   draw_leaf()
   pen.right(90)
   draw_leaf()

# 执行绘制
draw_morning_glory()
turtle.done()

三、运行效果说明

画布中间一朵紫色喇叭牵牛花,黄色小圆花心,下方绿色花杆搭配两片对称绿叶,造型完整流畅,线条连贯不会断杆。
全程只用基础绘图知识点,没有复杂代码,新手复制就能运行。

学习感悟

所有好看的花草图案,都是抬笔落笔、圆弧、填充、循环这些基础知识点拼接出来的。
不用害怕代码难,一点点拆解练习,多动手敲代码,慢慢就能独立画出各种花卉。

#Python零基础 #turtle绘图教程 #自学编程日常

最新文章

随机文章