日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pygame制作音乐播放器

發布時間:2024/1/8 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pygame制作音乐播放器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用pygame制作的包含基本功能和畫面效果的簡單音樂播放器

實現如下功能:

  • 基本音樂播放器功能:播放,暫停,繼續播放,上一曲,下一曲,可以在圖形界面或鍵盤操作
    (播放:小鍵盤回車, 暫停:p, 繼續播放:空格, 上一曲:方向鍵←, 下一曲:方向鍵→)
  • 音量調節:按鍵:↑ ↓
  • 界面實現簡單動畫

運行截圖


ps:暫停之后點擊中間圓盤是繼續播放,我偷了點懶

代碼如下

Music_Payer.py

import pygame import Load_Music import function from button import Buttondef player_run():pygame.init()screen = pygame.display.set_mode((630, 892))pygame.display.set_caption("Music Player")background=pygame.image.load(r"./img/bg.jpg")screen.blit(background,(0,0))music_num = [1]volume = [1] Music_list = Load_Music.Music_List().get_play_list()Load_Music.load_music(Music_list[music_num[0]])pygame.time.Clock().tick(30)#限制30幀play_bt = Button(screen=screen, add="./img/play.png", name="play_music", place=[250, 600])next_bt = Button(screen=screen, add="./img/next.png", name="next_music", place=[400, 600])previous_bt = Button(screen=screen, add="./img/previous.png", name="previous_music", place=[100, 600])pause_bt = Button(screen=screen, add="./img/pause.png", name="pause_music", place=[250, 450])show_bt = Button(screen=screen, add="./img/show.png", name="show_music", place=[150, 100])while True:get_mouse = function.check_events(music_num, volume, Music_list)function.get_click(get_mouse, play_bt, pause_bt, next_bt, previous_bt, show_bt, music_num, Music_list)show_bt.show_button()play_bt.show_button()next_bt.show_button()previous_bt.show_button()pause_bt.show_button()if pygame.mixer.music.get_busy() == True:#音樂播放圖片開始轉動show_bt.ro_img()pygame.display.flip()player_run()

Load_Music.py

import pygame import osclass Music_List():def __init__(self):# 播放列表self.play_list = []# 默認文件夾self.file_dir = 'music'def get_play_list(self):# 開始遍歷目錄for root, dirs, files in os.walk(self.file_dir):for file in files:if '.mp3' in file:self.play_list.append(self.file_dir + "/" + file)return self.play_listdef load_music(titlt):#加載音樂try:pygame.mixer.music.load(titlt)except :passdef play_music():pygame.mixer.music.play()def stop_music():pygame.mixer.music.stop()def pause_music():"""暫停音樂"""pygame.mixer.music.pause()def unpause_music():"""解除暫停"""pygame.mixer.music.unpause() def next_music(music_num, Music_list):#下一曲.music_num[0] += 1load_music(Music_list[music_num[0]])play_music() def previous_music(music_num, Music_list):#上一曲music_num[0] -= 1load_music(Music_list[music_num[0]])play_music()def volume_music(volume):#調節音量pygame.mixer.music.set_volume(volume[0])print("音量:"+str(100*volume[0]))

function.py

import pygame import Load_Music import sys from button import Buttondef check_events(music_num, volume, Music_List):"""響應按鍵""" for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_RIGHT:Load_Music.next_music(music_num, Music_List)if event.key == pygame.K_LEFT:Load_Music.previous_music(music_num, Music_List)if event.key == pygame.K_p:Load_Music.pause_music()if event.key == pygame.K_SPACE:Load_Music.unpause_music()if event.key == pygame.K_KP_ENTER:Load_Music.load_music(str(music_num[0])+".mp3")Load_Music.play_music()if event.key == pygame.K_UP:volume[0] += 0.1Load_Music.volume_music(volume)if event.key == pygame.K_DOWN:volume[0] -= 0.1Load_Music.volume_music(volume)if event.type == pygame.MOUSEBUTTONDOWN:mouse_x, mouse_y = pygame.mouse.get_pos() mouse = [mouse_x, mouse_y]return mousedef get_click(get_mouse, play_bt, pause_bt, next_bt, previous_bt, show_bt, music_num, Music_list):if get_mouse != None :mouse = get_mouseif play_bt.click_button(mouse[0], mouse[1]) == True:Load_Music.play_music()elif pause_bt.click_button(mouse[0], mouse[1]) == True:Load_Music.pause_music()elif next_bt.click_button(mouse[0], mouse[1]) == True:Load_Music.next_music(music_num, Music_list)elif previous_bt.click_button(mouse[0], mouse[1]) == True:Load_Music.previous_music(music_num, Music_list)elif show_bt.click_button(mouse[0], mouse[1]) == True:Load_Music.unpause_music()

button.py

import pygameclass Button():def __init__(self, screen, add, name, place):self.screen = screenself.add = add#路徑self.name = name#按鈕功能self.place = place#按鈕繪制位置self.mybt = pygame.image.load(r''+self.add)#圖片地址self.rect = self.mybt.get_rect()#獲取圖案矩形 self.angle = 1#圖像旋轉角度def click_button(self,mouse_x,mouse_y):"""判定點擊按鈕"""if (mouse_x > self.place[0] and mouse_x < self.rect.width+(self.place[0]) and mouse_y > self.place[1] and mouse_y < self.rect.height+(self.place[1])):return Truedef show_button(self):"""顯示按鈕"""self.screen.blit(self.mybt, self.place)def ro_img(self):#旋轉圖像newimg = pygame.transform.rotozoom(self.mybt, self.angle, 1.0)newrect = newimg.get_rect(center=(300,248))#固定圖像中心self.screen.blit(newimg, newrect)self.angle += 1

文件結構:

總結

以上是生活随笔為你收集整理的Pygame制作音乐播放器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。