Python+Turtle绘制可爱的可达鸭
作者:阿黎逸阳 发布时间:2021-06-18 10:20:29
一年一度的六一儿童节又来了,祝大朋友小朋友节日快乐。
你有没有一瞬间,特别想要回到童年?
童年是一盒水彩笔,五颜六色精彩纷呈。童年是一幅漫画,新奇幻想思绪缤纷。童年是用水彩笔绘出的一幅漫画,一个追风的少年。
童年的时光总是安静又美好,我们总是盼望着长大,憧憬着未来的生活。
长大后,我们又总是在怀念,怀念过去简单又纯粹的自己。
本文主要介绍运用turtle库控制函数绘制可达鸭,希望你会喜欢(希望不要被丑哭)。
一、效果展示
在介绍代码之前,先来看下本文的实现效果。
本文绘制的可达鸭,是日本任天堂公司发行的掌机游戏系列《宝可梦》中的一种。
可达鸭虽然头痛变剧烈时就能使出不可思议的力量,不过当时的记忆却不会留下来。
由于怎样也想不起来因此总歪着脖子。
二、代码详解
python绘制可达鸭的原理是:应用turtle库首先绘制头和身体的外轮廓,然后绘制眼睛、手、脚、头发等不同模块。
1.导入库
首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。
# -*- coding: UTF-8 -*-
'''
代码用途 :画可达鸭
作者 :阿黎逸阳
博客 : https://blog.csdn.net/qq_32532663/article/details/106176609
'''
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()
来源:https://mp.weixin.qq.com/s/OC8SF5NtjJvDPDduBScUJA
猜你喜欢
- 1.C++ 代码Demo.h#pragma oncevoid GeneratorGaussKernel(int ksize, float s
- 首先看下Delphi单元文件基本结构:unit Unit1; //单元文件名 interface //这是接口关键
- 做了一个Python的小项目。利用了一点python的可视化技巧,做出烟花绽放的效果,文章的灵感来自网络上一位大神。一.编译环境Pychar
- 本文实例为大家分享了PHP变量传值赋值和引用赋值变量销毁的具体代码,供大家参考,具体内容如下<?php $a = 100
- 说到 IE 的 bug,一个臭名昭著的例子是它对于“盒模型”的错误解释:在 IE5.x 以及 Quirks 模式的 IE6/7 中,将 bo
- 本文实例讲述了Python解析并读取PDF文件内容的方法。分享给大家供大家参考,具体如下:一、问题描述利用python,去读取pdf文本内容
- 什么是php: PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,
- 一、背景 今天闲着无事,写了一个小小的Python脚本程序,然后给同学炫耀的时候,发现每次都得拉着其他人过来看着自己的电脑屏幕,感觉不是很爽
- 1 为什么找不见外星人为什么我们见不到外星人? 曾经在物理学上有一个著名人物叫费米,大家知道费米是在物理学上发现中子轰击的人,有一个著名的费
- 简介背景Pandas 是 Python 的一个工具库,用于数据分析。由 AQR Capital Management 于 2008 年 4
- 目录 一、环境配置 二、ASP对Excel的基本操作 三、ASP操作Excel生成数据表 四、ASP操作Excel生成Chart图 五、服务
- 最近在做cocos2d-x的简明配置,发现有的朋友的文本编辑器,自动将\r\n截断成\n,(在unix上换行使用\n,windows上,换行
- 最近基于selenium写了一个python小工具,记录下学习记录,自己运行的环境是Ubuntu 14.04.4, Python 2.7,C
- 1、实现的效果示例代码:df=pd.DataFrame({'A':[1,2],'B':[[1,2],[1,2
- 用QQ聊过天的朋友都对它的自动隐藏窗口功能爱不释手,它可以使窗口显得清爽整洁而且富有动感,笔者的几个朋
- 值得学习的地方:1.选择合法索引的方式2.数组转图像显示import numpy as npfrom PIL import Image#in
- 每次和朋友聊天苦于没有表情包,而别人的表情包似乎是取之不尽、用之不竭。作为一个程序员哪能甘愿认输,于是做了一个表情包下载器供大家斗图。首先,
- 导语每日游戏更新系列——今天带大家来看看扫雷小游戏!它是许多人接触到的第一款游戏,大概也是广大办公族和无网学生无聊时消遣的最佳游戏。在那些还
- 本文实例讲述了PHP实现对图片的反色处理功能。分享给大家供大家参考,具体如下:今天有个需求用php对图片进行反色,和转灰,之前不知道可不可行
- 优秀的Stoyan Stefanov在他的新书中(《Javascript Patterns》)介绍了很多编写高质量代码的技巧,比如避免使用全