15個(gè)采納pos機(jī)的小知識,pygame高級音樂播放器的python相關(guān)知識復(fù)習(xí)

 新聞資訊  |   2023-04-16 10:23  |  投稿人:pos機(jī)之家

網(wǎng)上有很多關(guān)于15個(gè)采納pos機(jī)的小知識,pygame高級音樂播放器的python相關(guān)知識復(fù)習(xí)的知識,也有很多人為大家解答關(guān)于15個(gè)采納pos機(jī)的小知識的問題,今天pos機(jī)之家(www.dsth100338.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!

本文目錄一覽:

1、15個(gè)采納pos機(jī)的小知識

15個(gè)采納pos機(jī)的小知識

1說明:

1.1 pygame音樂播放器:

#屬于原作者h(yuǎn)ttps://github.com/Hongten/PyMusichttps://www.cnblogs.com/hongten/p/hongten_pygame_pymusic_public.html

1.2 在他基礎(chǔ)上對代碼進(jìn)行刪除、修改、潤色、改進(jìn)、注釋等處理,突出重點(diǎn),提高可讀性。

1.3 復(fù)習(xí):python法。

1.3.1 圖片的相關(guān)知識,將jpg轉(zhuǎn)換png法,修改圖片大小。

1.3.2 如何將mp3轉(zhuǎn)換OGG格式。

1.4 沒有的模塊,自己pip安裝。

1.5 python3.8和微軟vscode編輯器,親測過,值得收藏和分享,可以轉(zhuǎn)發(fā)。

1.6 增加tool文件夾,里面都是python的文件,可以進(jìn)行相關(guān)轉(zhuǎn)換。

2 文件結(jié)構(gòu):簡化過

3 簡化過的代碼:完整版:pymusic.py

#---第1步:導(dǎo)出模塊---import os, pygamefrom pygame.locals import *from sys import exitfrom random import *#---第2步:初始化pygame,窗口標(biāo)題、大小設(shè)定等---pygame.init()pygame.display.set_caption('pygame高級音樂播放器')#pygame的特點(diǎn),就是屏幕大小的設(shè)定,與掛在屏幕上的圖片和文字等有關(guān),所以需要單獨(dú)定義#盡管有點(diǎn)覺得代碼繁瑣,大型游戲和項(xiàng)目還是推薦這樣#推薦大小1280和850SCREEN_W = 1280SCREEN_H = 850#注意沒有H+20干嘛,就是后面的顯示底部信息欄用的SCREEN_DEFAULT_SIZE = (SCREEN_W, SCREEN_H + 20)screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)#背景圖片設(shè)置,需放在screen定義之后,否則報(bào)錯(cuò),大小1366×768,可以用代碼修改bg = pygame.image.load('/home/xgj/Desktop/src/image/bg.jpg').convert()#通用字體和大小設(shè)置font = pygame.font.Font(('/home/xgj/Desktop/src/hwfs.ttf'), 24)#---第3步:個(gè)性化設(shè)置:初始化音量,圖片坐標(biāo),按鈕坐標(biāo)等設(shè)置---VOLUME =5IMAGE_START_POS = (60, 60) #圖片初始化坐標(biāo)IMAGE_END_POS = (245, 245) #圖片結(jié)束坐標(biāo)#因?yàn)椴シ虐粹o的是作者畫出的,所以坐標(biāo)、半徑、寬度定義CIRCLES_POS = [(85, 350), (150, 350), (215, 350), (280, 350), (555, 425)]CIRCLR_R = 25CIRCLR_W = 3#---第4步:路徑和歌曲列表等#注意路徑和目錄,如果打包,可能路徑和目錄需要從新設(shè)計(jì)IMAGE_DIR = '/home/xgj/Desktop/src/image'#放音樂的路徑sound_DIR = '/home/xgj/Desktop/src/sound'#演唱者的圖片大小=size:(240*240)SONG_FLAG = 0#音樂文件OGG放在/home/xgj/Desktop/src/sound下#圖片png放在/home/xgj/Desktop/src/image下#可自定義,其他信息可以自定義#比如自己換歌曲,換圖片,即可#歌曲列表SONGS = [('1.OGG', 'You Raise Me Up', 'WestLife', '1.png'), ('2.OGG', '不完整的旋律', '王力宏', '2.png'), ('3.OGG', 'A Place Nearby' , 'Lene Marlin', '3.png'), ('4.OGG', 'Just Give Me A Reason' , 'Pink', '4.png'), ('5.OGG', '我 ' , '張國榮', '5.png'), ('6.OGG', '大城小愛' , '王力宏', '6.png'), ('7.OGG', '聊天' , '郭靜', '7.png')]#---第5步:音量定義---VOLUME_POINTS = []VOLUME_POINTS_START = []VOLUME_Rect_COLORS = []for p in range(170, 250, 7): #音量位置,因?yàn)樾畔诘淖煮w變大后需要調(diào)整 #減去300與下面的音量5的設(shè)置調(diào)整一下,一前一后。 VOLUME_POINTS.append([SCREEN_W - p-300,SCREEN_H-40])for ps in range(175, 250, 7): #位置 VOLUME_POINTS_START.append([SCREEN_W - ps-300, SCREEN_H-40]) #音量隨機(jī)顏色 VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))#---第6步:歌曲播放設(shè)置---SONG_ARRAY = []SONG_IMAGE = []for song in range(len(SONGS)): #簡化過 SONG_ARRAY.append(pygame.mixer.Sound(os.path.join(SOUND_DIR, SONGS[song][0]))) SONG_IMAGE.append(pygame.image.load(os.path.join(IMAGE_DIR, SONGS[song][3])).convert())#---第7步:畫歌曲圖片定義---def draw_picture_rect(): #屏幕、顏色、坐標(biāo) pygame.draw.rect(screen,(255, 255, 255),Rect(IMAGE_START_POS, IMAGE_END_POS))#---第8步:音樂播放等按鈕繪畫---#播放=play按鈕def button_play(screen, color): pygame.draw.circle(screen, color, CIRCLES_POS[0], CIRCLR_R, CIRCLR_W) points=[(77,340),(77,360),(95,350)] pygame.draw.polygon(screen,color,points)#暫停=stop按鈕def button_stop(screen, color): pygame.draw.circle(screen, color, CIRCLES_POS[0], CIRCLR_R, CIRCLR_W) pygame.draw.rect(screen,color,Rect(77, 340, 5, 23 )) pygame.draw.rect(screen,color,Rect(88, 340, 5, 23 ))#上一首=perfer按鈕def button_perfer(screen, color): pygame.draw.circle(screen, color, CIRCLES_POS[1], CIRCLR_R, CIRCLR_W) points=[(138,340),(162,340),(150,363)] pygame.draw.polygon(screen,color,points)#刪除=del按鈕def button_del(screen, color): pygame.draw.circle(screen, color, CIRCLES_POS[2], CIRCLR_R, CIRCLR_W) pygame.draw.circle(screen, color, (215, 340), 6, 3) pygame.draw.rect(screen,color,Rect(200, 340, 30, 6 )) pygame.draw.rect(screen,color,Rect(204, 340, 3, 20 )) pygame.draw.rect(screen,color,Rect(210, 340, 3, 20 )) pygame.draw.rect(screen,color,Rect(217, 340, 3, 20 )) pygame.draw.rect(screen,color,Rect(223, 340, 3, 20 )) pygame.draw.rect(screen,color,Rect(204, 360, 22, 5 ))#下一首=next按鈕def button_next_song(screen, color): pygame.draw.circle(screen, color, CIRCLES_POS[3], CIRCLR_R, CIRCLR_W) points_one =[(270,343),(270,357),(277,350)] points_two =[(277,343),(277,357),(284,350)] pygame.draw.polygon(screen,color,points_one) pygame.draw.polygon(screen,color,points_two) pygame.draw.rect(screen,color,Rect(284, 343, 5, 15 ))#---播放等按鈕繪畫---作者很牛是不是,點(diǎn)個(gè)贊---#---第9步:全局定義flag,監(jiān)聽鼠標(biāo)點(diǎn)擊按鈕事件函數(shù)---PLAY_FLAG = TruePREFER_FLAG = True#鼠標(biāo)點(diǎn)擊播放等按鈕的監(jiān)聽設(shè)置def mouse_down_listener(sound): global PLAY_FLAG global PREFER_FLAG global SONG_FLAG #獲取鼠標(biāo)點(diǎn)擊的坐標(biāo) x, y = pygame.mouse.get_pos() for index in range(len(CIRCLES_POS)): p_x = (CIRCLES_POS[index][0] - x)**2 p_y = (CIRCLES_POS[index][1] - y)**2 p_r = (CIRCLR_R)**2 if (p_x + p_y <= p_r): if index == 0 and PLAY_FLAG: sound.stop() PLAY_FLAG = False elif index == 0 and not PLAY_FLAG: sound.play(0) PLAY_FLAG = True elif index == 1 and PREFER_FLAG: PREFER_FLAG = False elif index == 1 and not PREFER_FLAG: PREFER_FLAG = True elif index == 2: sound.stop() if SONG_FLAG > 0: SONGS.pop(SONG_FLAG) SONG_IMAGE.pop(SONG_FLAG) SONG_ARRAY.pop(SONG_FLAG) if SONG_FLAG >= len(SONGS) - 1: SONG_FLAG -= 1 else: #print('This is the last song.') #這個(gè)注釋掉后需要加個(gè)pass pass elif index == 3: sound.stop() if SONG_FLAG < len(SONGS) - 1: SONG_FLAG += 1 else: SONG_FLAG = 0#---第10步:定義畫按鈕的點(diǎn)擊函數(shù),即播放還是暫停音樂等---def draw_button(sound): color = (255,255,255) color_red = (230, 0, 0) #play or stop if PLAY_FLAG: sound.play(0) button_stop(screen, color) elif not PLAY_FLAG: button_play(screen, color) #perfer song if PREFER_FLAG: button_perfer(screen, color) elif not PREFER_FLAG: button_perfer(screen, color_red) #delete button_del(screen, color) #next song button_next_song(screen, color)#---第11步:畫音量信息和調(diào)節(jié)音量設(shè)置---def draw_volume_info(): #the background of volume #20代表音量調(diào)節(jié)背景框的高度 pygame.draw.rect(screen, (255, 255, 255), Rect((VOLUME_POINTS_START[-1][0], VOLUME_POINTS_START[-1][1]), (VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0], 20))) #the size of volume for v in range(VOLUME+1): if v > 0: #20代表目前的音量高度 pygame.draw.rect(screen, VOLUME_RECT_COLORS[v], Rect((VOLUME_POINTS_START[-v][0], VOLUME_POINTS_START[-v][1]), (VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0], 20)))#---第12步:歌曲名顯示設(shè)置---def draw_song_title(): title = font.render(SONGS[SONG_FLAG][1], True, (255,165,0)) songer = font.render(SONGS[SONG_FLAG][2], True, (255, 255, 255)) screen.blit(title, (320, 60)) screen.blit(songer, (320, 110))#---第13步:播放狀態(tài)欄信息設(shè)置---def draw_state_bar_info(): #底部歌曲信息欄的水平紅線 pygame.draw.line(screen, (165,42,42),(0, SCREEN_H-70), (SCREEN_W, SCREEN_H-70)) #music info music_info = 'AllSongs: ' + str(len(SONGS)) +' Current: ' + str(SONG_FLAG + 1) #font是通用字體設(shè)置 text = font.render(music_info, True, (255,255,255)) #-50,則信息欄的文字往上提高,便于字體變大后的顯示 screen.blit(text, (0, SCREEN_H-50)) #volume info,bug,str(VOLUME)當(dāng)按左右箭頭調(diào)節(jié)音量時(shí),原來是5,之后的數(shù)字覆蓋而模糊 volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255)) screen.blit(volume_text, (SCREEN_W - 750, SCREEN_H-50)) #author into author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255)) screen.blit(author_info, (SCREEN_W - 300, SCREEN_H-50))#---第14步:播放循環(huán)---while True: screen.blit(bg, (0, 0)) pic = SONG_IMAGE[SONG_FLAG] bg_sound = SONG_ARRAY[SONG_FLAG] bg_sound.set_volume(0.1 * VOLUME) draw_button(bg_sound) #listener() for event in pygame.event.get(): if event.type == QUIT: bg_sound.stop() exit() elif event.type == KEYDOWN: #音量調(diào)節(jié)設(shè)置,←和→的定義 if event.key == K_LEFT: if VOLUME > 0: VOLUME -= 1 elif event.key == K_RIGHT: if VOLUME <= 9: VOLUME += 1 pygame.display.update() elif event.type == MOUSEBUTTONDOWN: pressed_array = pygame.mouse.get_pressed() for index in range(len(pressed_array)): if pressed_array[index]: if index == 0: #When the LEFT button down #鼠標(biāo)點(diǎn)擊播放等按鈕的監(jiān)聽 mouse_down_listener(bg_sound) #picture rect draw_picture_rect() #volume information draw_volume_info() #state bar information draw_state_bar_info() #song title draw_song_title() #顯示圖片坐標(biāo),與上面的60和60坐標(biāo)相呼應(yīng) #screen.blit(pic, (62.5, 62.5)) #最新的python3.8和pygame1.9.6浮點(diǎn)數(shù)是報(bào)錯(cuò) screen.blit(pic, (62, 62)) pygame.display.update()

注意路徑自定義。

4 效果:

小bug:當(dāng)前曲目:1~7和音量調(diào)節(jié)數(shù)字1~10,模糊不清;啟動(dòng)有點(diǎn)慢。

5 相關(guān)工具介紹:3個(gè)好工具。

5.1 mp3toogg.py代碼:用來將mp3轉(zhuǎn)換為OGG音樂文件。

奇怪,原作者為什么不用mp3格式的音樂文件呢?莫非他也是采納國外某些人的習(xí)慣么?我也是第一次接觸OGG音樂文件,是不是可以改為mp3音樂文件操作起來更簡單些。

from pydub import AudioSegment#注意路徑song = AudioSegment.from_mp3("/home/xgj/Desktop/src/1.mp3")#song.export("out.ogg", format="ogg") # Is the same as:song.export("out.ogg", format="ogg", codec="libvorbis")

5.2 picsize.py代碼:調(diào)節(jié)圖片大小的。

from PIL import Imageimport os# 獲取文件大小:KBdef get_size(file): size = os.path.getsize(file) return size / 1024#輸出文件def get_outfile(infile, outfile): if outfile: return outfile dir, suffix = os.path.splitext(infile) outfile = '{}-out{}'.format(dir, suffix) return outfile#修改圖片函數(shù)def resize_image(infile, outfile='', x_s=1376): im = Image.open(infile) #輸出指定大小,60×60大小圖片 out = im.resize((1366, 768), Image.ANTIALIAS) outfile = get_outfile(infile, outfile) out.save(outfile)#主函數(shù)if __name__ == '__main__': #路徑可自定義 resize_image('/home/xgj/Desktop/src/data/image/background/bg.jpg')

5.3 圖片格式轉(zhuǎn)換:jpgtopng.py代碼:

# pip install Pillowimport PIL.Image as Image# 以第一個(gè)像素為準(zhǔn),相同色改為透明def transparent_back(img): img = img.convert('RGBA') L, H = img.size color_0 = (255,255,255,255)#要替換的顏色 for h in range(H): for l in range(L): dot = (l,h) color_1 = img.getpixel(dot) if color_1 == color_0: color_1 = color_1[:-1] + (0,) img.putpixel(dot,color_1) return img if __name__ == '__main__': img=Image.open('1231.jpg') img=transparent_back(img) img.save('1231.png')

好東西拿來分享,值得收藏。

以上就是關(guān)于15個(gè)采納pos機(jī)的小知識,pygame高級音樂播放器的python相關(guān)知識復(fù)習(xí)的知識,后面我們會繼續(xù)為大家整理關(guān)于15個(gè)采納pos機(jī)的小知識的知識,希望能夠幫助到大家!

轉(zhuǎn)發(fā)請帶上網(wǎng)址:http://www.dsth100338.com/news/19053.html

你可能會喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至 babsan@163.com 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。