前言
我用用python实现三只可爱的宝可梦!
小海龟
Python中的turtle是一个简单易用的绘图库,它可以让我们通过编程的方式画出各种各样的图形。它内部实现了一个海龟(turtle),我们可以通过控制海龟的移动方向和长度等参数来进行绘图,非常适合初学者入门使用。本文将介绍turtle的基本绘图函数和实例,帮助初学者快速入门。
01
安装turtle
turtle是Python自带的标准库,所以我们不需要安装任何东西,只需要在Python终端或者编辑器上导入turtle库即可开始使用。
import turtle
02
turtle的基本绘图函数
turtle的基本绘图函数有很多,下面是一些常用的函数:
- turtle.forward(distance):向当前方向移动指定距离的海龟。
- turtle.backward(distance):向相反方向移动指定距离的海龟。
- turtle.right(angle):将当前方向向右旋转指定角度。
- turtle.left(angle):将当前方向向左旋转指定角度。
- turtle.penup():将画笔抬起,不再画出海龟的轨迹。
- turtle.pendown():将画笔放下,继续画出海龟的轨迹。
- turtle.pensize(width):设置画笔的宽度为指定大小。
- turtle.pencolor(color):设置画笔的颜色为指定颜色。
- turtle.fillcolor(color):设置填充的颜色为指定颜色。
- turtle.begin_fill():开始填充。
- turtle.end_fill():结束填充。
- turtle.circle(radius):绘制一个指定半径的圆形。
03
turtle的实例
下面是一些turtle的实例,帮助我们更好地理解上面的基本绘图函数。
3.1 简单的绘图
我们先来画一个简单的图形,让海龟向前移动100个像素,然后向左旋转90度,再向前移动100个像素,最后向左旋转90度,形成一个正方形图形。
import turtle
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.done() # 表示绘图结束
3.2 绘制彩色螺旋图形
下面我们来绘制一个彩色的螺旋图形,让海龟不断向前移动并旋转,每次旋转的角度和颜色都不同,最终形成一个美丽的螺旋图形。
import turtle
turtle.speed(10)
for i in range(200):
turtle.forward(i)
turtle.right(98)
turtle.pencolor('red')
turtle.pencolor('orange')
turtle.pencolor('yellow')
turtle.pencolor('green')
turtle.pencolor('cyan')
turtle.pencolor('blue')
turtle.pencolor('purple')
turtle.done()
3.3 绘制五角星
下面我们来绘制一个五角星,让海龟向前移动100个像素,向左旋转72度,重复5次,即可形成一个五角星。
import turtle
turtle.pensize(5)
turtle.pencolor('purple')
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(5):
turtle.forward(100)
turtle.right(72)
turtle.forward(100)
turtle.left(144)
turtle.end_fill()
turtle.done()
3.4 绘制圆形和正方形
下面我们来绘制一个圆形和一个正方形,先绘制一个圆形,然后以圆心为起点,绘制出正方形。
import turtle
turtle.circle(50) # 画一个圆形
turtle.penup() # 将画笔抬起
turtle.goto(0, -50) # 将海龟移动到圆心下面的位置
turtle.pendown() # 将画笔放下
for i in range(4):
turtle.forward(100)
turtle.left(90)
turtle.done()
通过上述实例,我们可以看到,turtle库非常适合初学者入门使用,其简单易用的接口和实时绘图的效果,可以让我们快速地理解和掌握Python编程的基本思路和方法。
皮卡丘
迷你版
完整程序
import turtle as t
import time
# 基础设置
t.setup(500, 500) # 设置画布大小
t.bgcolor("pink")
t.title("pikachu")
t.pensize(1) # 设置画笔的大小
# 画左偏曲线函数
def left(ang, dis, step, n):
for i in range(n):
dis += step # dis增大step
t.lt(ang) # 向左转ang度
t.fd(dis) # 向前走dis的步长
def right(ang, dis, step, n):
for i in range(n):
dis += step
t.rt(ang) # 向左转ang度
t.fd(dis) # 向前走dis的步长
# 画耳朵
def Ears():
t.color("black", "yellow")
# 左耳朵曲线
t.pu() # 提笔
t.goto(-50, 100) # 笔头初始位置
t.pd() # 下笔
t.setheading(110) # 画笔角度
t.begin_fill()
left(1.2, 0.4, 0.1, 40)
t.setheading(270) # 画笔角度
left(1.2, 0.4, 0.1, 40)
t.setheading(44) # 画笔角度
t.forward(32)
t.end_fill()
# 右耳朵曲线
t.pu() # 提笔
t.goto(50, 100) # 笔头初始位置
t.pd() # 下笔
t.setheading(70) # 画笔角度
t.begin_fill()
right(1.2, 0.4, 0.1, 40)
t.setheading(270) # 画笔角度
right(1.2, 0.4, 0.1, 40)
t.setheading(136) # 画笔角度
t.forward(32)
t.end_fill()
# 耳朵黑
t.begin_fill()
t.fillcolor("black")
t.pu() # 提笔
t.goto(88, 141) # 笔头初始位置
t.pd() # 下笔
t.setheading(35) # 画笔角度
right(1.2, 1.6, 0.1, 16)
t.setheading(270) # 画笔角度
right(1.2, 0.4, 0.1, 25)
t.setheading(132) # 画笔角度
t.forward(31)
t.end_fill()
t.begin_fill()
t.fillcolor("black")
t.pu() # 提笔
t.goto(-88, 141) # 笔头初始位置
t.pd() # 下笔
t.setheading(145) # 画笔角度
left(1.2, 1.6, 0.1, 16)
t.setheading(270) # 画笔角度
left(1.2, 0.4, 0.1, 25)
t.setheading(48) # 画笔角度
t.forward(31)
t.end_fill()
# 画尾巴
def Tail():
# 尾巴
t.begin_fill()
t.fillcolor("yellow")
t.pu() # 提笔
t.goto(64, -140) # 笔头初始位置
t.pd() # 下笔
t.setheading(10) # 画笔角度
t.forward(20)
t.setheading(90) # 画笔角度
t.forward(20)
t.setheading(10) # 画笔角度
t.forward(10)
t.setheading(80) # 画笔角度
t.forward(100)
t.setheading(35) # 画笔角度
t.forward(80)
t.setheading(260) # 画笔角度
t.forward(100)
t.setheading(205) # 画笔角度
t.forward(40)
t.setheading(260) # 画笔角度
t.forward(37)
t.setheading(205) # 画笔角度
t.forward(20)
t.setheading(260) # 画笔角度
t.forward(25)
t.setheading(175) # 画笔角度
t.forward(30)
t.setheading(100) # 画笔角度
t.forward(13)
t.end_fill()
# 画脚
def Foots():
# 脚
t.begin_fill()
t.fillcolor("yellow")
t.pensize(2)
t.pu() # 提笔
t.goto(-70, -200) # 笔头初始位置
t.pd() # 下笔
t.setheading(225) # 画笔角度
left(0.5, 1.2, 0, 12)
left(35, 0.6, 0, 4)
left(1, 1.2, 0, 18)
t.setheading(160) # 画笔角度
t.forward(13)
t.end_fill()
t.begin_fill()
t.fillcolor("yellow")
t.pensize(2)
t.pu() # 提笔
t.goto(70, -200) # 笔头初始位置
t.pd() # 下笔
t.setheading(315) # 画笔角度
right(0.5, 1.2, 0, 12)
right(35, 0.6, 0, 4)
right(1, 1.2, 0, 18)
t.setheading(20) # 画笔角度
t.forward(13)
t.end_fill()
# 画身体
def Body():
# 外形轮廓
t.begin_fill()
t.pu() # 提笔
t.goto(112, 0) # 笔头初始位置
t.pd() # 下笔
t.setheading(90) # 画笔角度
t.circle(112, 180)
t.setheading(250) # 画笔角度
left(1.6, 1.3, 0, 50)
left(0.8, 1.5, 0, 25)
t.setheading(255) # 画笔角度
left(0.4, 1.6, 0.2, 27)
left(2.8, 1, 0, 45)
right(0.9, 1.4, 0, 31)
t.setheading(355) # 画笔角度
right(0.9, 1.4, 0, 31)
left(2.8, 1, 0, 45)
left(0.4, 7.2, -0.2, 27)
t.setheading(10) # 画笔角度
left(0.8, 1.5, 0, 25)
left(1.6, 1.3, 0, 50)
t.end_fill()
def Eyes_Open():
# 左眼睛
t.begin_fill()
t.fillcolor("black")
t.pu() # 提笔
t.goto(-46, 10) # 笔头初始位置
t.pd() # 下笔
t.setheading(90) # 画笔角度
t.circle(5, 360)
t.end_fill()
# 右眼睛
t.begin_fill()
t.fillcolor("black")
t.pu() # 提笔
t.goto(46, 10) # 笔头初始位置
t.pd() # 下笔
t.setheading(-90) # 画笔角度
t.circle(5, 360)
t.end_fill()
# 画脸
def Face():
# 脸蛋
t.begin_fill()
t.fillcolor("red")
t.pu() # 提笔
t.goto(-63, -10) # 笔头初始位置
t.pd() # 下笔
t.setheading(90) # 画笔角度
t.circle(10, 360)
t.end_fill()
t.begin_fill()
t.fillcolor("red")
t.pu() # 提笔
t.goto(63, -10) # 笔头初始位置
t.pd() # 下笔
t.setheading(-90) # 画笔角度
t.circle(10, 360)
t.end_fill()
# 嘴巴
t.pensize(2.2)
t.pu() # 提笔
t.goto(0, 0) # 笔头初始位置
t.pd() # 下笔
t.setheading(235) # 画笔角度
right(5, 0.8, 0, 30)
t.pu() # 提笔
t.goto(0, 0) # 笔头初始位置
t.pd() # 下笔
t.setheading(305) # 画笔角度
left(5, 0.8, 0, 30)
# 画手
def Hands():
# 左手
t.pensize(2)
t.pu() # 提笔
t.goto(-46, -100) # 笔头初始位置
t.pd() # 下笔
t.setheading(285) # 画笔角度
right(0.4, 1.2, 0, 26)
right(5, 0.35, 0, 26)
right(0.3, 1.2, 0, 15)
# 右手
t.pu() # 提笔
t.goto(46, -100) # 笔头初始位置
t.pd() # 下笔
t.setheading(255) # 画笔角度
left(0.4, 1.2, 0, 26)
left(5, 0.35, 0, 26)
left(0.3, 1.2, 0, 15)
def Eyes_Close():
# 左眼睛
t.pu() # 提笔
t.goto(-46, 12) # 笔头初始位置
t.pd() # 下笔
t.setheading(180) # 画笔角度
t.forward(10)
# 右眼睛
t.pu() # 提笔
t.goto(46, 12) # 笔头初始位置
t.pd() # 下笔
t.setheading(0) # 画笔角度
t.forward(10)
# 爱心
def Love(): # 画爱心函数,就是用turtle画爱心
t.pensize(1)
t.penup()
t.color("pink")
t.goto(-2.5, -100)
t.pendown()
t.begin_fill()
t.fillcolor('pink')
t.setheading(120)
t.circle(20, 195)
t.fd(20 * 2.4)
t.lt(90)
t.fd(20 * 2.4)
t.circle(20, 195)
t.end_fill()
# 初始化
def Init():
Ears()
Tail()
Foots()
Body()
Face()
Hands()
Eyes_Open()
Love()
# 眨眼睛
def Close():
Ears()
Tail()
Foots()
Body()
Face()
Hands()
Eyes_Close()
def Open():
Ears()
Tail()
Foots()
Body()
Face()
Hands()
Eyes_Open()
Love()
def main():
# 初始化皮卡丘
Init()
# 眨眼睛动画
for i in range(30):
if i % 2 == 0:
t.reset()
t.hideturtle()
Close()
t.update()
time.sleep(0.3)
else:
t.reset()
t.hideturtle()
Open()
t.update()
time.sleep(1)
t.tracer(0)
main()
t.done()
程序分析
这是一个使用Python的turtle库绘制皮卡丘的程序。下面是对程序的分析:
- 基础设置
首先,程序设置了画布的大小、背景颜色和窗口的标题等基础设置。
- 画左偏曲线函数和右偏曲线函数
程序定义了两个函数:left()和right()用于画左偏和右偏的曲线。这两个函数分别接受四个参数:旋转角度、前进距离、增加的前进距离、曲线的段数。这两个函数使用turtle库提供的函数实现曲线的绘制。
- 画耳朵、尾巴、脚和身体
程序定义了四个函数:Ears(),Tail(),Foots()和Body(),并使用这些函数画出了一个皮卡丘的基本轮廓。这些函数使用前面定义的left()和right()函数来实现曲线的绘制。
- 画眼睛和嘴巴
程序定义了一个函数Face(),并在其中画出了皮卡丘的面部特征,包括眼睛和嘴巴等。
- 画手
程序定义了一个函数Hands(),并使用left()和right()函数以及前进和旋转等基础操作函数来实现手的绘制。其中,左手和右手的绘制是对称的。
- 眨眼动画
程序定义了Open()和Close()函数来实现眼睛的开闭动画效果。通过不断地调用这两个函数,程序实现了一个皮卡丘眨眼睛的动画效果。
- 爱心
程序定义了一个函数Love(),使用turtle库提供的函数以及前进、旋转和绘制填充等基础操作来画出一个爱心。
- 初始化和主函数
程序定义了一个Init()函数,用于调用前面定义的函数来完成皮卡丘的绘制。程序还定义了一个主函数main(),在其中使用Init()函数来初始化皮卡丘,并实现眨眼睛的动画效果。
总体来说,程序使用turtle库提供的函数和基础操作函数来绘制皮卡丘的各种部位,并通过调用函数和控制基础操作的顺序和参数,实现了一个可爱的皮卡丘绘图程序。
高级版
完整程序
from turtle import *
import turtle as t
def infoPrt():
print('coordinate: ' + str(t.pos()))
print('angle: ' + str(t.heading()))
t.tracer(0)
t.pensize(3)
t.hideturtle()
t.colormode(255)
t.color("black")
t.setup(700, 650)
t.speed(1)
t.st()
# t.dot()
t.pu()
# t.goto(-150,100)
t.goto(-210, 86)
t.pd()
infoPrt()
# 头
print('头')
t.seth(85)
t.circle(-100, 50)
# t.seth(78)
# t.circle(-100,25)
infoPrt()
t.seth(25)
t.circle(-170, 50)
infoPrt()
# 右耳
print('右耳')
t.seth(40)
# t.circle(-250,52)
t.circle(-250, 30)
infoPrt()
# 右耳尖
t.begin_fill()
# 左
t.circle(-250, 22)
# t.fillcolor("pink")
# 右
t.seth(227)
t.circle(-270, 15)
prePos = t.pos()
infoPrt()
# 下
t.seth(105)
t.circle(100, 32)
t.end_fill()
t.pu()
t.setpos(prePos)
t.pd()
t.seth(212)
t.circle(-270, 28)
prePos = t.pos()
t.pu()
t.goto(t.xcor() + 5, t.ycor() - 2)
t.pd()
# 躯干
print('躯干')
t.seth(280)
t.circle(500, 30)
infoPrt()
# 臀部
print('臀部')
t.seth(120)
# t.circle(150, -55)
t.circle(150, -11)
p_tail = t.pos()
t.circle(150, -44)
p_butt = t.pos()
infoPrt()
# 尾巴
t.pu()
t.setpos(p_tail)
t.pd()
t.begin_fill()
t.seth(50)
t.fd(25)
t.seth(-50)
t.fd(30)
p_tail1 = t.pos
t.seth(-140)
t.fd(36)
t.end_fill()
t.seth(39)
# 右尾和h1
t.fd(72)
# 右尾和v1
t.seth(125)
t.fd(48)
# 右尾和h2
t.seth(40)
t.fd(53)
# 右尾和v2
t.seth(88)
t.fd(45)
# 右尾和h3
t.seth(35)
t.fd(105)
# 右尾和v3
t.seth(105)
t.circle(850, 8)
# t.fd(105)
t.seth(215)
# t.fd(125)
t.circle(850, 11)
t.seth(280)
t.fd(110)
t.seth(220)
t.fd(50)
t.seth(309)
t.fd(56)
# 底盘
print('底盘')
t.pu()
t.setpos(p_butt)
t.pd()
t.seth(20)
t.circle(120, -45)
infoPrt()
t.seth(330)
t.circle(-150, -30)
infoPrt()
prePos = t.pos()
t.pu()
t.goto(t.xcor() + 20, t.ycor())
t.pd()
t.seth(230)
t.circle(-70, 120)
p_bot = t.pos()
# 两脚-right
t.pu()
t.setpos(p_butt)
t.setpos(t.xcor() + 5, t.ycor() + 5)
t.pd()
t.seth(-86)
t.fd(30)
t.seth(-93)
t.fd(33)
t.seth(-225)
t.circle(-150, 22)
# 两脚-left
t.pu()
t.setpos(p_bot)
t.setpos(t.xcor() + 85, t.ycor() - 43)
t.pd()
t.seth(-105)
t.fd(50)
t.seth(-225)
t.circle(-150, 22)
# 左躯干
print('躯干')
t.pu()
t.setpos(p_bot)
t.pd()
t.seth(90)
t.circle(450, 13)
p_lfhd = t.pos()
t.circle(450, 5)
t.pu()
t.circle(450, 5)
t.pd()
t.circle(450, 6)
infoPrt()
# 左脸
t.begin_fill()
t.fillcolor("pink")
print('左脸')
t.seth(330)
t.circle(50, -90)
infoPrt()
# 左酒窝
t.seth(30)
t.circle(-15, 120)
t.seth(-70)
t.circle(-30, 90)
t.end_fill()
# 左手
t.pu()
t.setpos(p_lfhd)
t.pd()
t.seth(160)
t.circle(150, 30)
infoPrt()
t.seth(180)
t.circle(-30, 150)
t.fd(67)
t.pu()
t.setpos(t.xcor() - 40, t.ycor() - 60)
t.pd()
t.seth(200)
t.circle(-5, 180)
# 右手
t.pu()
t.setpos(p_lfhd)
t.setpos(t.xcor() + 180, t.ycor() + 5)
t.pd()
t.seth(200)
t.circle(-50, 100)
t.pu()
t.circle(-50, 15)
t.pd()
t.circle(-50, 65)
t.pu()
t.setpos(t.xcor() + 10, t.ycor() - 45)
t.pd()
t.seth(80)
t.fd(10)
t.seth(165)
t.circle(10, 60)
t.seth(90)
t.fd(5)
t.seth(165)
t.circle(10, 60)
t.seth(95)
t.fd(5)
t.seth(185)
t.circle(10, 60)
t.seth(105)
t.fd(10)
t.seth(230)
t.fd(20)
t.seth(145)
t.fd(10)
t.seth(285)
t.fd(20)
# 右酒窝
t.begin_fill()
t.fillcolor("pink")
t.pu()
t.setpos(t.xcor() - 40, t.ycor() + 110)
t.pd()
t.circle(27, 360)
t.end_fill()
"""画嘴"""
color("black", "#F35590")
# 下嘴弧度并填充颜色
penup()
goto(-100, 72)
pendown()
begin_fill()
setheading(260)
forward(60)
circle(-11, 150)
forward(55)
print(position())
penup()
goto(-128.46, 71.97)
pendown()
end_fill()
# 嘴中最上方的阴影部分
color("#6A070D", "#6A070D")
begin_fill()
penup()
goto(-99.00, 72.00)
pendown()
penup()
goto(-104.29, 48.3)
pendown()
penup()
goto(-142, 45)
pendown()
penup()
goto(-150.40, 62.74)
pendown()
penup()
goto(-128.46, 71.97)
pendown()
penup()
goto(-99.00, 72.00)
pendown()
end_fill()
# 上嘴唇
color("black", "#FFD624")
penup()
goto(-168, 65)
pendown()
begin_fill()
setheading(-25)
for i in range(2):
setheading(-25)
circle(35, 70)
end_fill()
# 嘴中第二个阴影部分
color("#AB1945", "#AB1945")
penup()
goto(-142, 45)
pendown()
begin_fill()
setheading(40)
circle(-33, 70)
goto(-104, 48.3)
penup()
goto(-108, 33)
pendown()
setheading(155)
circle(25, 70)
end_fill()
# 左眼
t.pu()
t.color("black")
t.setpos(t.xcor() - 40, t.ycor() + 90)
t.pd()
t.circle(5)
t.pu()
t.setpos(t.xcor() + 5, t.ycor() + 10)
t.pd()
t.begin_fill()
t.seth(190)
t.circle(15, 130)
t.seth(310)
t.circle(10, 15)
t.seth(0)
t.circle(17, 133)
t.seth(90)
t.circle(10, 15)
t.end_fill()
t.pu()
t.setpos(t.xcor() + 2, t.ycor() - 15)
t.pd()
t.color("white")
t.begin_fill()
t.circle(5)
t.end_fill()
# 右眼
t.pu()
t.setpos(t.xcor() + 85, t.ycor() + 15)
t.pd()
t.color("black")
t.circle(5)
t.pu()
t.setpos(t.xcor() + 5, t.ycor() + 10)
t.pd()
t.begin_fill()
t.seth(190)
t.circle(20, 130)
t.seth(310)
t.circle(10, 15)
t.seth(0)
t.circle(22, 133)
t.seth(90)
t.circle(13, 15)
t.end_fill()
t.pu()
t.setpos(t.xcor() - 7, t.ycor() - 15)
t.pd()
t.color("white")
t.begin_fill()
t.circle(7)
t.end_fill()
# 左耳
t.color("black")
t.pu()
t.goto(-210, 86)
t.setpos(t.xcor() + 15, t.ycor() + 38)
t.pd()
t.seth(90)
t.circle(-250, 30)
t.begin_fill()
# 左
t.circle(-250, 18)
# 右
t.seth(270)
t.circle(-270, 12)
prePos = t.pos()
# 下
t.seth(180)
t.circle(100, 30)
t.end_fill()
t.pu()
t.setpos(prePos)
t.pd()
t.seth(270)
t.circle(-270, 18)
t.screensize(50, 50, bg='yellow')
# 输出文字
printer = t.Turtle()
printer.hideturtle()
printer.penup()
printer.goto(-50, 220)
printer.color("black")
printer.write("Pika !", move=True, align="left", font=("Comic Sans MS", 50, "bold"))
# 爱心
t.pensize(1)
t.penup()
t.color("pink")
t.goto(-125, -50)
t.pendown()
t.begin_fill()
t.fillcolor('pink')
t.setheading(120)
t.circle(40, 195)
t.fd(40 * 2.4)
t.lt(90)
t.fd(40 * 2.4)
t.circle(40, 195)
t.end_fill()
t.hideturtle()
t.done()
程序分析
这是一个使用turtle库绘制皮卡丘的程序。下面是对程序的分析:
- 基础设置
程序使用turtle库提供的函数和基础操作函数进行绘制。在开始绘制前,程序进行了一些基础设置。比如,设置画布大小、画笔的粗细和隐藏画笔等。
- 绘制图形
程序通过turtle库提供的函数和基础操作函数,绘制出了皮卡丘的各个部分,包括头、身体、四肢以及面部等。在绘制过程中,程序使用了turtle库提供的绘制圆形、绘制直线、填充颜色等函数。
- 绘制文字
程序使用turtle库提供的函数和基础操作函数,绘制了一个"Pika!"的文字,并将其放置在画布上方。程序使用了Turtle实例来进行绘制。
- 绘制爱心
程序通过turtle库提供的函数和基础操作函数,绘制了一个粉色的爱心。在绘制过程中,程序使用了turtle库提供的绘制圆形、绘制直线、填充颜色等函数。
- 总结
综上所述,这是一个使用turtle库绘制皮卡丘的程序。程序使用了turtle库提供的函数和基础操作函数,实现了各种绘图功能,绘制出了一个可爱的皮卡丘。程序还使用了Turtle实例来绘制文字,并在画布上方放置了一个粉色的爱心。该程序展示了Python和turtle库的强大功能,同时也向用户展示了皮卡丘的可爱形象。
豪华版
完整程序
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):
turtle.tracer(0)
self.t.penup()
self.t.goto(x, y)
self.t.pendown()
def leftEye(self, x, y):
turtle.tracer(0)
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):
turtle.tracer(0)
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(0)
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):
turtle.tracer(0)
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):
turtle.tracer(0)
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):
turtle.tracer(0)
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):
turtle.tracer(0)
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):
turtle.tracer(0)
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()
turtle.setup(999, 777)
turtle.title('Pikachu')
pikachu = Pikachu()
pikachu.start()
turtle.penup()
turtle.goto(-444,200)
turtle.bgcolor("blue")
turtle.color("gold")
turtle.write("Pika !", move = True, align="left", font=("Comic Sans MS", 66, "bold"))
turtle.pensize(1)
turtle.penup()
turtle.color("skyblue")
turtle.goto(-2.5, -100)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor('skyblue')
turtle.setheading(120)
turtle.circle(30, 195)
turtle.fd(30 * 2.4)
turtle.lt(90)
turtle.fd(30 * 2.4)
turtle.circle(30, 195)
turtle.end_fill()
turtle.pendown()
turtle.mainloop()
程序分析
这是一个使用turtle库绘制皮卡丘的程序,下面是对程序的分析:
- 程序基础设置
程序在开始绘制之前,对画布进行了一些基础设置,包括设置画布大小、设置画笔粗细、速度、隐藏turtle绘制轨迹等等。
- 绘制各个部分
程序通过定义不同的方法,使用turtle库绘制了皮卡丘的各个部分,包括左右眼、嘴巴、红脸颊、左右耳朵等。在绘制过程中,程序使用了turtle库提供的绘制圆形、绘制直线、填充颜色等函数。
- 绘制身体部分
程序通过定义一个body方法,使用turtle库绘制了皮卡丘的身体部分。在绘制过程中,程序使用了turtle库提供的绘制弧线、绘制曲线、绘制直线、填充颜色等函数,完成了身体的细节绘制。
- 绘制帽子
绘制帽子的方法是cap,使用turtle库绘制帽子的细节,包括帽子的形状、红色部分以及帽檐。在绘制过程中,程序使用了turtle库提供的绘制圆形、绘制直线、填充颜色等函数。
- 绘制文字和图案
程序使用turtle库提供的函数和基础操作函数,绘制了一个"Pika!"的文字,并使用turtle库提供的函数绘制了一个小图案。完成了绘制后,程序还使用了Turtle实例来绘制文字,并将其放置在画布上方。
- 总结
综上所述,这是一个使用turtle库绘制皮卡丘的程序。程序使用了turtle库提供的函数和基础操作函数,实现了各种绘图功能,绘制出了一个可爱的皮卡丘。程序还使用了Turtle实例来绘制文字,并在画布上方放置了一个可爱的小图案。该程序展示了Python和turtle库的强大功能,并向用户展示了皮卡丘的可爱形象。
尾声
感谢支持吖!