杨紫和肖战的《余生请多指教》于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:\公众号.余生请多指教\杨紫,肖战 - 余生请多指教 (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)

其余代码用到的函数也大致相同,由于篇幅原因,本文不再一一展示。

以上就是利用python绘制一个可爱的米老鼠的详细内容,更多关于python米老鼠的资料请关注www.887551.com其它相关文章!