为了让初学者更加容易接受编程,我们这里先从海龟画图开始讲解。这样,大家在不接触其他编程概念时,就能开始做出一些简单的效果。提高兴趣,寓教于乐。
下面是用海龟模块绘制出来的两张经典图形:
是不是非常生动形象,我们无法想象这是一行行冰冷的代码能绘制出来图形
我们步入正题:海龟英文名字是Turtle,所以我们在集成开发软件上使用海龟绘图时,需要导入一行代码:
import turtle # 导入模块
Turtle 是 Python 内置的一个比较有趣味的模块,俗称 海龟作图,它是基于 tkinter 模块打造,提供一些简单的绘图工具,海龟作图最初源自 20 世纪 60 年代的 Logo 编程语言,之后一些很酷的 Python 程序员构建了 turtle 库,让其他程序员只需要 import turtle,就可以在 Python 中使用海龟作图。
下面是用turtle模块写的两个最基本的几何图形:圆和三角形
代码实现如下,我的每个代码都会加上注释以方便大家理解
import turtle #导入turtle模块
turtle.showturtle() #显示箭头
turtle.write("csdn") #写字符串
turtle.forward(300) #前进300像素
turtle.color("red") #画笔颜色改为red
turtle.left(90) #箭头左转90度
turtle.forward(300)
turtle.goto(0,50) #去坐标(0,50)
turtle.goto(0,0)
turtle.penup() #抬笔,路径就不会画出来
turtle.goto(0,300)
turtle.pendown() #下笔,路径就会画出
turtle.circle(100) #画圆
turtle.done() #程序结束,保持窗口存在
当然仅仅打出这两个肯定是不够的,要想在深入理解一些,我们不妨打印出来一个奥运五环吧:
下面先是图形的实现:
代码的实现:
import turtle
turtle.width(5) # 这里指的是画笔的线条的粗细
turtle.color("red")
turtle.circle(50)
# 第二个圆
turtle.penup()
turtle.goto(80,0)
turtle.pendown()
turtle.color("blue")
turtle.circle(50)
# 第三个圆
turtle.penup()
turtle.goto(160,0)
turtle.pendown()
turtle.color("yellow")
turtle.circle(50)
# 第四个圆
turtle.penup()
turtle.goto(35,-65)
turtle.goto(35,-65)
turtle.pendown()
turtle.color("black")
turtle.circle(50)
# 第五个圆
turtle.penup()
turtle.goto(120,-65)
turtle.pendown()
turtle.color("green")
turtle.circle(50)
turtle.done() #程序结束保持窗口
最后送大家一个派大星的海龟代码:
import turtle
t = turtle.Turtle()
t.penup()
t.goto(-200, 297)
t.speed(400)
t.pensize(3)
t.pencolor("black")
# 背景框
t.begin_fill()
t.fillcolor("lightpink")
t.penup()
t.goto(-200, -83)
t.seth(0)
t.pendown()
t.forward(380)
t.seth(90)
t.forward(380)
t.seth(180)
t.forward(380)
t.seth(270)
t.forward(380)
t.end_fill()
t.penup()
# turtle.screensize(800,600,"lightpink")
# t.pendown()
# 头部
# t.pencolor("black")
# t.pensize(3)
t.penup()
t.goto(-65, 170)
t.seth(70)
t.pendown()
t.forward(70)
t.seth(10)
t.circle(-120, 35)
t.seth(-90)
t.forward(100)
# 右腮帮
t.penup()
t.goto(65, 100)
t.pendown()
t.circle(-25, 360)
# 右手
t.penup()
t.goto(52, 75)
t.seth(-60)
t.pendown()
t.circle(-300, 25)
t.seth(130)
t.forward(110)
# 身体
t.penup()
t.goto(63, -20)
t.seth(200)
t.pendown()
t.circle(-300, 34)
t.seth(85)
t.circle(-300, 20)
# 左腮帮
t.penup()
t.goto(-100, 110)
t.pendown()
t.circle(-26, 360)
# 左手
t.penup()
t.goto(-88, 77)
t.seth(-120)
t.pendown()
t.circle(350, 24)
t.seth(60)
t.circle(350, 8)
# 眉毛
t.penup()
t.goto(-60, 200)
t.seth(15)
t.pensize(8)
t.pencolor("black")
t.pendown()
t.forward(14)
t.penup()
t.goto(0, 200)
t.seth(-25)
t.pendown()
t.forward(15)
# 嘴巴
t.penup()
t.goto(-40, 85)
t.seth(-15)
t.pensize(2)
t.pencolor("black")
t.pendown()
t.circle(15, 80)
t.seth(25)
t.circle(-15, 55)
t.penup()
t.goto(-35, 78)
t.pendown()
t.circle(15, 80)
# 肚脐
t.penup()
t.goto(-50, -18)
t.seth(35)
t.pendown()
t.circle(-30, 85)
t.penup()
t.goto(-30, -23)
t.pensize(6)
t.pendown()
t.forward(1)
# 帽子
t.penup()
t.goto(-42, 235)
t.seth(70)
t.pensize(2)
t.pendown()
t.begin_fill()
t.fillcolor("#000080")
t.forward(15)
t.seth(25)
t.circle(-75, 53)
t.seth(-85)
t.forward(20)
t.seth(160)
t.circle(125, 37)
t.penup()
t.goto(-39, 250)
t.seth(145)
t.pendown()
t.forward(5)
t.circle(-25, 180)
t.forward(25)
t.seth(-5)
t.forward(20)
t.circle(-20, 180)
t.end_fill()
# 红飘带
t.penup()
t.goto(45, 260)
t.seth(-75)
t.begin_fill()
t.fillcolor("red")
t.pendown()
t.forward(40)
t.seth(145)
t.forward(10)
t.seth(-135)
t.forward(10)
t.seth(85)
t.forward(35)
t.end_fill()
# 裤子
t.penup()
t.goto(-110, -33)
t.seth(-88)
t.begin_fill()
t.fillcolor("#00EE00")
t.pendown()
t.forward(50)
t.seth(0)
t.forward(80)
t.seth(65)
t.forward(15)
t.seth(-65)
t.forward(15)
t.seth(0)
t.forward(80)
t.seth(90)
t.forward(60)
t.goto(63, -20)
t.seth(200)
t.circle(-300, 34)
t.end_fill()
t.penup()
# 裤子上的紫色花纹
t.goto(-109, -60)
t.seth(-45)
t.begin_fill()
t.fillcolor("purple")
t.pendown()
t.circle(25, 160)
t.seth(175)
t.forward(41)
t.end_fill()
t.penup()
t.goto(-12, -82)
t.seth(90)
t.begin_fill()
t.fillcolor("purple")
t.pendown()
t.circle(-25, 180)
t.end_fill()
# 左眼睛
t.penup()
t.goto(-50, 100)
t.pendown()
t.begin_fill()
t.fillcolor("white")
t.setheading(0)
for i in range(2): # 2*10*9*2=360
for j in range(10):
t.forward(j)
t.left(9)
for j in range(10, 0, -1):
t.forward(j)
t.left(9)
t.end_fill()
# 右眼睛
t.penup()
t.goto(-5, 100)
t.begin_fill()
t.fillcolor("white")
t.pendown()
t.setheading(0)
for i in range(2): # 2*5*18*2=360
for j in range(10):
t.forward(j)
t.left(9)
for j in range(10, 0, -1):
t.forward(j)
t.left(9)
t.end_fill()
t.penup()
# 黑眼球
t.goto(-45, 130)
t.begin_fill()
t.fillcolor("black")
t.pendown()
t.setheading(0)
for i in range(2): # 2*10*9*2=360
for j in range(5):
t.forward(j)
t.left(18)
for j in range(5, 0, -1):
t.forward(j)
t.left(18)
t.end_fill()
t.penup()
turtle.done()
这是图形实现的代码:
以上这就是python海龟绘图我想要给初学者带来的内容了,主要是对turtle模块的熟悉与掌握,至于你像绘制出来什么样的图形,那就需要你多加练习与思考了!