引路者👇
引言
嘿,朋友们!今天我要和大家聊聊 Python 中的 turtle 模块。如果你对编程感兴趣,但又觉得它太枯燥,那么 turtle 模块绝对会让你眼前一亮!它不仅简单易学,还能让你在编程中感受到艺术的乐趣。
一、turtle 是什么?
turtle 是 Python 的一个内置绘图模块,它提供了一个绘图板和一个小海龟(也就是光标)。你可以通过编写代码来控制小海龟的移动,从而绘制出各种图形。想象一下,你在一个沙滩上,手里拿着一根棍子,通过移动棍子来画出各种图案,turtle 就是这样的一个工具。
二、turtle的常见使用以及示例
首先,要使用turtle模块肯定就得先导入相关模块
import turtle as t
from turtle import *
import turtle
此三种导入方式均可使用
2.1、基础绘图方法
-
forward(distance)
或fd(distance)
-
功能:向前移动指定的距离。
-
参数:
distance
(数值类型),表示移动的距离。 -
示例:
turtle.forward(100) # 向前移动100个单位
-
-
backward(distance)
或bk(distance)
-
功能:向后移动指定的距离。
-
参数:
distance
(数值类型),表示移动的距离。 -
示例:
turtle.backward(50) # 向后移动50个单位
-
-
right(angle)
-
功能:向右转指定的角度。
-
参数:
angle
(数值类型),表示旋转的角度。 -
示例:
turtle.right(90) # 向右转90度
-
-
left(angle)
-
功能:向左转指定的角度。
-
参数:
angle
(数值类型),表示旋转的角度。 -
示例
turtle.left(45) # 向左转45度
-
-
circle(radius, extent=None, steps=None)
-
功能:绘制一个圆或圆弧。
-
参数:
-
radius
(数值类型),表示圆的半径。 -
extent
(数值类型,可选),表示圆弧的角度(默认为360度)。 -
steps
(数值类型,可选),表示圆弧的步数(用于绘制多边形)。
-
-
示例:
turtle.circle(50) # 绘制一个半径为50的完整圆 turtle.circle(50, 180) # 绘制一个半径为50的半圆 turtle.circle(50, steps=6) # 绘制一个半径为50的六边形
-
2.2、画笔控制方法
-
penup()
或pu()
-
功能:提起画笔,移动时不绘制线条。
-
示例:
turtle.penup() # 提起画笔
-
-
pendown()
或pd()
-
功能:放下画笔,移动时绘制线条。
-
示例:
turtle.pendown() # 放下画笔
-
-
pensize(width)
-
功能:设置画笔的宽度。
-
参数:
width
(数值类型),表示画笔的宽度。 -
示例:
turtle.pensize(3) # 设置画笔宽度为3
-
-
pencolor(color)
-
功能:设置画笔的颜色。
-
参数:
color
(字符串或颜色元组),表示画笔的颜色。 -
示例:
turtle.pencolor("red") # 设置画笔颜色为红色 turtle.pencolor((255, 0, 0)) # 设置画笔颜色为红色(RGB值)
-
-
fillcolor(color)
-
功能:设置填充颜色。
-
参数:
color
(字符串或颜色元组),表示填充颜色。 -
示例:
turtle.fillcolor("blue") # 设置填充颜色为蓝色 turtle.fillcolor((0, 0, 255)) # 设置填充颜色为蓝色(RGB值)
-
-
begin_fill()
-
功能:开始填充图形。
-
示例:
turtle.begin_fill() # 开始填充
-
-
end_fill()
-
功能:结束填充图形。
-
示例:
turtle.end_fill() # 结束填充
-
2.3、位置和方向控制方法
-
goto(x, y)
-
功能:移动到指定的坐标位置。
-
参数:
x
和y
(数值类型),表示目标坐标。 -
示例:
turtle.goto(100, 100) # 移动到坐标(100, 100)
-
-
setheading(angle)
或seth(angle)
-
功能:设置当前的方向。
-
参数:
angle
(数值类型),表示方向的角度。 -
示例:
turtle.setheading(90) # 设置方向为向上
-
-
home()
-
功能:将小海龟移动到原点(0, 0),并面向右方。
-
示例:
turtle.home() # 返回原点
-
-
position()
或pos()
-
功能:获取当前的位置。
-
返回值:一个包含当前坐标
(x, y)
的元组。 -
示例:
current_position = turtle.position() # 获取当前坐标 print(current_position) # 输出当前坐标
-
-
heading()
-
功能:获取当前的方向。
-
返回值:当前的方向角度。
-
示例:
current_heading = turtle.heading() # 获取当前方向 print(current_heading) # 输出当前方向
-
2.4、窗口和屏幕控制方法
-
setup(width, height)
-
功能:设置绘图窗口的大小。
-
参数:
width
和height
(数值类型),表示窗口的宽度和高度。 -
示例:
turtle.setup(800, 600) # 设置窗口大小为800x600
-
-
screensize(canvwidth=None, canvheight=None, bg=None)
-
功能:设置绘图区域的大小和背景颜色。
-
参数:
-
canvwidth
和canvheight
(数值类型,可选),表示绘图区域的宽度和高度。 -
bg
(字符串,可选),表示背景颜色。
-
-
示例:
turtle.screensize(800, 600, "white") # 设置绘图区域大小和背景颜色
-
-
title(title)
-
功能:设置窗口的标题。
-
参数:
title
(字符串),表示窗口的标题。 -
示例:
turtle.title("Turtle Drawing") # 设置窗口标题
-
-
bgcolor(color)
-
功能:设置背景颜色。
-
参数:
color
(字符串或颜色元组),表示背景颜色。 -
示例:
turtle.bgcolor("lightblue") # 设置背景颜色为浅蓝色
-
2.5、其他常用方法
-
clear()
-
功能:清除绘图区域的内容,但不移动小海龟。
-
示例:
turtle.clear() # 清除绘图区域
-
-
reset()
-
功能:清除绘图区域的内容,并将小海龟重置到初始状态。
-
示例:
turtle.reset() # 重置绘图区域和小海龟
-
-
speed(speed)
-
功能:设置小海龟的移动速度。
-
参数:
speed
(数值类型),表示速度(范围为0到10)。 -
示例:
turtle.speed(5) # 设置速度为5
-
-
hideturtle()
或ht()
-
功能:隐藏小海龟。
-
示例:
turtle.hideturtle() # 隐藏小海龟
-
三、手搓卡通人物示例
接下来就是博主手搓的一小部分卡通人物(有些小瑕疵🤭)
3.1 哆啦A梦
代码如下:
import turtle
def flyTo(x, y): #开启无轨迹跳跃
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
def drawEye(): #画眼睛
turtle.tracer(False)
a = 2.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
else:
a += 0.05
turtle.left(3)
turtle.fd(a)
turtle.tracer(True)
def beard(): #画胡子
# 左边第一根胡子
flyTo(-37, 135)
turtle.seth(165)
turtle.fd(60)
# 左边第二根胡子
flyTo(-37, 125)
turtle.seth(180)
turtle.fd(60)
# 左边第三根胡子
flyTo(-37, 115)
turtle.seth(193)
turtle.fd(60)
# 右边第一根胡子
flyTo(37, 135)
turtle.seth(15)
turtle.fd(60)
# 右边第二根胡子
flyTo(37, 125)
turtle.seth(0)
turtle.fd(60)
# 右边第三根胡子
flyTo(37, 115)
turtle.seth(-13)
turtle.fd(60)
def drawRedScarf():
turtle.fillcolor("red") # 填充颜色
turtle.begin_fill()
turtle.seth(0) # 调整转向向右
turtle.fd(200)
turtle.circle(-5, 90)
turtle.fd(10)
turtle.circle(-5, 90)
turtle.fd(207)
turtle.circle(-5, 90)
turtle.fd(10)
turtle.circle(-5, 90)
turtle.end_fill()
def drawMouse():
flyTo(5, 148)
turtle.seth(270)
turtle.fd(100)
turtle.seth(0)
turtle.circle(120, 50)
turtle.seth(230)
turtle.circle(-120, 100)
def drawRedNose():
flyTo(-10, 158)
turtle.fillcolor("red") # 填充颜色
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
def drawBlackdrawEye():
turtle.seth(0)
flyTo(-20, 195)
turtle.fillcolor("#000000") # 填充颜色
turtle.begin_fill()
turtle.circle(13)
turtle.end_fill()
turtle.pensize(6)
flyTo(20, 205)
turtle.seth(75)
turtle.circle(-10, 150)
turtle.pensize(3)
flyTo(-17, 200)
turtle.seth(0)
turtle.fillcolor("#ffffff")
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
flyTo(0, 0)
def drawFace():
turtle.forward(183) # 前行183个单位
turtle.fillcolor("white") # 填充颜色为白色
turtle.begin_fill() # 开始填充
turtle.left(45) # 左转45度
turtle.circle(120, 100) # 右边那半边脸
turtle.seth(90) # 调整转向向上
drawEye() # 画右眼睛
turtle.seth(180) # 调整转向向左
turtle.penup() # 抬笔
turtle.fd(60) # 前行60
turtle.pendown() # 落笔
turtle.seth(90) # 调整转向向上
drawEye() # 画左眼睛
turtle.penup() # 抬笔
turtle.seth(180) # 调整转向向左
turtle.fd(64) # 前进64
turtle.pendown() # 落笔
turtle.seth(215) # 修改朝向
turtle.circle(120, 100) # 左边那半边脸
turtle.end_fill() #
def drawHead():
turtle.penup() # 抬笔
turtle.circle(150, 40) # 画圆, 半径150,圆周角40
turtle.pendown() # 落笔
turtle.fillcolor("#00a0de") # 填充色
turtle.begin_fill() # 开始填充
turtle.circle(150, 280) # 画圆,半径150, 圆周角280
turtle.end_fill()
def drawAll():
drawHead()
drawRedScarf()
drawFace()
drawRedNose()
drawMouse()
beard()
flyTo(0, 0)
turtle.seth(0)
turtle.penup()
turtle.circle(150, 50)
turtle.pendown()
turtle.seth(30)
turtle.fd(40)
turtle.seth(70)
turtle.circle(-30, 270)
turtle.fillcolor("#00a0de")
turtle.begin_fill()
turtle.seth(230)
turtle.fd(80)
turtle.seth(90)
turtle.circle(1000, 1)
turtle.seth(-89)
turtle.circle(-1000, 10)
turtle.seth(180)
turtle.fd(70)
turtle.seth(90)
turtle.circle(30, 180)
turtle.seth(180)
turtle.fd(70)
turtle.seth(100)
turtle.circle(-1000, 9)
turtle.seth(-86)
turtle.circle(1000, 2)
turtle.seth(230)
turtle.fd(40)
turtle.circle(-30, 230)
turtle.seth(45)
turtle.fd(81)
turtle.seth(0)
turtle.fd(203)
turtle.circle(5, 90)
turtle.fd(10)
turtle.circle(5, 90)
turtle.fd(7)
turtle.seth(40)
turtle.circle(150, 10)
turtle.seth(30)
turtle.fd(40)
turtle.end_fill()
# 左手
turtle.seth(70)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.circle(-30)
turtle.end_fill()
# 脚
flyTo(103.74, -182.59)
turtle.seth(0)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.fd(15)
turtle.circle(-15, 180)
turtle.fd(90)
turtle.circle(-15, 180)
turtle.fd(10)
turtle.end_fill()
flyTo(-96.26, -182.59)
turtle.seth(180)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.fd(15)
turtle.circle(15, 180)
turtle.fd(90)
turtle.circle(15, 180)
turtle.fd(10)
turtle.end_fill()
# 右手
flyTo(-133.97, -91.81)
turtle.seth(50)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
# 口袋
flyTo(-103.42, 15.09)
turtle.seth(0)
turtle.fd(38)
turtle.seth(230)
turtle.begin_fill()
turtle.circle(90, 260)
turtle.end_fill()
flyTo(5, -40)
turtle.seth(0)
turtle.fd(70)
turtle.seth(-90)
turtle.circle(-70, 180)
turtle.seth(0)
turtle.fd(70)
# 铃铛
flyTo(-103.42, 15.09)
turtle.fd(90)
turtle.seth(70)
turtle.fillcolor("#ffd200")
turtle.begin_fill()
turtle.circle(-20)
turtle.end_fill()
turtle.seth(170)
turtle.fillcolor("#ffd200")
turtle.begin_fill()
turtle.circle(-2, 180)
turtle.seth(10)
turtle.circle(-100, 22)
turtle.circle(-2, 180)
turtle.seth(180 - 10)
turtle.circle(100, 22)
turtle.end_fill()
flyTo(-13.42, 15.09)
turtle.seth(250)
turtle.circle(20, 110)
turtle.seth(90)
turtle.fd(15)
turtle.dot(10)
flyTo(0, -150)
drawBlackdrawEye()
def main():
turtle.screensize(800, 6000, "#F0F0F0")
turtle.pensize(3)
turtle.speed(9)
drawAll()
turtle.penup() #抬笔
turtle.goto(100, -300)
#turtle.color("violet") #粉红色有点那个
turtle.write('by fly', font=("Bradley Hand ITC", 30, "bold"))
if __name__ == "__main__":
main()
turtle.mainloop()
3.2 小猪佩奇
代码如下:
# coding:utf-8
import time
import turtle as t
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255,155,192),"pink") # 设置画笔颜色和填充颜色(pink)
t.setup(840,500) # 设置主窗口的大小为840*500
t.speed(10) # 设置画笔速度为10
#鼻子
t.pu() # 提笔
t.goto(-100,100) # 画笔前往F坐标(-100,100)
t.pd() # 下笔
t.seth(-30) # 笔的角度为-30°
t.begin_fill() # 外形填充的开始标志
a=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
t.lt(3) #向左转3度
t.fd(a) #向前走a的步长
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill() # 依据轮廓填充
t.pu() # 提笔
t.seth(90) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10)
t.pd()
t.pencolor(255,155,192) # 设置画笔颜色
t.seth(10)
t.begin_fill()
t.circle(5) # 画一个半径为5的圆
t.color(160,82,45) # 设置画笔和填充颜色
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255,155,192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160,82,45)
t.end_fill()
#头
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300,-30) # 顺时针画一个半径为300,圆心角为30°的园
t.circle(100,-60)
t.circle(80,-100)
t.circle(150,-20)
t.circle(60,-95)
t.seth(161)
t.circle(-300,15)
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
a=0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
a=a+0.08
t.lt(3) #向左转3度
t.fd(a) #向前走a的步长
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill()
#耳朵
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,56)
t.end_fill()
#眼睛
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
#腮
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
#嘴
t.color(239,69,19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30,40)
t.circle(40,80)
#身体
t.color("red",(255,99,71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100,10)
t.circle(300,30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300,30)
t.circle(100,3)
t.color((255,155,192),(255,100,100))
t.seth(-135)
t.circle(-80,63)
t.circle(-150,24)
t.end_fill()
#手
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300,15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20,90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300,15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20,90)
#脚
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
#尾巴
t.pensize(4)
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70,20)
t.circle(10,330)
t.circle(70,30)
3.3 皮卡丘
import turtle
def getPosition(x, y):
turtle.setx(x)
turtle.sety(y)
print(x, y)
class Pikachu:
def __init__(self):
self.t = turtle.Turtle()
t = self.t
t.pensize(3)
t.speed(9)
t.ondrag(getPosition)
def noTrace_goto(self, x, y):
self.t.penup()
self.t.goto(x, y)
self.t.pendown()
def leftEye(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.seth(0)
t.fillcolor('#333333')
t.begin_fill()
t.circle(22)
t.end_fill()
self.noTrace_goto(x, y + 10)
t.fillcolor('#000000')
t.begin_fill()
t.circle(10)
t.end_fill()
self.noTrace_goto(x + 6, y + 22)
t.fillcolor('#ffffff')
t.begin_fill()
t.circle(10)
t.end_fill()
def rightEye(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.seth(0)
t.fillcolor('#333333')
t.begin_fill()
t.circle(22)
t.end_fill()
self.noTrace_goto(x, y + 10)
t.fillcolor('#000000')
t.begin_fill()
t.circle(10)
t.end_fill()
self.noTrace_goto(x - 6, y + 22)
t.fillcolor('#ffffff')
t.begin_fill()
t.circle(10)
t.end_fill()
def mouth(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.fillcolor('#88141D')
t.begin_fill()
# 下嘴唇
l1 = []
l2 = []
t.seth(190)
a = 0.7
for i in range(28):
a += 0.1
t.right(3)
t.fd(a)
l1.append(t.position())
self.noTrace_goto(x, y)
t.seth(10)
a = 0.7
for i in range(28):
a += 0.1
t.left(3)
t.fd(a)
l2.append(t.position())
# 上嘴唇
t.seth(10)
t.circle(50, 15)
t.left(180)
t.circle(-50, 15)
t.circle(-50, 40)
t.seth(233)
t.circle(-50, 55)
t.left(180)
t.circle(50, 12.1)
t.end_fill()
# 舌头
self.noTrace_goto(17, 54)
t.fillcolor('#DD716F')
t.begin_fill()
t.seth(145)
t.circle(40, 86)
t.penup()
for pos in reversed(l1[:20]):
t.goto(pos[0], pos[1] + 1.5)
for pos in l2[:20]:
t.goto(pos[0], pos[1] + 1.5)
t.pendown()
t.end_fill()
# 鼻子
self.noTrace_goto(-17, 94)
t.seth(8)
t.fd(4)
t.back(8)
# 红脸颊
def leftCheek(self, x, y):
turtle.tracer(False)
t = self.t
self.noTrace_goto(x, y)
t.seth(300)
t.fillcolor('#DD4D28')
t.begin_fill()
a = 2.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
t.lt(3)
t.fd(a)
else:
a += 0.05
t.lt(3)
t.fd(a)
t.end_fill()
turtle.tracer(True)
def rightCheek(self, x, y):
t = self.t
turtle.tracer(False)
self.noTrace_goto(x, y)
t.seth(60)
t.fillcolor('#DD4D28')
t.begin_fill()
a = 2.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
t.lt(3)
t.fd(a)
else:
a += 0.05
t.lt(3)
t.fd(a)
t.end_fill()
turtle.tracer(True)
def colorLeftEar(self, x, y):
t = self.t
self.noTrace_goto(x, y)
t.fillcolor('#000000')
t.begin_fill()
t.seth(330)
t.circle(100, 35)
t.seth(219)
t.circle(-300, 19)
t.seth(110)
t.circle(-30, 50)
t.circle(-300, 10)
t.end_fill()
def colorRightEar(self, x, y):
t = self.t
self.noTrace_goto(x, y)
t.fillcolor('#000000')
t.begin_fill()
t.seth(300)
t.circle(-100, 30)
t.seth(35)
t.circle(300, 15)
t.circle(30, 50)
t.seth(190)
t.circle(300, 17)
t.end_fill()
def body(self):
t = self.t
t.fillcolor('#F6D02F')
t.begin_fill()
# 右脸轮廓
t.penup()
t.circle(130, 40)
t.pendown()
t.circle(100, 105)
t.left(180)
t.circle(-100, 5)
# 右耳朵
t.seth(20)
t.circle(300, 30)
t.circle(30, 50)
t.seth(190)
t.circle(300, 36)
# 上轮廓
t.seth(150)
t.circle(150, 70)
# 左耳朵
t.seth(200)
t.circle(300, 40)
t.circle(30, 50)
t.seth(20)
t.circle(300, 35)
# print(t.pos())
# 左脸轮廓
t.seth(240)
t.circle(105, 95)
t.left(180)
t.circle(-105, 5)
# 左手
t.seth(210)
t.circle(500, 18)
t.seth(200)
t.fd(10)
t.seth(280)
t.fd(7)
t.seth(210)
t.fd(10)
t.seth(300)
t.circle(10, 80)
t.seth(220)
t.fd(10)
t.seth(300)
t.circle(10, 80)
t.seth(240)
t.fd(12)
t.seth(0)
t.fd(13)
t.seth(240)
t.circle(10, 70)
t.seth(10)
t.circle(10, 70)
t.seth(10)
t.circle(300, 18)
t.seth(75)
t.circle(500, 8)
t.left(180)
t.circle(-500, 15)
t.seth(250)
t.circle(100, 65)
# 左脚
t.seth(320)
t.circle(100, 5)
t.left(180)
t.circle(-100, 5)
t.seth(220)
t.circle(200, 20)
t.circle(20, 70)
t.seth(60)
t.circle(-100, 20)
t.left(180)
t.circle(100, 20)
t.seth(300)
t.circle(10, 70)
t.seth(60)
t.circle(-100, 20)
t.left(180)
t.circle(100, 20)
t.seth(10)
t.circle(100, 60)
# 横向
t.seth(180)
t.circle(-100, 10)
t.left(180)
t.circle(100, 10)
t.seth(5)
t.circle(100, 10)
t.circle(-100, 40)
t.circle(100, 35)
t.left(180)
t.circle(-100, 10)
# 右脚
t.seth(290)
t.circle(100, 55)
t.circle(10, 50)
t.seth(120)
t.circle(100, 20)
t.left(180)
t.circle(-100, 20)
t.seth(0)
t.circle(10, 50)
t.seth(110)
t.circle(100, 20)
t.left(180)
t.circle(-100, 20)
t.seth(30)
t.circle(20, 50)
t.seth(100)
t.circle(100, 40)
# 右侧身体轮廓
t.seth(200)
t.circle(-100, 5)
t.left(180)
t.circle(100, 5)
t.left(30)
t.circle(100, 75)
t.right(15)
t.circle(-300, 21)
t.left(180)
t.circle(300, 3)
# 右手
t.seth(43)
t.circle(200, 60)
t.right(10)
t.fd(10)
t.circle(5, 160)
t.seth(90)
t.circle(5, 160)
t.seth(90)
t.fd(10)
t.seth(90)
t.circle(5, 180)
t.fd(10)
t.left(180)
t.left(20)
t.fd(10)
t.circle(5, 170)
t.fd(10)
t.seth(240)
t.circle(50, 30)
t.end_fill()
self.noTrace_goto(130, 125)
t.seth(-20)
t.fd(5)
t.circle(-5, 160)
t.fd(5)
# 手指纹
self.noTrace_goto(166, 130)
t.seth(-90)
t.fd(3)
t.circle(-4, 180)
t.fd(3)
t.seth(-90)
t.fd(3)
t.circle(-4, 180)
t.fd(3)
# 尾巴
self.noTrace_goto(168, 134)
t.fillcolor('#F6D02F')
t.begin_fill()
t.seth(40)
t.fd(200)
t.seth(-80)
t.fd(150)
t.seth(210)
t.fd(150)
t.left(90)
t.fd(100)
t.right(95)
t.fd(100)
t.left(110)
t.fd(70)
t.right(110)
t.fd(80)
t.left(110)
t.fd(30)
t.right(110)
t.fd(32)
t.right(106)
t.circle(100, 25)
t.right(15)
t.circle(-300, 2)
##############
# print(t.pos())
t.seth(30)
t.fd(40)
t.left(100)
t.fd(70)
t.right(100)
t.fd(80)
t.left(100)
t.fd(46)
t.seth(66)
t.circle(200, 38)
t.right(10)
t.fd(10)
t.end_fill()
# 尾巴花纹
t.fillcolor('#923E24')
self.noTrace_goto(126.82, -156.84)
t.begin_fill()
t.seth(30)
t.fd(40)
t.left(100)
t.fd(40)
t.pencolor('#923e24')
t.seth(-30)
t.fd(30)
t.left(140)
t.fd(20)
t.right(150)
t.fd(20)
t.left(150)
t.fd(20)
t.right(150)
t.fd(20)
t.left(130)
t.fd(18)
t.pencolor('#000000')
t.seth(-45)
t.fd(67)
t.right(110)
t.fd(80)
t.left(110)
t.fd(30)
t.right(110)
t.fd(32)
t.right(106)
t.circle(100, 25)
t.right(15)
t.circle(-300, 2)
t.end_fill()
# 帽子、眼睛、嘴巴、脸颊
self.cap(-134.07, 147.81)
self.mouth(-5, 25)
self.leftCheek(-126, 32)
self.rightCheek(107, 63)
self.colorLeftEar(-250, 100)
self.colorRightEar(140, 270)
self.leftEye(-85, 90)
self.rightEye(50, 110)
t.hideturtle()
def cap(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.fillcolor('#CD0000')
t.begin_fill()
t.seth(200)
t.circle(400, 7)
t.left(180)
t.circle(-400, 30)
t.circle(30, 60)
t.fd(50)
t.circle(30, 45)
t.fd(60)
t.left(5)
t.circle(30, 70)
t.right(20)
t.circle(200, 70)
t.circle(30, 60)
t.fd(70)
# print(t.pos())
t.right(35)
t.fd(50)
t.circle(8, 100)
t.end_fill()
self.noTrace_goto(-168.47, 185.52)
t.seth(36)
t.circle(-270, 54)
t.left(180)
t.circle(270, 27)
t.circle(-80, 98)
t.fillcolor('#444444')
t.begin_fill()
t.left(180)
t.circle(80, 197)
t.left(58)
t.circle(200, 45)
t.end_fill()
self.noTrace_goto(-58, 270)
t.pencolor('#228B22')
t.dot(35)
self.noTrace_goto(-30, 280)
t.fillcolor('#228B22')
t.begin_fill()
t.seth(100)
t.circle(30, 180)
t.seth(190)
t.fd(15)
t.seth(100)
t.circle(-45, 180)
t.right(90)
t.fd(15)
t.end_fill()
t.pencolor('#000000')
def start(self):
self.body()
def main():
print('Painting the Pikachu... ')
turtle.screensize(800, 600)
turtle.title('Pikachu')
pikachu = Pikachu()
pikachu.start()
turtle.mainloop()
if __name__ == '__main__':
main()
3.4 玫瑰花
#!/usr/bin/env python
# coding: utf-8
#绘制玫瑰花并添加文字
import turtle
# 设置画布大小
# turtle.screensize(canvwidth=None, canvheight=None, bg=None)
turtle.setup(width=0.6, height=0.6)
# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
# 输出文字
printer = turtle.Turtle()
printer.hideturtle()
printer.penup()
printer.back(200)
printer.write("赠给亲爱的 ww\n\n", align="right", font=("楷体", 16, "bold"))
printer.write("from fly", align="center", font=("楷体", 12, "normal"))
# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill()
# 花瓣1
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)
turtle.circle(80, 98)
turtle.circle(-90, 40)
# 花瓣2
turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)
# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)
turtle.done()
3.5 小新吃包子
import turtle as t
t.setup(600,600)
t.screensize(100,100,"#b9a281")
t.speed(10)
t.pensize(2)
#头
t.pu()
t.goto(-200,-100)
t.pd()
t.color('black', '#fea993')
t.begin_fill()
t.seth(-30)
t.circle(300,80)
t.circle(110,110)
t.circle(50,20)
t.fd(30)
t.rt(60)
t.circle(170,130)
t.rt(10)
t.circle(250,23)
#耳朵
t.pu()
t.goto(-210,0)
t.seth(115)
t.pd()
t.circle(40,140)
t.circle(200,17)
t.circle(51,120)
t.fd(15)
t.end_fill()
#头发
t.pu()
t.fillcolor(0,0,0)
t.goto(-260,20)
t.begin_fill()
t.pd()
t.rt(11)
t.circle(-40,95)
t.seth(90)
t.fd(72)
t.rt(5)
t.fd(60)
t.circle(-20,50)
t.circle(-500,23)
t.fd(3)
t.seth(162)
t.circle(170,75)
t.lt(0)
t.circle(400,20)
t.end_fill()
#眼睛
t.pu()
t.goto(-170,75)
t.pd()
t.seth(33)
t.circle(-400,16)
t.pu()
t.lt(7)
t.fd(30)
t.pd()
t.fd(25)
t.rt(20)
t.circle(-400,10)
#眉毛
t.pu()
t.goto(-167,135)
t.pd()
t.pensize(30)
t.seth(65)
t.fd(40)
t.circle(-25,105)
t.fd(25)
t.pu()
t.goto(-45,185)
t.pd()
t.pensize(30)
t.seth(55)
t.fd(30)
t.circle(-25,105)
t.fd(25)
#羞羞
t.pensize(3)
t.pu()
t.goto(-155,60)
t.pencolor("#fa2a55")
t.pd()
t.seth(-110)
t.fd(25)
t.pu()
t.goto(-145,63)
t.pd()
t.seth(-110)
t.fd(25)
t.pu()
t.goto(70,125)
t.pd()
t.seth(-110)
t.fd(20)
t.pu()
t.goto(80,120)
t.pd()
t.seth(-110)
t.fd(18)
t.pensize(2)
t.pencolor(0,0,0)
#嘴巴
t.pu()
t.goto(-115,-65)
t.pd()
t.seth(-70)
t.pd()
t.color('black', '#a24857')
t.begin_fill()
a =9
for i in range(36):
if 0 <= i < 9 or 18 <= i <27:
a = a+2.5
t.lt(10)
t.fd(a)
else:
a = a-2.5
t.lt(10)
t.fd(a)
t.end_fill()
#包子
t.fillcolor("#ffd8b1")
t.pu()
t.goto(20,-110)
t.pd()
t.begin_fill()
t.seth(-10)
t.circle(500,25)
t.circle(20,30)
t.fd(10)
t.circle(20,10)
t.fd(10)
t.circle(70,50)
t.circle(30,2)
t.circle(70,60)
t.rt(30)
t.circle(40,70)
t.rt(60)
t.circle(50,70)
t.rt(70)
t.circle(40,70)
t.rt(20)
t.circle(70,40)
t.rt(10)
t.circle(100,60)
t.circle(25,50)
t.end_fill()
t.pu()
t.goto(80,25)
t.pd()
t.seth(-140)
t.circle(50,60)
t.pu()
t.goto(120,27)
t.pd()
t.seth(-130)
t.circle(50,55)
t.pu()
t.goto(150,22)
t.pd()
t.seth(-80)
t.circle(50,50)
t.pu()
t.goto(190,27)
t.pd()
t.fd(15)
t.hideturtle()
t.mainloop()