首页 > 数据库 > 数据库开发技术 > Python画小蜜蜂
公众号中有个朋友私信我,想要帮忙画下小蜜蜂。

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

本小节会详细解锁如何通过Python中的turtle库绘制小蜜蜂。
1 导入库
首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。
import osimport pygameimport turtle as t
本文应用到的库较少,只应用了os、pygame和turtle三个库。
os库可以设置文件读取的位置。
pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐,如果无需背景音乐,不用加载该库。
turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。
2 播放音乐
接着应用pygame库播放背景音乐,本文的音乐是《 照耀星河》。
###############不需要播放音乐的话这一段可以直接删除###################播放音乐print('播放音乐')pygame.mixer.init()pygame.mixer.music.load(r"F:\公众号\51.小蜜蜂\乐进大将军 - 照耀星河.mp3")pygame.mixer.music.set_volume(0.5)pygame.mixer.music.play(1, 10)print('播放音乐')###############不需要播放音乐的话这一段可以直接删除##################
如果选择播放音乐,需要在代码music.load函数中把你想放音乐的地址填进去。
3 画小蜜蜂的头
然后进入小蜜蜂的正式绘制过程,先画的是头部外轮廓。
t.title('阿黎逸阳的代码公众号')t.speed(1)t.setup(startx=0, starty = 0, width=800, height = 600)#画身体#画头print('画头')t.penup()t.goto(-50, 50)t.pendown()t.begin_fill()t.left(40)t.pendown()t.pensize(2)t.color('black', 'yellow')t.circle(100, 5)t.circle(25, 130)t.circle(30, 70)t.left(60)t.circle(90, 32)t.end_fill()print('画脖子')
关键代码详解:
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函数中的半径和弧度来调节曲线的弧度,从而使得小蜜蜂的轮廓比较流畅。
画完头部外轮廓后就可以分模块画其它组成部分了,本小节画脖子和腹部。
#画脖子t.left(180)t.begin_fill()t.fillcolor('brown')t.circle(-90, 32)t.left(110)t.circle(50, 20)t.setheading(-60)t.circle(80, 28)t.setheading(23)t.circle(60, 22)t.end_fill()print('画腹部')#画腹部t.left(180)t.circle(-60, 20)t.begin_fill()t.fillcolor('yellow')t.circle(-50, 25)t.setheading(140)t.circle(-40, 33)t.setheading(80)t.circle(-40, 20)t.setheading(-60)t.circle(80, 28)t.end_fill()print('画尾巴')
5 全量代码
本小节放上全量绘制小蜜蜂的代码,感兴趣的朋友可以自行调整,绘制别的飞行状态的小蜜蜂。
import osimport pygameimport turtle as tt.title('阿黎逸阳的代码公众号')t.speed(1)t.setup(startx=0, starty = 0, width=800, height = 600)#画身体#画头print('画头')t.penup()t.goto(-50, 50)t.pendown()t.begin_fill()t.left(40)t.pendown()t.pensize(2)t.color('black', 'yellow')t.circle(100, 5)t.circle(25, 130)t.circle(30, 70)t.left(60)t.circle(90, 32)t.end_fill()print('画脖子')#画脖子t.left(180)t.begin_fill()t.fillcolor('brown')t.circle(-90, 32)t.left(110)t.circle(50, 20)t.setheading(-60)t.circle(80, 28)t.setheading(23)t.circle(60, 22)t.end_fill()print('画腹部')#画腹部t.left(180)t.circle(-60, 20)t.begin_fill()t.fillcolor('yellow')t.circle(-50, 25)t.setheading(140)t.circle(-40, 33)t.setheading(80)t.circle(-40, 20)t.setheading(-60)t.circle(80, 28)t.end_fill()print('画尾巴')#画尾巴t.penup()t.goto(-89, 36)t.pendown()t.begin_fill()t.fillcolor('brown')t.setheading(140)t.circle(-40, 31)t.setheading(-120)t.circle(20, 40)t.setheading(-150)t.circle(-50, 14)t.setheading(-45)t.circle(9, 90)t.setheading(-40)t.circle(10, 70)t.end_fill()print('画翅膀')#画翅膀t.penup()t.goto(-88, 83)t.pendown()t.begin_fill()t.fillcolor('LightSkyBlue')t.setheading(100)t.circle(22, 80)t.setheading(-150)t.circle(30, 20)t.setheading(-100)t.circle(20, 107)t.end_fill()t.setheading(-180)t.begin_fill()t.circle(50, 20)t.circle(8, 180)t.circle(30, 15)t.end_fill()print('画眼睛')#画眼睛#左眼睛t.penup()t.goto(-70, 85)t.pendown()t.setheading(30)t.circle(-8, 90)#右眼睛t.penup()t.goto(-52, 82)t.pendown()t.setheading(40)t.circle(-7, 90)print('画嘴巴')#画嘴巴t.penup()t.goto(-62, 75)t.pendown()t.setheading(-50)t.circle(6, 90)print('画耳朵')#画耳朵t.penup()t.goto(-68, 98)t.pendown()t.setheading(110)t.circle(20, 60)t.penup()t.goto(-58, 98)t.pendown()t.setheading(80)t.circle(-18, 80)print('画飞行的阴影')#画飞行的阴影#线1t.penup()t.goto(-100, 105)t.pendown()t.pensize(1.3)t.setheading(50)t.circle(-18, 35)#线2t.penup()t.goto(-97, 104)t.pendown()t.pensize(1.3)t.setheading(40)t.circle(-18, 25)#线3t.penup()t.goto(-120, 70)t.pendown()t.setheading(92)t.circle(-18, 23)#线4t.penup()t.goto(-118, 70)t.pendown()t.setheading(92)t.circle(-18, 10)#线5t.penup()t.goto(-115, 43)t.pendown()t.setheading(115)t.circle(-18, 23)#线6t.penup()t.goto(-113, 46)t.pendown()t.setheading(115)t.circle(-18, 13)t.hideturtle()print('写诗')def write_1(x, y, size, ss):t.hideturtle()t.penup()t.goto(x, y)t.pendown()t.pencolor('black')#t.write('猜谜语', font=('Times New Roman', 12, 'normal'))t.write(ss, font=('Times New Roman', size, 'normal'))def write_2():x = 120y = 150write_1(x, y, 14, '咏蜂 (王锦)')write_1(x-17, y-40, 13, '纷 纷 穿 飞 万 花 间,')write_1(x-17, y-40*2, 13, '终 生 未 得 半 日 闲。')write_1(x-17, y-40*3, 13, '世 人 都 夸 蜜 味 好,')write_1(x-17, y-40*4, 13, '釜 底 添 薪 有 谁 怜。')write_2()write_2()write_2()write_2()write_2()write_2()write_2()write_2()write_2()write_2()print('绘图完毕')
本文至此已全部介绍完毕,如有疑问也可以到“阿黎逸阳的代码”公众号中咨询。


扫一扫关注我
13162366985
投稿微信号、手机号
原文作者:阿黎逸阳的代码;如有侵权,请联系管理员删除。