当前位置: 首页 > news >正文

简单代码画皮卡丘_超酷!用 Python 教你绘制皮卡丘和哆啦A梦

0ca66a483481af56c54bbad71d00dce9.png

7d3c6342249100f4b4876b1f9c48d0f5.png

本文利用 Python 绘制两个卡通角色,并带大家熟悉一下绘图程序包 turtle 的一些用法,先看一下 最终皮卡丘的绘制效果

v2-9dedc66eafa6871656f4561a83779158_b.gif

在使用之前请确保 turtle 已经安装成功,第一次安装 turtle 时,用的是 pip 安装包,但总会出现下面的错误,

经查阅资料找到了一种可行的解决方法:

d0ad132b30e96505589925750425dcbf.png
  • 1,从网上下载 turtle 程序包,下载链接为:https://files.pythonhosted.org/packages/ff/f0/21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f/turtle-0.0.2.tar.gz
  • 2,解压 1 中的压缩包,修改其中的 setup.py 脚本:修改方法在40行处的 ValueError,ve ;

0e7410555286ff1c00ad6dc3737540e1.png

except ValueError, ve:
改为
except (ValueError, ve):

  • 3, 使用 pip 命令对本地下载文件进行安装
    pip install C:UsersXXXDesktopturtle-0.0.2

2, turtle 命令介绍

turtle 绘图时有个特点,代码行数看起来比较大,但用到的也就是命令操作,来回使用;这里我提前把 turtle 几个常用的命令罗列出来,并在旁边简单介绍一下其用法,方便大家的学习:

Func(函数)Para type(参数类型)Desc(描述)

: func 中换行表示或的意思,每种函数名有它的缩写,所以存在函数 有2-3 种 不同写法,但函数功能是一样的, - 表示无参数;

... 关于 turtle 相关方法和类还有很多,这里列出来的只是常用的一部分,有兴趣的可以仔细去看一下官网文档,链接为:https://docs.python.org/3.7/library/turtle.html?highlight=screensize#turtle.seth

3,绘制皮卡丘

2115956936b56b9d6e7fd6691ba3f0b2.png
录制_2020_08_01_22_15_39_710.gif

代码部分

'''
绘制皮卡丘
'''

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 onTrace_goto(self,x,y):
        self.t.penup()
        self.t.goto(x,y)
        self.t.pendown()

    def leftEye(self,x,y):
        self.onTrace_goto(x,y)
        t = self.t
        t.seth(0)
        t.fillcolor('#333333')
        t.begin_fill()
        t.circle(22)
        t.end_fill()

        self.onTrace_goto(x,y+10)
        t.fillcolor('#000000')
        t.begin_fill()
        t.circle(10)
        t.end_fill()

        self.onTrace_goto(x+6,y+22)
        t.fillcolor('#ffffff')
        t.begin_fill()
        t.circle(10)
        t.end_fill()

    def rightEye(self,x,y):
        self.onTrace_goto(x,y)
        t = self.t
        t.seth(0)
        t.fillcolor('#333333')
        t.begin_fill()
        t.circle(22)
        t.end_fill()

        self.onTrace_goto(x,y+10)
        t.fillcolor('#000000')
        t.begin_fill()
        t.circle(10)
        t.end_fill()

        self.onTrace_goto(x-6,y+22)
        t.fillcolor('#ffffff')
        t.begin_fill()
        t.circle(10)
        t.end_fill()


    def mouth(self,x,y):
        self.onTrace_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.onTrace_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.onTrace_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.onTrace_goto(-17,94)
        t.seth(8)
        t.fd(4)
        t.back(8)


    # 红脸颊

    def leftCheck(self,x,y):
        turtle.tracer(False)
        t = self.t
        self.onTrace_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 rightCheck(self,x,y):
        t = self.t
        turtle.tracer(False)
        self.onTrace_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.onTrace_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.onTrace_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.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)

        # 左脸轮廓
        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.seth(300)
        t.circle(10,80)
        t.seth(220)
        t.seth(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(110,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.onTrace_goto(130,125)
        t.seth(-20)
        t.fd(5)
        t.circle(-5,160)
        t.fd(5)


        # 手指纹
        self.onTrace_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.onTrace_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)

        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.end_fill()


        # 尾巴花纹
        t.fillcolor('#923E24')
        self.onTrace_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.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(30)
        t.left(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.leftCheck(-126,32)
        self.rightCheck(107,63)
        self.colorLeftEar(-250,100)
        self.colorRightEar(150,270)
        self.leftEye(-85,90)
        self.rightEye(50,110)
        t.hideturtle()

    def cap(self,x,y):
        self.onTrace_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(60)
        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)
        t.right(35)
        t.fd(50)
        t.right(35)
        t.fd(50)
        t.circle(8,100)
        t.end_fill()
        self.onTrace_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.onTrace_goto(-58,270)
        t.pencolor('#228B22')
        t.dot(35)

        self.onTrace_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.fillcolor('#228B22')


    def start(self):
        self.body()

def main():
    print(" Painting the Pikachu....")
    turtle.screensize(800,600)
    turtle.title("Pickachu")
    pickachu = Pikachu()
    pickachu.start()

    turtle.mainloop() # running


if __name__ =='__main__':
    main()

4,绘制 哆啦A梦

代码部分

#五轨迹跳跃
def my_goto(x,y):
    penup()
    goto(x,y)
    pendown()

def eyes():
    fillcolor('#ffffff')
    begin_fill()

    tracer(False)
    a = 2.5
    for i in range(120):
        if 0<= i<30 or 60 <= i <90:
            a -= 0.05
            lt(3)
            fd(a)
        else:
            a += 0.05
            lt(3)
            fd(a)
    tracer(True)
    end_fill()


#胡须
def beard():
    my_goto(-32,135)
    seth(165)
    fd(60)

    my_goto(-32,125)
    seth(180)
    fd(60)

    my_goto(-32,115)
    seth(193)
    fd(60)

    my_goto(37,135)
    seth(15)
    fd(60)

    my_goto(37,125)
    seth(0)
    fd(60)

    my_goto(37,115)
    seth(-13)
    fd(60)


#嘴巴
def mouth():
    my_goto(5,148)
    seth(270)
    fd(100)
    seth(0)
    circle(120,50)
    seth(230)
    circle(-120,100)

#围巾
def scarf():
    fillcolor('#e70010')
    begin_fill()
    seth(0)
    fd(200)
    circle(-5,90)
    fd(10)
    circle(-5,90)
    fd(207)
    circle(-5,90)
    fd(10)
    circle(-5,90)
    end_fill()


#鼻子
def nose():
    my_goto(-10,158)
    seth(315)
    fillcolor('#e70010')
    begin_fill()
    circle(20)
    end_fill()


#黑眼睛
def black_eyes():
    seth(0)
    my_goto(-20,195)
    fillcolor('#000000')
    begin_fill()
    circle(13)
    end_fill()

    pensize(6)
    my_goto(20,205)
    seth(75)
    circle(-10,150)
    pensize(3)

    my_goto(-17,200)
    seth(0)
    fillcolor('#ffffff')
    begin_fill()
    circle(5)
    end_fill()
    my_goto(0,0)


def face():

    fd(183)
    lt(45)
    fillcolor('#ffffff')
    begin_fill()
    circle(120,100)
    seth(180)
    fd(121)
    pendown()
    seth(215)
    circle(120,100)
    end_fill()
    my_goto(63.56,218.24)
    seth(90)
    eyes()
    seth(180)
    penup()
    fd(60)
    pendown()
    seth(90)
    eyes()
    penup()
    seth(180)
    fd(64)



def head():
    penup()
    circle(150,40)
    pendown()
    fillcolor('#00a0de')
    begin_fill()
    circle(150,280)
    end_fill()


# body
def Doraemon():

    head()

    scarf()

    face()

    nose()

    mouth()

    beard()

    my_goto(0,0)
    seth(0)
    penup()
    circle(150,50)
    pendown()
    seth(30)
    fd(40)
    seth(70)
    circle(-30,270)

    fillcolor('#00a0de')
    begin_fill()

    seth(230)
    fd(80)
    seth(90)
    circle(1000,1)
    seth(-89)
    circle(-1000,10)

    seth(180)
    fd(70)
    seth(90)
    circle(30,180)
    seth(180)
    fd(70)

    seth(100)
    circle(-1000,9)


    seth(-86)
    circle(1000,2)
    seth(230)
    fd(40)


    circle(-30,230)
    seth(45)
    fd(81)
    seth(0)
    fd(203)
    circle(5,90)
    fd(10)
    circle(5,90)
    fd(7)
    seth(40)
    circle(150,10)
    seth(30)
    fd(40)
    end_fill()

    seth(70)
    fillcolor('#ffffff')
    begin_fill()
    circle(-30)
    end_fill()


    my_goto(103.74,-182.59)
    seth(0)
    fillcolor('#ffffff')
    begin_fill()
    fd(15)
    circle(-15,180)
    fd(90)
    circle(-15,180)
    fd(10)
    end_fill()


    #右手
    my_goto(-103.42,15.09)
    seth(0)
    fd(38)
    seth(230)
    begin_fill()
    circle(90,260)
    end_fill()

    my_goto(5,-40)
    seth(0)
    fd(70)
    seth(-90)
    circle(-70,180)
    seth(0)
    fd(70)

    #铃铛
    my_goto(-103.42,15.09)
    fd(90)
    seth(70)
    fillcolor('#ffd200')
    begin_fill()
    circle(-20)
    end_fill()
    seth(170)
    fillcolor('#ffd200')
    begin_fill()
    circle(-2,180)
    seth(10)
    circle(-100,22)
    circle(-2,180)
    seth(180-10)
    circle(100,22)
    end_fill()
    goto(-13.42,15.09)
    seth(250)
    circle(20,110)
    seth(90)
    fd(15)
    dot(10)

    my_goto(0,-150)

    #画眼睛
    black_eyes()


if __name__ =='__main__':
    screensize(800,600,'#f0f0f0')
    pensize(3) # size of pen
    speed(9) # speed of pen
    Doraemon()
    my_goto(100,-300)
    write("by zeroing",font = ("Bladley Hand ITC",30,'bold'))
    mainloop()

从上面的代码行数应该了解一点, turtle 最难的地方不是它的用法,而是使用者对于整个物体的几何整体塑造,必须全局把握地非常准,否则最终很难绘制出比较满意的图案

感兴趣的同学可以直接把上面代码复制到自己的 IDE 中,直接运行就可以,以上就是本篇文章的所有内容啦,最后感谢大家的阅读!

相关文章:

  • 分析函数hive计算均值_Hive第六天——Hive函数(开窗函数之累计统计)
  • 蓝卡攻略_剑与远征:4.18版本的新手攻略,崛起的三巨头
  • 关抢占 自旋锁_Linux学习第28节,什么是自旋锁?内核是如何设计,如何实现它的...
  • 2019pro与air怎么选_iPad Air 2019 VS iPad Pro 10.5 | 普通人的角度简单思考
  • mysql安装教程与启动_MySql安装启动两种方法教程详解
  • apparmor mysql_Ubuntu 上更改 MySQL 数据库数据存储目录
  • mysql工程师需要会哪些_MySQL面试高频100问(工程师方向)
  • mysql 客户端 连接数_监控mysql上客户端的连接数
  • mysql带库名查询_MySQL优化
  • docker mysql 差8小时_docker之容器日志输出与系统时间相差8小时解决办法
  • java白盒测试问题_白盒测试项目实践经验总结(三)-返回码问题
  • 9点到17点半 cron_定时任务Quartz简单配置与cron表达式
  • python3 web服务器_python3实现微型的web服务器
  • python if else简写_python代码简写(推导式 if else for in)
  • mysql的终端窗口是什么意思_什么叫终端窗口
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • Android Studio:GIT提交项目到远程仓库
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • create-react-app项目添加less配置
  • JSONP原理
  • Linux链接文件
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • node.js
  • SpiderData 2019年2月23日 DApp数据排行榜
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • TypeScript迭代器
  • 动态规划入门(以爬楼梯为例)
  • 后端_MYSQL
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 微信小程序:实现悬浮返回和分享按钮
  • 微信支付JSAPI,实测!终极方案
  • 线性表及其算法(java实现)
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • ionic异常记录
  • ​卜东波研究员:高观点下的少儿计算思维
  • #前后端分离# 头条发布系统
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (1)虚拟机的安装与使用,linux系统安装
  • (solr系列:一)使用tomcat部署solr服务
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (利用IDEA+Maven)定制属于自己的jar包
  • (顺序)容器的好伴侣 --- 容器适配器
  • (转载)CentOS查看系统信息|CentOS查看命令
  • *2 echo、printf、mkdir命令的应用
  • .NET Remoting学习笔记(三)信道
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • .net网站发布-允许更新此预编译站点
  • .NET应用架构设计:原则、模式与实践 目录预览
  • ;号自动换行