欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用Python绘制萌萌哒的皮卡丘

程序员文章站 2022-04-12 19:36:01
开发工具 Python版本:3.6.4 相关模块: turtle模块。 环境搭建 安装Python并添加到环境变量即可。 在cmd窗口运行"pikachu.py"文件即可。 效果如下: turtle文档: https://docs.python.org/zh-cn/3.7/library/turtl ......

开发工具

  • python版本:3.6.4
  • 相关模块:
  • turtle模块。

环境搭建

安装python并添加到环境变量即可。

在cmd窗口运行"pikachu.py"文件即可。

效果如下:

利用Python绘制萌萌哒的皮卡丘

 

 

turtle文档:

https://docs.python.org/zh-cn/3.7/library/turtle.html

代码里有不懂的可以参考这个turtle文档~这里只介绍一下用到的:

  • seth:设置朝向;

  • fd:前进;

  • circle:画圆;

  • fillcolor:填充颜色;

  • pensize:画笔粗细;

  • speed:速度;

  • hideturtle:隐藏海龟;

  • screensize:设置屏幕大小;

  • begin_fill:开始填充;

  • end_fill:结束填充;

  • penup:画笔抬起;

  • pendown:画笔落下(此时移动将画线)。

源码

'''
python学习交流群:960410445
'''
import turtle


# 画鼻子
def drawnose():
    turtle.penup()
    turtle.seth(90)
    turtle.fd(100)
    turtle.pendown()
    turtle.begin_fill()
    turtle.fillcolor('black')
    turtle.seth(45)
    turtle.fd(25)
    turtle.seth(135)
    turtle.circle(25, 95)
    turtle.seth(315)
    turtle.fd(25)
    turtle.end_fill()


# 画眼睛
def draweyes(seth, fd, r):
    turtle.penup()
    turtle.seth(seth)
    turtle.fd(fd)
    turtle.pendown()
    turtle.begin_fill()
    turtle.fillcolor('black')
    turtle.circle(50)
    turtle.end_fill()
    turtle.penup()
    turtle.circle(50, r)
    turtle.pendown()
    turtle.begin_fill()
    turtle.fillcolor('white')
    turtle.circle(20)
    turtle.end_fill()


# 画脸
def drawface(seth, fd):
    turtle.penup()
    turtle.seth(seth)
    turtle.fd(fd)
    turtle.pendown()
    turtle.begin_fill()
    turtle.fillcolor('red')
    turtle.circle(70)
    turtle.end_fill()


# 画嘴巴
def drawlip():
    turtle.penup()
    turtle.seth(135)
    turtle.fd(250)
    turtle.pendown()
    turtle.seth(-300)
    turtle.circle(30, -65)
    turtle.begin_fill()
    turtle.fillcolor('firebrick')
    turtle.seth(165)
    turtle.fd(140)
    turtle.seth(195)
    turtle.fd(140)
    turtle.seth(-360)
    turtle.circle(30, -65)
    turtle.penup()
    turtle.seth(-60)
    turtle.circle(30, 65)
    turtle.pendown()
    turtle.seth(-70)
    turtle.fd(240)
    turtle.circle(55, 140)
    turtle.seth(70)
    turtle.fd(240)
    turtle.end_fill()
    turtle.seth(-110)
    turtle.fd(80)
    turtle.begin_fill()
    turtle.fillcolor('firebrick')
    turtle.seth(120)
    turtle.circle(120, 123)
    turtle.seth(-70)
    turtle.fd(165)
    turtle.circle(55, 140)
    turtle.seth(72)
    turtle.fd(165)
    turtle.end_fill()


# 主函数
def main():
    turtle.pensize(4)
    turtle.hideturtle()
    turtle.setup(1000, 600)
    turtle.speed(10)
    turtle.screensize(bg='yellow')
    drawnose()
    draweyes(160, 250, 60)
    draweyes(-9.5, 530, 230)
    drawface(195, 600)
    drawface(-11, 720)
    drawlip()
    turtle.done()


if __name__ == '__main__':
    main()