用python画一只帅气的皮卡丘
作者:一手代码一手诗 发布时间:2023-08-10 09:18:13
没错,全网最帅的比卡丘在我这~~~
为了访问 Python 库,您需要将它导入到您的 Python 环境中,使用以下命令将其导入 turtle到您的 Python 脚本中。
import turtle
首先,让我们创建一个 Screen 实例。
wn = turtle.Screen()
现在,创建一个 Turtle 实例。
self.t = turtle.Turtle()
让我们将速度设置为 3 使用 speed 方法,这意味着皮卡丘不会只是出现在屏幕上,绘图也会有一些动画。如果您想更改背景颜色,可以使用该 bgcolor 方法,默认为白色。您可以使用pensize 方法调整笔的粗细 ,它会稍微加粗。ondrag 拖动 Turtle 将在屏幕上移动它,从而产生手绘(如果笔已放下)。
t.pensize(3)
t.speed(9)
t.ondrag(getPosition)
现在让我们看看这个程序中使用的一些常用方法:
Turtle(): 用于创建并返回一个新的海龟对象。
forward(value): 相对于指定的值,海龟向前移动。
向后(值): 相对于指定的值,海龟向后移动。
right(angle): 乌龟顺时针转动。
left(angle): 海龟逆时针旋转。
penup(): 捡起乌龟笔。
pendown(): 乌龟笔放下。
up(): 与 penup() 相同。
down(): 与 pendown() 相同。
color(颜色名称): Turtle 笔的颜色发生变化。
fillcolor(颜色名称): 用于填充特定形状的颜色已更改。
标题(): 返回当前标题。
position(): 返回当前位置。
goto(x, y): 将海龟的位置移动到坐标 x, y。
end_fill(): 关闭多边形后填充当前填充颜色。
begin_fill(): 记住填充多边形的起点。
dot(): 点留在当前位置。
stamp(): 海龟形状的印象留在当前位置。
Shape(): 应该是 – ‘turtle’, ‘classic’, ‘arrow’ 或 ‘circle’。
上代码:
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()
# Lower Lip
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())
# Upper Lip
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()
# Tongue
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()
# Nose
self.noTrace_goto(-17, 94)
t.seth(8)
t.fd(4)
t.back(8)
# Red Cheeks
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()
# Right face contour
t.penup()
t.circle(130, 40)
t.pendown()
t.circle(100, 105)
t.left(180)
t.circle(-100, 5)
# Right ear
t.seth(20)
t.circle(300, 30)
t.circle(30, 50)
t.seth(190)
t.circle(300, 36)
# Upper profile
t.seth(150)
t.circle(150, 70)
# Left ear
t.seth(200)
t.circle(300, 40)
t.circle(30, 50)
t.seth(20)
t.circle(300, 35)
#print(t.pos())
# Left face contour
t.seth(240)
t.circle(105, 95)
t.left(180)
t.circle(-105, 5)
# Left hand
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)
# Left foot
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)
# Horizontal
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)
# Right foot
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)
# Right body contour
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)
# Right hand
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)
# Fingers
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)
# Tail
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()
# Tail Pattern
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()
# Hat, Eye, Mouth, Cheek
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... ')
wn = turtle.Screen()
wn.setup(width=600, height=800)
pikachu = Pikachu()
pikachu.start()
turtle.mainloop()
if __name__ == '__main__':
main()
来源:https://blog.51cto.com/u_14303514/4920099
猜你喜欢
- label转onehot的很多,但是onehot转label的有点难找,所以就只能自己实现以下,用的topk函数,不知道有没有更好的实现on
- 1、str.split不支持正则及多个切割符号,不感知空格的数量,比如用空格切割,会出现下面情况。>>> s1="
- MySQL 数据(字段)类型在创建表的时候,要明确定义字段对应的数据类型。MySQL 主要的数据类型分为数值类型、字符串(文本)类型、时间日
- 匿名管道管道是一个单向通道,有点类似共享内存缓存.管道有两端,包括输入端和输出端.对于一个进程的而言,它只能看到管道一端,即要么是输入端要么
- 使用python将图片改为灰度图或黑白图有三种方式,分别是是使用cv2库和PIL库来实现,详细过程如下所示。1. 使用cv2库将图片改为灰度
- 在通过拼组sql语句来实现数据插入的应用中,我们很有可能会遇到需要插入大型数据的情况,例如,在oracle中需要插入字节数超过4000的字段
- 关于python数据分析常用库pandas中的DataFrame的loc和iloc取数据 基本方法总结归纳及示例如下:1.准备一组DataF
- 前言 使用scrapy进行大型爬取任务的时候(爬取耗时以天为单位),无论主机网速多好,爬完之后总会发现scrapy
- 最近越来越多在博客上写些UX相关的内容作为分享,就涉及到跟普通博文不一样的文章建构问题。文章内容固然很重要,但排版、组织也是提高可读性和用户
- 任何一个交互过程的操作,对于用户来说都有学习成本,谁也不能保证所有人都可以准确无误地走完一个流程。交互设计师在设计时应该考虑适时地给用户相应
- 对于比较长的数字组成的字符串,我们一般会用逗号(,)隔开来格式化数字,从右往左每三个数字用一个逗号分组隔开。为什么要用逗号隔开数字呢?因为当
- 前言:Python 面向对象的高级编程语言,以其语法简单、免费开源、免编译扩展性高,同时也可以嵌入到C/C++程序和丰富的第三方
- 本文实例讲述了Python中函数的参数定义和可变参数用法。分享给大家供大家参考。具体如下:刚学用Python的时候,特别是看一些库的源码时,
- 开发环境:Ubuntu16.04+Django 1.11.9+Python2.7一:使用自定义函数输出日志到log文件:import tim
- 目录1. 双向循环的练习2. break_pass_continue的使用3. for循环小提示:4. 小练习问题:答案:总结1. 双向循环
- 内容摘要:浏览器不兼容这个难题,一直是网页设计师们头痛的事情。ie7.0的面世,尚且不论他是否较之ie6.0进步, ie7和ie6
- 方法一 :这个是我在站长工具的查询页面使用的防止频繁查询,刷新页面的代码!下面函数的功能是3秒内查询页面即刷新了页面,超过2次就提示!sea
- DROP PRIMAY DEY用于取消主索引。注释:在MySQL较早的版本中,如果没有主索引,则DROP PRIMARY KEY会取消表中的
- 一、k-近邻算法原理及API1.k-近邻算法原理如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则
- 表示文字链接最清楚的方式是“蓝色文字+下划线”,这是在浏览器发展过程中形成的。这个问题大家都说过很多次了,我也曾经说过。然而,这样的规范却总