python3简易音乐播放器_花框音乐盒子_turtle版源码

"""这是一个用海龟画图模块和pygame的混音模块制作的简易播放器。
作者:李兴球,日期:2018/8/26"""

from turtle import *

def init_screen():
    """初始化屏幕"""
    screen = Screen()
    screen.setup(width,height)
    screen.bgpic("舞台.png")
    screen.title(gametitle)
    screen.delay(0)
    return screen

def init_mixer():
    """初始化混音器,注意在函数内部导入的模块的作用范围"""
    have_pygame = False
    try:
        import pygame
        pygame.mixer.init()
        have_pygame = True
    except:
        pygame = None
    return have_pygame ,pygame
    
class Button(Turtle):
    """按钮类,每个按钮有两张图片,自带音乐"""
    
    def __init__(self,costume_list,x,y,music,width,height):
        Turtle.__init__(self,visible=False)
        self.penup()
        self.costume_list = costume_list     #造型列表
        self.costume_index = 0               #造型初始索引号
        self.shape(self.costume_list[self.costume_index]) #设置造型为索引为0的图
        self.goto(x,y)
        self.width = width
        self.height = height
        self.left = x - width/2     #左边x坐标
        self.right = x + width/2    #右边x坐标
        self.top  = y + height/2    #上边y坐标
        self.bottom = y - height/2  #下边y坐标
        self.music = music         
        self.showturtle()
        self.onclick(self.play)     #单击按钮调用play方法
        
    def play(self,x,y):
        """先停止音乐再播放音乐"""
        pygame.mixer.music.stop()           #停止正在播放的音乐
        pygame.mixer.music.load(self.music)
        screen.title(gametitle + ",正在播放:" + self.music)
        pygame.mixer.music.play(-1,0)    #-1表示循环播放,0表示从头开始播放
        
    def onmousemove(self,event):
        """判断鼠标指针是否在按钮坐标范围内"""
        mouse_x = event.x - width/2      #转换为海龟坐标系的x坐标
        mouse_y = height/2 - event.y     #转换为海龟坐标系的y坐标
        #以下代码判断鼠标指针是否在按钮矩形范围内
        if mouse_x > self.left and mouse_x < self.right \
           and mouse_y > self.bottom and mouse_y < self.top:
            self.costume_index = 1
        else:
            self.costume_index = 0
        self.shape(self.costume_list[self.costume_index])

def make_button():
    """加载资源,生成播放按钮"""
    c1_list = ("Losing_Sleep0.gif","Losing_Sleep1.gif")
    [screen.addshape(image) for image in c1_list]
    music1 = "Alan Walker - Losing Sleep.mp3"
    b1 = Button(c1_list,-250,0,music1,200,150)
    screen.cv.bind("<Motion>",b1.onmousemove,add=True)
    
    c2_list = ("和兰花在一起0.gif","和兰花在一起1.gif")
    [screen.addshape(image) for image in c2_list]
    music2 = "Yanni - With An Orchid.mp3"
    b2 = Button(c2_list,00,0,music2,200,150)
    screen.cv.bind("<Motion>",b2.onmousemove,add=True)

    c3_list = ("Faded0.gif","Faded1.gif")
    [screen.addshape(image) for image in c3_list]
    music3 = "Alan Walker - Faded (纯音乐).wav"
    b3 = Button(c3_list,250,0,music3,200,150)
    screen.cv.bind("<Motion>",b3.onmousemove,add=True)
    
    c4_list = ("兰贵人0.gif","兰贵人1.gif")
    [screen.addshape(image) for image in c4_list]
    music4 = "胡伟立-兰贵人.mp3"
    b4 = Button(c4_list,-250,-200,music4,200,150)
    screen.cv.bind("<Motion>",b4.onmousemove,add=True)

    c5_list = ("Spectre0.gif","Spectre1.gif")
    [screen.addshape(image) for image in c5_list]
    music5 = "Alan Walker - Spectre.mp3"
    b5 = Button(c5_list,0,-200,music5,200,150)
    screen.cv.bind("<Motion>",b5.onmousemove,add=True)
    
    c6_list = ("新古典主义0.gif","新古典主义1.gif")
    [screen.addshape(image) for image in c6_list]
    music6 = "新古典主义-组曲.mp3"
    b6 = Button(c6_list,250,-200,music6,200,150)
    screen.cv.bind("<Motion>",b6.onmousemove,add=True)
    

if __name__ == "__main__":

    gametitle = "花框音乐盒"
    width,height = 800,600
    screen = init_screen()
    mixer_success,pygame = init_mixer()
    if mixer_success:
        print("成功初始化混音器。")
    else:
        print("初始化混音器出现问题。")
    make_button()
    screen.mainloop()

    """需要本作品素材请联系风火轮少儿编程李兴球."""

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李兴球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值