ITPub博客

首页 > 数据库 > 数据库开发技术 > 六一儿童节来临,送大朋友小朋友一只最近爆火的“可达鸭”

六一儿童节来临,送大朋友小朋友一只最近爆火的“可达鸭”

原创 数据库开发技术 作者:阿黎逸阳的代码 时间:2022-05-29 07:00:00 0 删除 编辑

一年一度的六一儿童节又来了,祝大朋友小朋友节日快乐🎈。

你有没有一瞬间,特别想要回到童年?




回到童年

保持童心,永远热爱

童年是一盒水彩笔,五颜六色精彩纷呈。童年是一幅漫画,新奇幻想思绪缤纷。童年是用水彩笔绘出的一幅漫画,一个追风的少年。

童年的时光总是安静又美好,我们总是盼望着长大,憧憬着未来的生活。
长大后,我们又总是在怀念,怀念过去简单又纯粹的自己。
本文主要介绍运用turtle库控制函数绘制可达鸭,希望你会喜欢(希望不要被丑哭)。
本文目录
  1. 效果展示

  2. 代码详解

    2.1 导入库

    2.2 播放音乐

    2.3 画可达鸭的头和身体外轮廓

    2.4 画眼睛

    2.5 画手

    2.6 画嘴和脚

    2.7 画头发

    2.8 写文字


一、效果展示

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

本文绘制的可达鸭,是日本任天堂公司发行的掌机游戏系列《宝可梦》中的一种。

可达鸭虽然头痛变剧烈时就能使出不可思议的力量,不过当时的记忆却不会留下来。

由于怎样也想不起来因此总歪着脖子。


二、代码详解
python绘制可达鸭的原理是:应用turtle库首先绘制头和身体的外轮廓,然后绘制眼睛、手、脚、头发等不同模块。

 1   导入库

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

# -*- coding: UTF-8 -*-'''代码用途 :画可达鸭作者     :阿黎逸阳博客     :  https://blog.csdn.net/qq_32532663/article/details/106176609'''import osimport pygameimport turtle as t from time import sleep

本文应用到的库较少,只应用了os、pygame、turtle和time四个库。

os库可以设置文件读取的位置。

pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。

turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。

time库是时间管理库,本文用来设置绘图的暂停时间。


 2   播放音乐

接着应用pygame库播放背景音乐,本文的音乐是《 瞬间的永恒》。

#播放音乐print('播放音乐')pygame.mixer.init()pygame.mixer.music.load(r"F:\公众号\53.可达鸭\赵海洋 - 《瞬间的永恒》夜色钢琴曲.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)t.penup()t.goto(150, 50)t.pendown()t.color('#FFCC00')t.pendsize(1)t.color('yellow')t.begin_fill()t.setheading(30)#画头print('画头')t.circle(40, 70)t.circle(60, 160)t.circle(40, 70)#画身体print('画身体')t.setheading(-150)t.circle(100, 30)t.circle(70, 90)t.circle(100, 60)t.circle(70, 90)t.circle(100, 30)t.end_fill()

关键代码详解:

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   画眼睛

画完头和身体外轮廓后就可以分模块画其它组成部分了,本小节画眼睛。

#画左眼睛print('画左眼睛')t.penup()t.goto(85, 95)t.pendown()t.color('white')t.begin_fill()t.setheading(20)t.pensize(3)t.circle(15, 70)t.setheading(120)t.circle(15, 50)t.left(10)t.circle(20, 100)t.left(10)t.circle(8, 90)t.circle(40, 30)t.end_fill()t.penup()t.goto(80, 102)t.pendown()t.color('black')t.begin_fill()t.setheading(0)t.pensize(2)t.circle(1.5, 360)t.end_fill()#画右眼睛print('画右眼睛')t.penup()t.goto(135, 95)t.pendown()t.color('white')t.begin_fill()t.setheading(160)t.pensize(3)t.circle(-15, 70)t.setheading(60)t.circle(-15, 50)t.right(10)t.circle(-20, 100)t.right(10)t.circle(-8, 90)t.circle(-40, 30)t.end_fill()t.penup()t.goto(140, 102)t.pendown()t.color('black')t.begin_fill()t.setheading(0)t.pensize(2)t.circle(1.5, 360)t.end_fill()


 5   画手

本小节介绍画手代码,为了看起来效果更好,需要注意的是手的对称。

#画左手print('画左手')t.penup()t.goto(60, 42)t.pendown()t.color('#FFCC00')#t.color('yellow')t.begin_fill()t.setheading(160)t.circle(20, 50)t.right(30)t.circle(90, 50)t.forward(5)t.circle(5, 180)t.setheading(-120)t.forward(5)t.circle(6, 180)t.right(20)t.circle(-90, 50)t.end_fill()#画右手print('画右手')t.penup()t.goto(160, 42)t.pendown()t.color('#FFCC00')#t.color('yellow')t.begin_fill()t.setheading(-15)t.circle(10, 80)t.right(18)t.circle(80, 42)t.circle(-5, 180)t.setheading(80)t.forward(1)t.circle(-6, 180)t.setheading(80)t.circle(-5, 180)t.left(10)t.circle(-90, 40)t.circle(-80, 30)t.end_fill()


 6   画嘴和脚

本小节介绍画嘴和脚代码。

#画嘴巴print('画嘴巴')t.penup()t.goto(114, 85)t.pendown()t.color('#FFFF99')#t.color('#FFCC00')#t.color('black')t.begin_fill()t.setheading(180)t.forward(6)t.setheading(-130)t.circle(-50, 40)t.setheading(-70)t.circle(-50, 50)t.circle(20, 90)t.circle(50, 60)t.circle(20, 90)t.right(15)t.circle(-50, 50)t.left(120)t.circle(-50, 47)t.end_fill()#画左脚print('画左脚')t.penup()t.goto(55, -78)t.pendown()t.color('#FFFF99')t.begin_fill()t.setheading(-115)t.forward(40)t.setheading(50)t.circle(-12, 160)t.setheading(70)t.circle(-10, 160)t.setheading(85)t.forward(39)t.setheading(170)t.circle(-100, 18)t.end_fill()#画右脚print('画右脚')t.penup()t.goto(113, -97)t.pendown()t.color('#FFFF99')t.begin_fill()t.setheading(-100)t.forward(35)t.setheading(60)t.circle(-10, 160)t.setheading(80)t.circle(-10, 160)t.setheading(90)t.forward(47)t.setheading(197)t.circle(-100, 18)t.end_fill()


 7   画头发

本小节介绍画头发代码。

#画头发print('画头发')#第一根头发t.penup()t.goto(92, 138)t.pendown()t.color('black')t.begin_fill()t.pensize(1)t.setheading(115)t.circle(-100, 15)t.setheading(20)t.circle(80, 6)t.setheading(-76)t.circle(100, 16)t.setheading(180)t.circle(30, 20)t.end_fill()#第二根头发t.penup()t.goto(106, 140)t.pendown()t.color('black')t.begin_fill()t.pensize(1)t.setheading(105)t.circle(-100, 18)t.setheading(10)t.circle(80, 7)t.setheading(-92)t.circle(100, 20)t.setheading(160)t.circle(30, 16)t.end_fill()#第三根头发t.penup()t.goto(123, 138)t.pendown()t.color('black')t.begin_fill()t.pensize(1)t.setheading(75)t.circle(100, 17)t.setheading(-5)t.circle(80, 7)t.setheading(-100)t.circle(120, 15)t.setheading(160)t.circle(30, 20)t.end_fill()

‍


 8   写文字

最后介绍写会动文字的代码。

#写文字print('写文字')def write_1(x, y,  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', 20, 'normal'))def write_all():    write_1(-130, 120,  '节')    write_1(-130, 80,  '日')    write_1(-130, 40,  '快')    write_1(-130, 0,  '乐')    write_1(-130, -40,  '!')def undos():    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()    t.undo()while 1:    write_all()    sleep(1)    undos()

注:如想获得本文全量直接可执行的代码,可在本公众号中回复”可达鸭“,即可免费获取。

至此,在Python中实现可达鸭的绘制已全部讲解完毕,感兴趣的朋友可以自己实现一遍。
往期回顾:

情人节表白烟花

520永恒的心动

520表白代码合集
黑客帝国中的代码雨
用python绘制皮卡丘
娱乐圈排行榜动态条形图绘制
Python人脸识别—我的眼里只有你
3D星空图V2版——添加背景图片和音乐



扫一扫关注我

13162366985

投稿微信号、手机号

原文作者:阿黎逸阳的代码;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2020-02-15

  • 博文量
    282
  • 访问量
    130908