【Turtle库】皮卡丘

引言

这段Python代码利用了Turtle图形库来绘制一个卡通角色,看起来像皮卡丘。以下是代码的概述:

导入模块:

代码导入了必要的模块:turtle、random,并将turtle重命名为t以便简洁。 函数定义:

infoPrt()函数打印了海龟当前的坐标和角度。 设置海龟:

设置了Turtle图形环境的特定参数,并配置了海龟的初始设置。 头部和耳朵:

代码使用Turtle图形命令绘制了卡通角色的头部和耳朵。 身体和尾巴:

绘制了角色的身体和尾巴,包括尾巴的细节。 身体底部:

代码继续绘制了角色身体的底部。 脸部、眼睛和嘴巴:

使用特定的形状和颜色绘制了角色的脸部、眼睛和嘴巴。 左臂和右臂:

绘制了角色的左臂和右臂,并确定了它们的位置。 左腿和右腿:

代码包括了绘制角色左腿和右腿的部分。 额外的文字:

另一个名为printer的Turtle对象用于显示额外的文字,比如"皮"和其他字符。 隐藏和完成:

隐藏主要的海龟,并完成Turtle图形窗口。 注意:代码的一些部分被用占位符注释,表示提供的片段中省略了具体细节。要理解完整的绘图细节,您需要检查整个代码。

总代码: 

from turtle import *
import turtle as t
from random import *

def infoPrt():
    print('coordinate: ' + str(t.pos()))
    print('angle: ' + str(t.heading()))

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(270)
#t.circle(-30, -180)
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()
#x-20 ,y+50
"""画嘴"""
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(-350,-100)
printer.write("皮\n\n",move = True, align="left", font=("楷体", 30, "bold"))
printer.goto(-350,-150)
printer.write("卡\n\n",move = True, align="left", font=("楷体", 30, "bold"))
printer.goto(-350,-200)
printer.write("丘\n\n",move = True, align="left", font=("楷体", 30, "bold"))
printer.goto(-350,-250)
printer.write("!!\n\n",move = True, align="left", font=("楷体", 30, "bold"))

t.hideturtle()
t.done()

部分解释

导入必要的模块:

from turtle import *
import turtle as t
from random import *

定义一个函数来打印乌龟当前的坐标和角度:

def infoPrt():
    print('coordinate: ' + str(t.pos()))
    print('angle: ' + str(t.heading()))

初始化乌龟图形设置:

t.pensize(3)
t.hideturtle() 
t.colormode(255)
t.color("black")
t.setup(700, 650)
t.speed(1)
t.st()
t.pu()

 总结

在这个例子中,我们使用Python的Turtle库来绘制一个简单的皮卡丘图形。我们首先定义了几个绘制基本形状的函数,包括绘制圆形、椭圆形和矩形。然后,我们使用这些函数来绘制皮卡丘的脸部、耳朵和身体部分。

在绘制皮卡丘脸部时,我们首先使用一个圆形来表示脸部中央部分,然后使用一个圆形来表示脸部的高光部分。通过设置高光部分的填充颜色为白色,可以增加暗部和亮部的对比度,使脸部看起来更立体。

接下来,我们绘制皮卡丘的耳朵部分。我们使用两个半圆形来表示耳朵的外侧和内侧,并使用一个矩形来表示耳朵的根部。通过调整矩形的高度和宽度,可以调整耳朵的大小和形状。

最后,我们绘制皮卡丘的身体部分。我们使用一个矩形来表示身体,并使用一个椭圆形来表示身体的尾巴部分。通过调整矩形和椭圆形的大小和位置,可以调整身体的大小和形状。

在绘制过程中,我们使用了Turtle库提供的各种绘图函数,包括画笔的颜色、填充颜色、速度、形状大小等。通过合理地使用这些函数,我们可以绘制出各种形状和图案,包括皮卡丘这样的简单图形。

总之,Turtle库是一个非常有用的Python库,它可以帮助我们轻松地绘制各种形状和图案。通过学习和掌握Turtle库的使用方法,我们可以更好地控制绘图过程,并创造出更加精美和有趣的图形作品。

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个利用turtle绘制皮卡丘的示例代码: ```python import turtle # 设置画布大小和背景颜色 turtle.setup(500, 500) turtle.bgcolor('#FED55D') # 画皮卡丘的左耳朵 turtle.penup() turtle.goto(-120, 100) turtle.pendown() turtle.setheading(150) turtle.begin_fill() turtle.circle(70, -100) turtle.circle(30, -60) turtle.circle(70, -40) turtle.end_fill() # 画皮卡丘的右耳朵 turtle.penup() turtle.goto(120, 100) turtle.pendown() turtle.setheading(-30) turtle.begin_fill() turtle.circle(70, 100) turtle.circle(30, 60) turtle.circle(70, 40) turtle.end_fill() # 画皮卡丘的脸 turtle.penup() turtle.goto(0, -50) turtle.pendown() turtle.setheading(0) turtle.begin_fill() turtle.circle(120) turtle.end_fill() # 画皮卡丘的眼睛 turtle.penup() turtle.goto(-50, 60) turtle.pendown() turtle.setheading(0) turtle.begin_fill() turtle.circle(25) turtle.end_fill() turtle.penup() turtle.goto(-50, 70) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.penup() turtle.goto(50, 60) turtle.pendown() turtle.begin_fill() turtle.circle(25) turtle.end_fill() turtle.penup() turtle.goto(50, 70) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill() # 画皮卡丘的嘴巴 turtle.penup() turtle.goto(-70, 0) turtle.pendown() turtle.setheading(-60) turtle.circle(80, 120) # 画皮卡丘的腮红 turtle.penup() turtle.goto(-90, -30) turtle.pendown() turtle.setheading(0) turtle.begin_fill() turtle.circle(20) turtle.end_fill() turtle.penup() turtle.goto(90, -30) turtle.pendown() turtle.setheading(0) turtle.begin_fill() turtle.circle(20) turtle.end_fill() # 画皮卡丘的手 turtle.penup() turtle.goto(-120, -50) turtle.pendown() turtle.setheading(-30) turtle.begin_fill() turtle.circle(30, 120) turtle.circle(10, 180) turtle.circle(30, 120) turtle.end_fill() turtle.penup() turtle.goto(120, -50) turtle.pendown() turtle.setheading(-150) turtle.begin_fill() turtle.circle(30, 120) turtle.circle(10, 180) turtle.circle(30, 120) turtle.end_fill() # 画皮卡丘的脚 turtle.penup() turtle.goto(-50, -120) turtle.pendown() turtle.setheading(-90) turtle.begin_fill() turtle.circle(50, 180) turtle.forward(50) turtle.end_fill() turtle.penup() turtle.goto(50, -120) turtle.pendown() turtle.setheading(-90) turtle.begin_fill() turtle.circle(50, 180) turtle.forward(50) turtle.end_fill() # 完成绘制 turtle.done() ``` 您可以运行这段代码,查看绘制的皮卡丘效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zouia Gail(修行中)

你的鼓励是对我创作的最大支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值