栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python画可达鸭】六一儿童节来临,送小朋友大朋友一只可达鸭

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python画可达鸭】六一儿童节来临,送小朋友大朋友一只可达鸭

一年一度的六一儿童节又来了,祝大朋友小朋友节日快乐。你有没有一瞬间,特别想要回到童年?
  
  
  
童年的时光总是安静又美好,我们总是盼望着长大,憧憬着未来的生活。长大后,我们又总是在怀念,怀念过去简单又纯粹的自己。
  
本文主要介绍运用turtle库控制函数绘制可达鸭,希望你会喜欢(希望不要被丑哭臘‍)。
  

  
  
  

一、效果展示

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

本文绘制的可达鸭,是日本任天堂公司发行的掌机游戏系列《宝可梦》中的一种。可达鸭虽然头痛变剧烈时就能使出不可思议的力量,不过当时的记忆却不会留下来。由于怎样也想不起来因此总歪着脖子。

  
  

二、代码详解

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

1 导入库

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

# -*- coding: UTF-8 -*-
'''
代码用途 :画可达鸭
作者     :阿黎逸阳
公众号     :  阿黎逸阳的代码
'''
import os
import pygame
import 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中实现可达鸭的绘制已全部讲解完毕,感兴趣的朋友可以自己实现一遍图片。由于文章是手动转到博客中来的,微信公众号的发送时间一般会早于博客,如果想第一时间获取文章信息,可以关注 “阿黎逸阳的代码”公众号。

  
你可能感兴趣:
用Python绘制皮卡丘
用Python绘制词云图
用Python绘制520永恒心动
Python人脸识别—我的眼里只有你
Python画好看的星空图(唯美的背景)
【Python】情人节表白烟花(带声音和文字)
用Python中的py2neo库操作neo4j,搭建关联图谱
Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)

长按(扫一扫)识别上方二维码学习更多Python和建模知识,让你的学习和工作更出彩。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号