用Python的turtle库绘制皮卡丘

2024-03-20 17:04
文章标签 python 绘制 turtle 皮卡丘

本文主要是介绍用Python的turtle库绘制皮卡丘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

turtle库的简介

turtle(海龟)库是turtle绘图体系的python实现,turtle库是一种标准库,是python自带的。

turtle(海龟)是一种真实的存在,有一个海龟在窗口的正中心,在画布上游走,走过的轨迹形成了绘制的图形,海龟由程序控制,可改变颜色,宽度等。

turtle库的安装

pip install turtle

运行效果图片

在这里插入图片描述

turtle库绘制皮卡丘代码


import turtledef getPosition(x, y):turtle.setx(x)turtle.sety(y)print(x, y)class Pikachu:def __init__(self):self.t = turtle.Turtle()t = self.tt.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.tt.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.tt.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.tt.fillcolor('#88141D')t.begin_fill()# 下嘴唇l1 = []l2 = []t.seth(190)a = 0.7for i in range(28):a += 0.1t.right(3)t.fd(a)l1.append(t.position())self.noTrace_goto(x, y)t.seth(10)a = 0.7for i in range(28):a += 0.1t.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.tself.noTrace_goto(x, y)t.seth(300)t.fillcolor('#DD4D28')t.begin_fill()a = 2.3for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a -= 0.05t.lt(3)t.fd(a)else:a += 0.05t.lt(3)t.fd(a)t.end_fill()turtle.tracer(True)def rightCheek(self, x, y):t = self.tturtle.tracer(False)self.noTrace_goto(x, y)t.seth(60)t.fillcolor('#DD4D28')t.begin_fill()a = 2.3for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a -= 0.05t.lt(3)t.fd(a)else:a += 0.05t.lt(3)t.fd(a)t.end_fill()turtle.tracer(True)def colorLeftEar(self, x, y):t = self.tself.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.tself.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.tt.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.tt.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()

这篇关于用Python的turtle库绘制皮卡丘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/830158

相关文章

Python 字符串占位

在Python中,可以使用字符串的格式化方法来实现字符串的占位。常见的方法有百分号操作符 % 以及 str.format() 方法 百分号操作符 % name = "张三"age = 20message = "我叫%s,今年%d岁。" % (name, age)print(message) # 我叫张三,今年20岁。 str.format() 方法 name = "张三"age

以canvas方式绘制粒子背景效果,感觉还可以

这个是看到项目中别人写好的,感觉这种写法效果还可以,就存留记录下 就是这种的背景效果。如果想改背景颜色可以通过canvas.js文件中的fillStyle值改。 附上demo下载地址。 https://download.csdn.net/download/u012138137/11249872

一道经典Python程序样例带你飞速掌握Python的字典和列表

Python中的列表(list)和字典(dict)是两种常用的数据结构,它们在数据组织和存储方面有很大的不同。 列表(List) 列表是Python中的一种有序集合,可以随时添加和删除其中的元素。列表中的元素可以是任何数据类型,包括数字、字符串、其他列表等。列表使用方括号[]表示,元素之间用逗号,分隔。 定义和使用 # 定义一个列表 fruits = ['apple', 'banana

Python应用开发——30天学习Streamlit Python包进行APP的构建(9)

st.area_chart 显示区域图。 这是围绕 st.altair_chart 的语法糖。主要区别在于该命令使用数据自身的列和指数来计算图表的 Altair 规格。因此,在许多 "只需绘制此图 "的情况下,该命令更易于使用,但可定制性较差。 如果 st.area_chart 无法正确猜测数据规格,请尝试使用 st.altair_chart 指定所需的图表。 Function signa

python实现最简单循环神经网络(RNNs)

Recurrent Neural Networks(RNNs) 的模型: 上图中红色部分是输入向量。文本、单词、数据都是输入,在网络里都以向量的形式进行表示。 绿色部分是隐藏向量。是加工处理过程。 蓝色部分是输出向量。 python代码表示如下: rnn = RNN()y = rnn.step(x) # x为输入向量,y为输出向量 RNNs神经网络由神经元组成, python

python 喷泉码

因为要完成毕业设计,毕业设计做的是数据分发与传输的东西。在网络中数据容易丢失,所以我用fountain code做所发送数据包的数据恢复。fountain code属于有限域编码的一部分,有很广泛的应用。 我们日常生活中使用的二维码,就用到foutain code做数据恢复。你遮住二维码的四分之一,用手机的相机也照样能识别。你遮住的四分之一就相当于丢失的数据包。 为了实现并理解foutain

python 点滴学

1 python 里面tuple是无法改变的 tuple = (1,),计算tuple里面只有一个元素,也要加上逗号 2  1 毕业论文改 2 leetcode第一题做出来

Python爬虫-贝壳新房

前言 本文是该专栏的第32篇,后面会持续分享python爬虫干货知识,记得关注。 本文以某房网为例,如下图所示,采集对应城市的新房房源数据。具体实现思路和详细逻辑,笔者将在正文结合完整代码进行详细介绍。接下来,跟着笔者直接往下看正文详细内容。(附带完整代码) 正文 地址:aHR0cHM6Ly93aC5mYW5nLmtlLmNvbS9sb3VwYW4v 目标:采集对应城市的

python 在pycharm下能导入外面的模块,到terminal下就不能导入

项目结构如下,在ic2ctw.py 中导入util,在pycharm下不报错,但是到terminal下运行报错  File "deal_data/ic2ctw.py", line 3, in <module>     import util 解决方案: 暂时方案:在终端下:export PYTHONPATH=/Users/fujingling/PycharmProjects/PSENe

将一维机械振动信号构造为训练集和测试集(Python)

从如下链接中下载轴承数据集。 https://www.sciencedirect.com/science/article/pii/S2352340918314124 import numpy as npimport scipy.io as sioimport matplotlib.pyplot as pltimport statistics as statsimport pandas