
效果展示
代码详解
2.1 导入库
2.2 播放音乐
2.3 画冰墩墩外轮廓
2.4 画内耳朵
2.5 画眼睛
2.6 画嘴

在正式进入代码讲解之前,先来看下本文的实现效果。

1 导入库
首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。
import pygameimport turtle as t
本文应用到的库较少,只应用了pygame和turtle两个库。
turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。
pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。
2 播放音乐
接着应用pygame库播放背景音乐,本文的音乐是一首关于冰墩墩的歌曲。
#播放音乐pygame.mixer.init()pygame.mixer.music.load(r"F:\公众号\46.画冰墩墩\王溢,崎川 - 2022冬奥冰墩墩(吉祥物).mp3")pygame.mixer.music.set_volume(0.5)pygame.mixer.music.play()
3 画冰墩墩外轮廓
然后进入冰墩墩的正式绘制过程,先画的是外轮廓。
##画外轮廓t.title('冰墩墩')t.pensize(2)#t.color('gray')t.color('lavender')#画耳朵t.penup()t.goto(-50, 160)t.pendown()t.left(120)t.circle(20, 180)#画左边身体t.right(60)t.circle(200, 20)#画左手t.right(30)t.circle(200,15)#t.right(20)t.circle(15, 230)#画左腿t.right(180)t.circle(-150, 40)t.left(80)t.circle(50, 50)#画腿中间的弧度t.left(70)t.circle(-20, 200)#画右腿t.left(90)t.circle(70, 35)t.left(90)t.circle(-80, 25)t.right(30)t.circle(200, 28)#画右手t.right(45)t.circle(200, 15)t.left(20)t.circle(15, 190)t.right(30)t.circle(200, 5)#画右耳朵t.right(150)t.circle(150, 25)t.right(40)t.circle(20, 180)t.right(80)t.circle(600, 5)
关键代码详解:
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.pensize(1)t.penup()t.goto(-53, 158)t.pendown()t.color('gray', 'black')t.begin_fill()t.right(50)t.circle(16, 180)t.end_fill()t.penup()t.goto(35, 142)t.pendown()t.color('gray', 'black')t.begin_fill()t.left(120)t.circle(15, 180)t.end_fill()
关键代码详解:
t.begin_fill():开始填充。
t.end_fill():结束填充。
其他代码在第3小节中已介绍,所以本节只阐述之前未涉及的begin_fill和end_fill函数。
5 画眼睛
本小节介绍画眼睛的代码,为了看起来效果更好,需要注意的是眼睛的对称。
t.penup()t.goto(-40, 100)t.pendown()t.color('black')t.begin_fill()t.circle(15, 170)t.circle(100, 20)t.circle(16, 160)t.left(2)t.circle(100, 19)t.end_fill()t.penup()t.goto(-10, 100)t.pendown()t.color('black')t.begin_fill()t.left(50)t.circle(-15, 170)t.circle(-100, 20)t.circle(-16, 160)t.right(2)t.circle(-100, 19)t.end_fill()t.goto(-8, 95)t.color('white')t.begin_fill()t.circle(-10, 360)t.end_fill()t.goto(-6, 95)t.color('#341c02')t.begin_fill()t.circle(-8, 360)t.end_fill()t.penup()t.goto(-3, 93)t.color('black')t.begin_fill()t.circle(-5, 360)t.end_fill()t.penup()t.goto(-1, 100)t.color('white')t.begin_fill()t.circle(-2.5, 360)t.end_fill()t.penup()t.goto(-43, 95)t.color('white')t.begin_fill()t.right(30)t.circle(10, 360)t.end_fill()t.penup()t.goto(-47, 91)t.color('#341c02')t.begin_fill()t.right(30)t.circle(8, 360)t.end_fill()t.penup()t.goto(-50, 90)t.color('black')t.begin_fill()t.circle(5, 360)t.end_fill()t.penup()t.goto(-52, 98)t.color('white')t.begin_fill()t.circle(2.3, 360)t.end_fill()
6 画嘴
本小节介绍画嘴,本来以为嘴是比较容易画的,但是在绘制过程中出现了一些bug。
经验建议是有填充的地方要沿着一个方向绘制,否则会出现多余的填充。
t.penup()t.goto(-41, 76)t.pendown()t.color('black')t.begin_fill()t.pensize(0.1)t.right(50)t.circle(26, 90)t.penup()t.goto(-8, 75)t.pendown()t.left(170)t.circle(-25,86)t.penup()t.goto(-41, 77)t.pendown()t.penup()t.goto(-41, 76)t.pendown()t.end_fill()t.goto(-21, 70)t.pendown()t.pensize(3)t.end_fill()t.color('#ec2d01')t.left(60)t.circle(-10, 30)
其余代码用到的函数也大致相同,由于篇幅原因,本文不再一一展示。
需要全部代码可以在公众号后台回复"冰墩墩",即可获得完整源代码百度网盘链接。


扫一扫关注我
13162366985
投稿微信号、手机号