Python+Turtle绘制可爱的可达鸭

Felix
Felix
发布时间:2026-10-15| 6人浏览过
原创

本文详细介绍使用Python的turtle库绘制卡通形象可达鸭的全过程。从环境配置、库导入到播放背景音乐,再到分模块绘制头部、身体、眼睛、手脚及头发,逐步解析关键代码逻辑。通过调节画笔颜色、尺寸及circle函数的半径与弧度参数,实现流畅轮廓与生动细节,适合Python初学者练习图形编程。

六一儿童节如期而至,祝愿所有大朋友和小朋友节日快乐。

你是否曾在某个瞬间,强烈地渴望回到无忧无虑的童年时光?

童年如同装满五颜六色水彩笔的盒子,色彩斑斓且充满精彩;它也是一幅漫画,承载着新奇幻想与纷飞的思绪。童年就是用这些水彩笔绘就的漫画,描绘出一个追逐风少年的身影。

童年的岁月总是宁静而美好,我们那时总盼望着长大,憧憬着未来的生活。

然而,成年后的我们往往在怀念过去,怀念那个曾经简单而纯粹的自己。

本文将主要介绍如何利用Python的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()

以上就是Python+Turtle绘制可爱可达鸭的详细内容,更多关于Python Turtle绘制可达鸭的资料请关注本站其它相关文章!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。