本文實(shí)例為大家分享了python實(shí)現(xiàn)音樂播放器的具體代碼,供大家參考,具體內(nèi)容如下

"""這是一個(gè)用海龜畫圖模塊和pygame的混音模塊制作的簡(jiǎn)易播放器。
作者:李興球,日期:2018/8/26"""
from turtle import *
def init_screen():
"""初始化屏幕"""
screen = Screen()
screen.setup(width,height)
screen.bgpic("舞臺(tái).png")
screen.title(gametitle)
screen.delay(0)
return screen
def init_mixer():
"""初始化混音器,注意在函數(shù)內(nèi)部導(dǎo)入的模塊的作用范圍"""
have_pygame = False
try:
import pygame
pygame.mixer.init()
have_pygame = True
except:
pygame = None
return have_pygame ,pygame
class Button(Turtle):
"""按鈕類,每個(gè)按鈕有兩張圖片,自帶音樂"""
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 # 造型初始索引號(hào)
self.shape(self.costume_list[self.costume_index]) # 設(shè)置造型為索引為0的圖
self.goto(x,y)
self.width = width
self.height = height
self.left = x - width/2 # 左邊x坐標(biāo)
self.right = x + width/2 # 右邊x坐標(biāo)
self.top = y + height/2 # 上邊y坐標(biāo)
self.bottom = y - height/2 # 下邊y坐標(biāo)
self.music = music
self.showturtle()
self.onclick(self.play) # 單擊按鈕調(diào)用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表示循環(huán)播放,0表示從頭開始播放
def onmousemove(self,event):
"""判斷鼠標(biāo)指針是否在按鈕坐標(biāo)范圍內(nèi)"""
pass
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("初始化混音器出現(xiàn)問題。")
make_button()
screen.mainloop()另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
文章標(biāo)題:python實(shí)現(xiàn)音樂播放器python實(shí)現(xiàn)花框音樂盒子-創(chuàng)新互聯(lián)
URL地址:http://chinadenli.net/article20/ddpdco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、建站公司、網(wǎng)站設(shè)計(jì)、云服務(wù)器、App開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容