利用Python绘制一个可爱的米老鼠
作者:阿黎逸阳 发布时间:2022-01-29 14:34:30
杨紫和肖战的《余生请多指教》于3月15日起腾讯视频全网独播,湖南卫视金鹰独播剧场晚8:20播放。对于杨紫的纯剧粉(战长沙入的坑图片),想要用Python制作一份独特的宣传视频。
一、效果展示
在介绍代码之前,先来看下本文的实现效果。
视频链接 
二、代码详解
python绘制米老鼠的原理是:应用turtle库首先绘制头的外轮廓,然后绘制耳朵、手、衣服、裤子、脚、鞋子等不同模块。  
1.导入库
首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。
import os
import pygame
import turtle as t
本文应用到的库较少,只应用了os、pygame和turtle三个库。os库可以设置文件读取的位置。pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。
2.播放音乐
接着应用pygame库播放背景音乐,本文的音乐是关于《余生请多指教》的歌曲。
#播放音乐
print('播放音乐')
pygame.mixer.init()
pygame.mixer.music.load(r"F:\公众号\49.余生请多指教\杨紫,肖战 - 余生请多指教 (Live).mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(1, 10)
这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。如果选择播放音乐,需要在代码music.load函数中把你想放音乐的地址填进去。 
3.画米老鼠头部外轮廓
然后进入米老鼠的正式绘制过程,先画的是头部外轮廓。
t.title('阿黎逸阳的代码公众号')
t.speed(10)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
##画外轮廓
#画头
print('画头')
t.penup()
t.goto(20, 100)
t.begin_fill()
t.left(90)
t.pendown()
t.color('black')
t.pensize(2)
t.circle(60, 190)
t.left(150)
t.circle(-20, 110)
t.left(170)
t.circle(-35, 100)
t.circle(-15, 100)
t.left(140)
t.circle(-15, 100)
t.circle(-35, 95)
t.left(160)
t.circle(-20, 72)
t.end_fill()
t.left(20)
t.circle(-10, 80)
t.begin_fill()
t.circle(-60, 55)
t.left(60)
t.forward(20)
t.left(130)
t.forward(130)
t.left(120)
t.circle(-60, 30)
t.left(95)
t.forward(65)
t.end_fill()
t.penup()
t.goto(-100, 89)
t.pendown()
t.left(30)
t.circle(20, 60)
t.right(15)
t.circle(60, 30)
t.begin_fill()
#下巴
print('画下巴')
#t.right(30)
t.circle(60, 20)
t.right(30)
t.circle(33, 110)
关键代码详解:
t.pensize(width):设置画笔的尺寸。
t.color(color):设置画笔的颜色。
t.penup():抬起画笔,一般用于另起一个地方绘图使用。
t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。
t.pendown():放下画笔,一般和penup组合使用。
t.left(degree):画笔向左转多少度,括号里表示度数。
t.right(degree):画笔向右转多少度,括号里表示度数。
t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。
画外轮廓的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得米老鼠的轮廓比较流畅。
4.画衣服和耳朵
画完头部外轮廓后就可以分模块画其它组成部分了,本小节画衣服和耳朵。
#上半身
t.backward(5)
t.right(150)
t.forward(18)
#t.left(10)
t.circle(-100, 25)
#衣服下弧线
print('画衣服下弧线')
t.right(50)
t.circle(-75, 63)
t.left(60)
t.circle(100, 30)
t.right(80)
t.circle(-30, 70)
t.circle(-20, 55)
t.forward(70)
t.end_fill()
t.penup()
t.goto(-100, -10)
t.pendown()
t.pensize(1.2)
t.left(175)
#t.pencolor('red')
t.pencolor('white')
t.circle(-30, 30)
#胳肢窝处的线
#1
t.penup()
t.goto(-81, -3)
t.pendown()
t.pensize(1.3)
t.setheading(30)
#t.pencolor('red')
t.pencolor('white')
t.forward(13)
#2
t.penup()
t.goto(-81, -3)
t.pendown()
t.pensize(1.3)
t.setheading(-18)
#t.pencolor('red')
t.pencolor('white')
t.circle(20, 32)
##画耳朵
#画右耳朵
print('画右耳朵')
t.penup()
t.goto(8, 140)
t.pendown()
t.begin_fill()
t.setheading(-10)
t.color('black')
t.circle(30, 160)
t.circle(60, 20)
t.circle(30, 160)
t.end_fill()
#画左耳朵
print('画左耳朵')
t.penup()
t.goto(-90, 130)
t.pendown()
t.begin_fill()
t.setheading(40)
t.color('black')
t.circle(30, 160)
t.circle(60, 20)
t.circle(30, 160)
t.circle(60, 20)
t.end_fill()
5.画眼睛、鼻子、嘴
本小节介绍画眼睛、鼻子、嘴的代码,为了看起来效果更好,需要注意的是眼睛的对称。
#画眼睛
print('画眼睛')
#眼睛下方的线
t.penup()
t.goto(-48, 105)
t.pendown()
t.pensize(1.5)
t.right(17)
t.circle(-40, 42)
#左眼睛
t.penup()
t.goto(-42, 106)
t.pendown()
t.left(160)
t.circle(-30, 50)
t.circle(-7, 180)
t.left(30)
t.circle(-30, 44)
#左眼珠
t.penup()
t.goto(-42, 106)
t.pendown()
t.begin_fill()
t.right(140)
t.circle(30, 20)
t.circle(-4, 180)
#t.left(25)
t.circle(-15, 51)
t.end_fill()
#右眼睛
t.penup()
t.goto(-29, 107)
t.pendown()
t.right(160)
t.circle(-50, 28)
t.circle(-7, 180)
t.left(17)
t.circle(-30, 46)
#右眼珠
t.penup()
t.goto(-29, 107)
t.pendown()
t.begin_fill()
t.right(140)
t.circle(30, 20)
t.circle(-4, 180)
#t.left(25)
t.circle(-15, 51)
t.end_fill()
#画鼻子
print('画鼻子')
t.penup()
t.goto(-42, 102)
t.pendown()
t.begin_fill()
t.setheading(15)
t.circle(-40, 22)
t.circle(-7, 180)
t.circle(40, 20)
t.right(43)
t.circle(-7, 180)
t.end_fill()
#画嘴
print('画嘴')
#上弧线
t.penup()
t.goto(-80, 85)
t.pendown()
t.pensize(1.7)
t.setheading(-45)
t.circle(60, 90)
#嘴
t.begin_fill()
t.penup()
t.goto(-67, 73)
t.pendown()
t.setheading(-70)
t.circle(60, 30)
t.circle(20, 100)
t.right(10)
t.circle(60, 25)
t.setheading(210)
t.circle(-60, 55)
t.end_fill()
#画舌头
print('画舌头')
t.penup()
t.goto(-60, 57)
t.pendown()
t.begin_fill()
t.setheading(40)
t.color('black','pink')
t.circle(-18, 90)
t.setheading(61)
t.circle(-16, 90)
t.setheading(-122)
t.circle(-60, 20)
t.setheading(200)
t.circle(-50, 20)
t.setheading(150)
t.circle(-60, 20)
t.end_fill()
#画笑脸弧度
#左弧度
t.penup()
t.goto(-86, 77)
t.pendown()
t.pensize(1.7)
t.setheading(70)
t.circle(-18, 60)
#右弧度
t.penup()
t.goto(-5, 86)
t.pendown()
t.pensize(1.7)
#t.setheading(10)
t.circle(-18, 60)
print('画下巴')
#画下巴
t.penup()
t.goto(-58, 40)
t.pendown()
t.setheading(140)
t.circle(-60, 10)
#右嘎吱窝
t.penup()
t.goto(-2, 40)
t.pendown()
t.pencolor('white')
t.pensize(1.2)
t.setheading(-90)
t.forward(11)
其余代码用到的函数也大致相同,由于篇幅原因,本文不再一一展示。
来源:https://blog.csdn.net/qq_32532663/article/details/123529527


猜你喜欢
- 导语表妹心疼我,为了逗我开心,教我用Python制作会跳舞的美女。作为新时代的活雷锋,在这里分享给大家。开发工具Python版本:3.6.4
- 解决SQL Server 连接失败的问题最近因学习数据库系统原理,下载安装了一个2019版本的,启动服务后,发现使用Aqua Data St
- 前言激活函数在机器学习中常常用在神经网络隐含层节点与神经网络的输出层节点上,激活函数的作用是赋予神经网络更多的非线性因素,如果不用激励函数,
- 为什么能实现在线编辑呢? 首先需要ie 的支持,在 ie 5.5以后就有一个编辑状态,就是利用这个编辑状态,然后用javascript 来控
- 目录一、前端控制1、在router.js文件(把静态路由和动态路由分别写在router.js)2、store/permission.js(在
- Mac系统上虽然自带PHP和Apache,但是有时不是我们想要的版本呢。今天我们就在macOS Sierra(10.12.1)上安装比较新的
- 刚开始学习tensorflow,还不太会用,开个博记录,今天遇到一个问题是用tf.layers.dense创建的全连接层,如何查看权重?知道
- 概览最近开始在学习mysql相关知识,自己根据学到的知识点,根据自己的理解整理分享出来,本篇文章会分析下一个sql语句在mysql中的执行流
- win7以上需要使用管理员权限操作。# -*- coding: utf-8 -*-import osimport globimport sh
- 前言:Go 语言追求简洁优雅,所以,Go 语言不支持传统的 try…catch…finally
- ------谁正在访问数据库?Select c.sid, c.serial#,c.username,a.object_id,b.
- 本文实例讲述了Thinkphp5.0 框架的请求方式与响应方式。分享给大家供大家参考,具体如下:Thinkphp5.0 的请求方式方法一(使
- 先来看一下最终的效果吧开始聊天,输入消息并点击发送消息就可以开始聊天了点击 “获取后端数据”开启实时推送先来简单了解一下 Django Ch
- debian6系统:首先先安装mysql吧:打开终端(root)用户登入apt-get purge mysql-server-5.5安装完成
- (1)、函数y = sin(x)(2)、数据准备#数据准备X=np.arange(-np.pi,np.pi,1) #定义样本点X,从-pi到
- 1.方法方法描述bbox(item, column=None)返回指定item的框选范围,或者单元格的框选范围column( cid, op
- MySQL插件式存储引擎是MySQL数据库服务器中的组件,负责为数据库执行实际的数据I/O操作,并能允许和强制执行面向特殊应用需求的特定特性
- 前言js是一门弱类型的语言,它的强制类型转换的迷惑性也被人诟病,例如标题提到的一个小例子,我想可能很难再找到其他的语言,允许我们觉到一个值似
- 在这节教程中,我们将探讨PyQt5中的拖放操作。在计算机图形用户界面(GUI)中,拖放是在某个虚拟对象上点击并拖动到另一个位置或虚拟对象上的
- 学一门新技术或者新语言,我们都要首先学会如何去适应这们新技术,其中在适应过程中,我们必须得学习如何调试程序并打出相应的log信息来,正所谓“