脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 利用Python绘制一个可爱的米老鼠

利用Python绘制一个可爱的米老鼠

2022-11-04 10:20阿黎逸阳 Python

turtle库是一个点线面的简单图像库,在Python2.6之后被引入进来,能够完成一些比较简单的几何图像可视化。本文将利用turtle绘制一个可爱的米老鼠,感兴趣的可以试一试

杨紫和肖战的《余生请多指教》于3月15日起腾讯视频全网独播,湖南卫视金鹰独播剧场晚8:20播放。对于杨紫的纯剧粉(战长沙入的坑图片),想要用Python制作一份独特的宣传视频。

一、效果展示

在介绍代码之前,先来看下本文的实现效果。

视频链接

二、代码详解

python绘制米老鼠的原理是:应用turtle库首先绘制头的外轮廓,然后绘制耳朵、手、衣服、裤子、脚、鞋子等不同模块。  

1.导入库

首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。

?
1
2
3
import os
import pygame
import turtle as t

本文应用到的库较少,只应用了os、pygame和turtle三个库。os库可以设置文件读取的位置。pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。

2.播放音乐

接着应用pygame库播放背景音乐,本文的音乐是关于《余生请多指教》的歌曲。

?
1
2
3
4
5
6
#播放音乐
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.画米老鼠头部外轮廓

然后进入米老鼠的正式绘制过程,先画的是头部外轮廓。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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.画衣服和耳朵

画完头部外轮廓后就可以分模块画其它组成部分了,本小节画衣服和耳朵。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#上半身
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.画眼睛、鼻子、嘴

本小节介绍画眼睛、鼻子、嘴的代码,为了看起来效果更好,需要注意的是眼睛的对称。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#画眼睛
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米老鼠的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/qq_32532663/article/details/123529527

延伸 · 阅读

精彩推荐
  • Pythonnumpy找出array中的最大值,最小值实例

    numpy找出array中的最大值,最小值实例

    下面小编就为大家分享一篇numpy找出array中的最大值,最小值实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    月落乌啼silence24412021-01-27
  • Pythonpython编程普通及类和静态方法示例详解

    python编程普通及类和静态方法示例详解

    普通方法会将实例传入方法当中(通常用self表示),类方法会将类传入方法当中(通常用cls表示),静态方法中传入与类无关的变量。下面将举例详细说明...

    zjuPeco9822022-01-23
  • PythonPyQt5每天必学之事件与信号

    PyQt5每天必学之事件与信号

    这篇文章主要为大家详细介绍了PyQt5每天必学之事件与信号的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    我的世界你曾经来过13622021-02-03
  • PythonPython实现大数据收集至excel的思路详解

    Python实现大数据收集至excel的思路详解

    这篇文章主要介绍了Python实现大数据收集至excel的思路,本文通过完整代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 ...

    weixin_417543096222020-05-11
  • Pythonpython3+PyQt5+Qt Designer实现扩展对话框

    python3+PyQt5+Qt Designer实现扩展对话框

    这篇文章主要为大家详细介绍了python3+PyQt5+Qt Designer实现扩展对话框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    basisworker8872021-02-03
  • PythonPyQt5图形界面播放音乐的实例

    PyQt5图形界面播放音乐的实例

    今天小编就为大家分享一篇PyQt5图形界面播放音乐的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    huazwz4492021-07-13
  • PythonPython实现的生产者、消费者问题完整实例

    Python实现的生产者、消费者问题完整实例

    这篇文章主要介绍了Python实现的生产者、消费者问题,简单描述了生产者、消费者问题的概念、原理,并结合完整实例形式分析了Python实现生产者、消费者问...

    shw80011302021-02-27
  • Python在Python中封装GObject模块进行图形化程序编程的教程

    在Python中封装GObject模块进行图形化程序编程的教程

    这篇文章主要介绍了在Python中封装GObject模块进行图形化程序编程的教程,本文来自于IBM官方网站技术文档,需要的朋友可以参考下 ...

    脚本之家1942020-06-06