用python 的turtle库画哆啦A梦和皮卡丘顺便加个背景音乐

2023-12-16 18:10

本文主要是介绍用python 的turtle库画哆啦A梦和皮卡丘顺便加个背景音乐,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

python 的turtle库画哆啦A梦和皮卡丘顺便加个背景音乐
1、插入背景音乐需要下面的代码以及还要安装pygame库,安装的具体方法白度,完整的代码在下面。
在这里插入图片描述

import pygame
file=r'D:\新建文件夹\瓢三爷的小喇叭 - 哆啦A梦主题曲-唢呐改编版(翻自 東京プリン).mp3'
pygame.mixer.init()
track=pygame.mixer.music.load(file)
pygame.mixer.music.play()

2、下面的是完整的代码,导入背景音乐需要安装pygame库

from turtle import *
import pygame
file=r'D:\新建文件夹\瓢三爷的小喇叭 - 哆啦A梦主题曲-唢呐改编版(翻自 東京プリン).mp3'
pygame.mixer.init()
track=pygame.mixer.music.load(file)
pygame.mixer.music.play()
bgcolor(1,0.5,1)
pencolor('white')
penup()
goto(-220,50)
pendown()
write('加载背景音乐',font=('Arial Narrow',20,'italic'))
penup()
goto(-180,-80)
circle(5,5400)
pendown()
write('下面开始画了',font=('Arial Narrow',50))
penup()
circle(20,720)
pendown()
bgcolor('white')
reset()
#--------------------------------from turtle import *
pensize(3)
speed(1)
#画笔跳跃函数块
def my_goto(x,y):penup()goto(x,y)pendown()pass
#画鼻子
#眼睛
def eyes():a=2.5fillcolor('white')begin_fill()tracer(False)for i in range(120):if 0<=i<30 or 60<=i<90:a-=0.05fd(a)lt(3)passelse:a+=0.05fd(a)lt(3)passpasstracer(True)end_fill()pass
#头部
def head():fillcolor(0.3, 0.65, 0.8)begin_fill()seth(30)circle(120, 300)goto(0, 0)end_fill()fillcolor('red')begin_fill()lt(30)fd(12)circle(-6, 180)fd(140)circle(-6, 180)fd(140)end_fill()lt(180)fd(20)lt(-150)fillcolor('white')begin_fill()circle(90, 120)lt(30)fd(100)lt(30)circle(90, 120)end_fill()head()#画头部
my_goto(-5,155)
seth(95)
eyes()#画眼睛
my_goto(-65,155)
eyes()#画眼睛
my_goto(-49.5,115)
fillcolor('red')
begin_fill()
circle(15,630)#画鼻子
end_fill()
setheading(270)
fd(80)
setheading(360)
circle(100,50)#画嘴巴
my_goto(-62,20)
setheading(180)
circle(-100,45)
my_goto(-50,90)
setheading(370)
fd(60)#画胡须
my_goto(-50,80)
setheading(360)
fd(70)
my_goto(-50,70)
setheading(350)
fd(60)
my_goto(-75,90)
setheading(170)
fd(60)
my_goto(-75,80)
setheading(180)
fd(70)
my_goto(-75,70)
setheading(190)
fd(60)
#画身体
my_goto(0,0)
fillcolor(0.3, 0.65, 0.8)
begin_fill()
setheading(390)
fd(80)
setheading(90)
circle(-25,280)#画手
setheading(230)
fd(75)
setheading(-270)
fd(15)
setheading(270)
fd(20)
tracer(False)
b=1
for item in range(150):if 0<=item<100:fd(b)     right(0.1)else:fd(b)lt(0.1)passpass
tracer(True)
setheading(180)
fd(55)#画脚
setheading(90)
circle(30,180)
setheading(180)
fd(55)
setheading(90)
tracer(False)
c=1
for j in range(90):if 0<=j<70:fd(c)lt(0.1)else:fd(c)right(0.1)passpass
fd(20)
setheading(90)
fd(20)
setheading(270)
fd(10)
setheading(180+25)
fd(20)
setheading(-90)
circle(-25,260)
setheading(40)
fd(85)
setheading(-45)
fd(10)
setheading(360)
fd(140)
circle(6,180)
goto(0,0)
tracer(True)
end_fill()
#画肚子
fillcolor(1,1,1)
begin_fill()
my_goto(-17,-15)
setheading(330)
circle(-77,300)
goto(-17,-15)
end_fill()
#画肚兜
my_goto(0,-85)
setheading(270)
circle(-56,180)
setheading(360)
fd(112)
#重新画身体的残缺部分
my_goto(0,0)
setheading(390)
fd(80)
setheading(90)
fillcolor(1, 1, 1)
begin_fill()
circle(-25,280)
end_fill()
setheading(230)
fd(75)
setheading(-270)
fd(15)
setheading(270)
fd(20)
tracer(False)
b=1
for item in range(150):if 0<=item<100:fd(b)right(0.1)else:fd(b)lt(0.1)passpass
tracer(True)
setheading(360)
fd(10)
circle(-10,180)
fd(75)
circle(-10,180) 
fd(10)
setheading(90)
circle(30,180)
setheading(360)
fd(10)
circle(-10,180)
fd(75)
circle(-10,180)
fd(10)
setheading(90)
tracer(False)
c=1
for j in range(90):if 0<=j<70:fd(c)lt(0.1)    else:fd(c)right(0.1)passpass
tracer(True)
fd(20)
setheading(90)
fd(20)
setheading(270)
fd(10)
setheading(205)
fd(20)
setheading(-90)
fillcolor(1,1,1)
begin_fill()
circle(-25,620)
end_fill()
setheading(40)
fd(83)
setheading(-45)
fd(10)
setheading(360)
fd(70)
penup()
setheading(90)
fd(5)
pendown()
setheading(-180)
fillcolor('yellow')
begin_fill()
circle(12,435)
end_fill()
pensize(2)
setheading(90)
fd(3)
circle(2,180)
setheading(360)
fillcolor('yellow')
begin_fill()
fd(29)
circle(-2,180)
fd(31)
end_fill()
setheading(360)
fd(5)
setheading(270)
circle(12,100)
setheading(90)
pensize(4)
fd(5)
pensize(1)
fillcolor(0,0,0)
begin_fill()
setheading(400)
circle(2)
end_fill()
setheading(90)
my_goto(-40,160)
setheading(200)
fillcolor(0,0,0)
begin_fill()
d=0.1
tracer(False)
setheading(190)
for k in range(120):if 0<=k<30 or 60<=k<90:d+=0.02fd(d)lt(2.9)passelse:d-=0.02fd(d)lt(2.9)passpass
tracer(True)
end_fill()
setheading(230)
penup()
fd(5)
pendown()
fillcolor(1,1,1)
begin_fill()
circle(3)
end_fill()
my_goto(-90,160)
fillcolor(0,0,0)
begin_fill()
d=0.1
tracer(False)
setheading(190)
for k in range(120):if 0<=k<30 or 60<=k<90:d+=0.02fd(d)lt(2.9)passelse:d-=0.02fd(d)lt(2.9)passpass
tracer(True)
end_fill()
setheading(270)
fd(5)
fillcolor(1,1,1)
setheading(230)
begin_fill()
circle(3)
end_fill()
hideturtle()
circle(3,1440)
reset()
#-------------------------------------------------皮卡丘-----------------------------------------
# coding:utf-8
import turtle as t
import time
# 皮卡丘
# 基础设置
t.screensize(1366, 768)
t.pensize(2)  # 设置画笔的大小
t.speed(0)  # 设置画笔速度为10
# 画左偏曲线函数
bgcolor(1,0.2,0.1)
def radian_left(ang, dis, step, n):for i in range(n):dis += step  # dis增大stept.lt(ang)  # 向左转ang度t.fd(dis)  # 向前走dis的步长
def radian_right(ang, dis, step, n):for i in range(n):dis += stept.rt(ang)  # 向左转ang度t.fd(dis)  # 向前走dis的步长
#画耳朵def InitEars():t.color("black", "yellow")# 左耳朵曲线t.pu()  # 提笔t.goto(-50, 100)  # 笔头初始位置t.pd()  # 下笔t.setheading(110)  # 画笔角度t.begin_fill()radian_left(1.2, 0.4, 0.1, 40)t.setheading(270)  # 画笔角度radian_left(1.2, 0.4, 0.1, 40)t.setheading(44)  # 画笔角度t.forward(32)t.end_fill()# 右耳朵曲线t.pu()  # 提笔t.goto(50, 100)  # 笔头初始位置t.pd()  # 下笔t.setheading(70)  # 画笔角度t.begin_fill()radian_right(1.2, 0.4, 0.1, 40)t.setheading(270)  # 画笔角度radian_right(1.2, 0.4, 0.1, 40)t.setheading(136)  # 画笔角度t.forward(32)t.end_fill()# 耳朵黑t.begin_fill()t.fillcolor("black")t.pu()  # 提笔t.goto(88, 141)  # 笔头初始位置t.pd()  # 下笔t.setheading(35)  # 画笔角度radian_right(1.2, 1.6, 0.1, 16)t.setheading(270)  # 画笔角度radian_right(1.2, 0.4, 0.1, 25)t.setheading(132)  # 画笔角度t.forward(31)t.end_fill()t.begin_fill()t.fillcolor("black")t.pu()  # 提笔t.goto(-88, 141)  # 笔头初始位置t.pd()  # 下笔t.setheading(145)  # 画笔角度radian_left(1.2, 1.6, 0.1, 16)t.setheading(270)  # 画笔角度radian_left(1.2, 0.4, 0.1, 25)t.setheading(48)  # 画笔角度t.forward(31)t.end_fill()
# 画尾巴
def InitTail():# 尾巴t.begin_fill()t.fillcolor("yellow")t.pu()  # 提笔t.goto(64, -140)  # 笔头初始位置t.pd()  # 下笔t.setheading(10)  # 画笔角度t.forward(20)t.setheading(90)  # 画笔角度t.forward(20)t.setheading(10)  # 画笔角度t.forward(10)t.setheading(80)  # 画笔角度t.forward(100)t.setheading(35)  # 画笔角度t.forward(80)t.setheading(260)  # 画笔角度t.forward(100)t.setheading(205)  # 画笔角度t.forward(40)t.setheading(260)  # 画笔角度t.forward(37)t.setheading(205)  # 画笔角度t.forward(20)t.setheading(260)  # 画笔角度t.forward(25)t.setheading(175)  # 画笔角度t.forward(30)t.setheading(100)  # 画笔角度t.forward(13)t.end_fill()
# 画脚
def InitFoots():# 脚t.begin_fill()t.fillcolor("yellow")t.pensize(2)t.pu()  # 提笔t.goto(-70, -200)  # 笔头初始位置t.pd()  # 下笔t.setheading(225)  # 画笔角度radian_left(0.5, 1.2, 0, 12)radian_left(35, 0.6, 0, 4)radian_left(1, 1.2, 0, 18)t.setheading(160)  # 画笔角度t.forward(13)t.end_fill()t.begin_fill()t.fillcolor("yellow")t.pensize(2)t.pu()  # 提笔t.goto(70, -200)  # 笔头初始位置t.pd()  # 下笔t.setheading(315)  # 画笔角度radian_right(0.5, 1.2, 0, 12)radian_right(35, 0.6, 0, 4)radian_right(1, 1.2, 0, 18)t.setheading(20)  # 画笔角度t.forward(13)t.end_fill()
# 画身体
def InitBody():# 外形轮廓t.begin_fill()t.pu()  # 提笔t.goto(112, 0)  # 笔头初始位置t.pd()  # 下笔t.setheading(90)  # 画笔角度t.circle(112, 180)t.setheading(250)  # 画笔角度radian_left(1.6, 1.3, 0, 50)radian_left(0.8, 1.5, 0, 25)t.setheading(255)  # 画笔角度radian_left(0.4, 1.6, 0.2, 27)radian_left(2.8, 1, 0, 45)radian_right(0.9, 1.4, 0, 31)t.setheading(355)  # 画笔角度radian_right(0.9, 1.4, 0, 31)radian_left(2.8, 1, 0, 45)radian_left(0.4, 7.2, -0.2, 27)t.setheading(10)  # 画笔角度radian_left(0.8, 1.5, 0, 25)radian_left(1.6, 1.3, 0, 50)t.end_fill()
def InitEyes():# 左眼睛t.begin_fill()t.fillcolor("black")t.pu()  # 提笔t.goto(-46, 10)  # 笔头初始位置t.pd()  # 下笔t.setheading(90)  # 画笔角度t.circle(5, 360)t.end_fill()# 右眼睛t.begin_fill()t.fillcolor("black")t.pu()  # 提笔t.goto(46, 10)  # 笔头初始位置t.pd()  # 下笔t.setheading(-90)  # 画笔角度t.circle(5, 360)t.end_fill()
# 画脸
def InitFace():# 脸蛋t.begin_fill()t.fillcolor("red")t.pu()  # 提笔t.goto(-63, -10)  # 笔头初始位置t.pd()  # 下笔t.setheading(90)  # 画笔角度t.circle(10, 360)t.end_fill()t.begin_fill()t.fillcolor("red")t.pu()  # 提笔t.goto(63, -10)  # 笔头初始位置t.pd()  # 下笔t.setheading(-90)  # 画笔角度t.circle(10, 360)t.end_fill()# 嘴巴t.pensize(2.2)t.pu()  # 提笔t.goto(0, 0)  # 笔头初始位置t.pd()  # 下笔t.setheading(235)  # 画笔角度radian_right(5, 0.8, 0, 30)t.pu()  # 提笔t.goto(0, 0)  # 笔头初始位置t.pd()  # 下笔t.setheading(305)  # 画笔角度radian_left(5, 0.8, 0, 30)
# 画手
def InitHands():# 左手t.pensize(2)t.pu()  # 提笔t.goto(-46, -100)  # 笔头初始位置t.pd()  # 下笔t.setheading(285)  # 画笔角度radian_right(0.4, 1.2, 0, 26)radian_right(5, 0.35, 0, 26)radian_right(0.3, 1.2, 0, 15)# 右手t.pu()  # 提笔t.goto(46, -100)  # 笔头初始位置t.pd()  # 下笔t.setheading(255)  # 画笔角度radian_left(0.4, 1.2, 0, 26)radian_left(5, 0.35, 0, 26)radian_left(0.3, 1.2, 0, 15)
def CloseEyes():# 左眼睛t.pu()  # 提笔t.goto(-46, 12)  # 笔头初始位置t.pd()  # 下笔t.setheading(180)  # 画笔角度t.forward(10)# 右眼睛t.pu()  # 提笔t.goto(46, 12)  # 笔头初始位置t.pd()  # 下笔t.setheading(0)  # 画笔角度t.forward(10)
# 初始化
def Init():InitEars()InitTail()InitFoots()InitBody()InitFace()InitHands()InitEyes()
# 眨眼睛
def Upgarde():InitEars()InitTail()InitFoots()InitBody()InitFace()InitHands()CloseEyes()
def Upgarde_Init():InitEars()InitTail()InitFoots()InitBody()InitFace()InitHands()InitEyes()
def main():Init()t.tracer(False)# 眨眼睛动画for i in range(30):if i % 2 == 0:t.reset()t.hideturtle()Upgarde()t.update()time.sleep(0.3)else:t.reset()t.hideturtle()Upgarde_Init()t.update()time.sleep(1)
main()
done()

这篇关于用python 的turtle库画哆啦A梦和皮卡丘顺便加个背景音乐的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/501405

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e