程序名:PyMusic
解釋:pygame+music
之前發布了自己寫的小程序:百度隨心聽音樂播放器的一些效果圖
你可以去到這里再次看看效果:
這個程序的靈感來自於百度隨心聽
說明:
動作按鈕全部是畫出來的,沒有用到任何圖片
用到圖片的只有:背景,歌手圖片,作者圖片
經過一個階段的調試,現在算是可以拿上台面和大家交流
功能介紹:
A.四個按鈕介紹
1.完成停止,播放音樂功能
2.喜歡/不喜歡當前所播放的音樂
3.刪除當前所播放的音樂(物理環境下不會刪除,刪除的是內存中的)
4.下一曲
5.當鼠標經過按鈕的時候,按鈕顏色會變化
B.狀態欄(最下面)
1.顯示所有歌曲數(AllSongs)
2.當前播放的歌曲是第只歌曲
3.當前音量(范圍:0 - 10)
4.音量的圖形顯示(這里沒有用到圖片,而是系統畫出來的:-))
5.我的郵箱信息:hongtenzone@foxmail.com
C.右下角
1.當鼠標移動到黃色圓區域,會展示出我的相片和'Yes,You are Luck:)'字樣
鼠標一開的時候會自動隱藏(我想接觸過android系統手機的的朋友,可能有這樣的經歷,在工具里面有一個地方
當點擊三下的時候,會出現android里面的一副圖片....對我這里的靈感來自於這里ll)
=======================================
下面我說說我做的思路吧
我想有一部分朋友會喜歡的...:)
首先,主題功能是音樂播放,那么我們就應該實現這個音樂播放的功能
哈哈,這個靈感來自於我之前寫的小游戲:
在這個游戲中,我實現了音樂的播放,於是乎我就想在音樂播放上面做一些文章...
接下來是程序PyMusic的主題界面需要考慮,我個人喜歡聽歌...百度音樂,百度隨心聽都是常去的地方..
百度隨心聽的界面設計簡潔,很適合我的風格...所以我選擇了百度隨心聽..
於是乎我看是看百度隨心聽的代碼...一不小心被我大概看明白了按鈕之間的邏輯....
一時間,頭腦中出現了PyMusic的原型....
為什么不做一個呢?我就這樣問着我自己,慢慢在腦海中呈現PyMusic的原型...
然后開始動筆,把pyMusic的原型在紙上畫了出來....
畫好了,就開始分析...(這個過程有一點長..)
然后把原型中的物體(按鈕,圖片加載..)一個一個的實現..
最后加上我的小自信..呵呵,成啦..
=======================================
一些細節
在做PyMusic的過程中,需要注意一些細節的地方
我下面把這些地方羅列出來
1.音量圖形的初始化
1 VOLUME_POINTS = [] 2 VOLUME_POINTS_START = [] 3 VOLUME_RECT_COLORS = [] 4 for p in range(170, 250, 7): 5 VOLUME_POINTS.append([SCREEN_W - p,SCREEN_H + 20]) 6 for ps in range(175, 250, 7): 7 VOLUME_POINTS_START.append([SCREEN_W - ps, SCREEN_H]) 8 VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))
2.加載歌曲和圖片
1 SONG_ARRAY = [] 2 SONG_IMAGE = [] 3 for song in range(len(SONGS)): 4 SONG_ARRAY.append(pygame.mixer.Sound(os.path.join(DATA_DIR, SOUND_DIR, SONGS[song][0]))) 5 SONG_IMAGE.append(pygame.image.load(os.path.join(DATA_DIR, IMAGE_DIR, SONGS[song][3])).convert())
3.字體加載
1 font = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'TORK____.ttf'), 14) 2 font_song_title = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyhbd.ttf'), 24) 3 font_song = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyh.ttf'), 16)
4.停止/播放按鈕
1 def button_play(screen, color): 2 pygame.draw.circle(screen, color, CIRCLES_POS[0], CIRCLR_R, CIRCLR_W) 3 points=[(77,340),(77,360),(95,350)] 4 pygame.draw.polygon(screen,color,points) 5 6 def button_stop(screen, color): 7 pygame.draw.circle(screen, color, CIRCLES_POS[0], CIRCLR_R, CIRCLR_W) 8 pygame.draw.rect(screen, 9 color, 10 Rect(77, 340, 5, 23 )) 11 pygame.draw.rect(screen, 12 color, 13 Rect(88, 340, 5, 23 ))
5.刪除歌曲按鈕
1 def button_del(screen, color): 2 pygame.draw.circle(screen, color, CIRCLES_POS[2], CIRCLR_R, CIRCLR_W) 3 pygame.draw.circle(screen, color, (215, 340), 6, 3) 4 pygame.draw.rect(screen, 5 color, 6 Rect(200, 340, 30, 6 )) 7 pygame.draw.rect(screen, 8 color, 9 Rect(204, 340, 3, 20 )) 10 pygame.draw.rect(screen, 11 color, 12 Rect(210, 340, 3, 20 )) 13 pygame.draw.rect(screen, 14 color, 15 Rect(217, 340, 3, 20 )) 16 pygame.draw.rect(screen, 17 color, 18 Rect(223, 340, 3, 20 )) 19 pygame.draw.rect(screen, 20 color, 21 Rect(204, 360, 22, 5 ))
6.作者信息展示
1 def button_authon_image(screen, color): 2 pygame.draw.circle(screen, color, CIRCLES_POS[4], CIRCLR_R, CIRCLR_W) 3 pygame.draw.rect(screen, 4 (255, 255, 255), 5 Rect(418, 248, 144, 154 )) 6 screen.blit(author_image, (420, 250)) 7 luck = font_song_title.render('Yes, You are Luck :-)', True, (255,165,10)) 8 screen.blit(luck, (280, 416))
7.第一個監聽器
1 def listener(): 2 global PLAY_FLAG 3 global PREFER_FLAG 4 x, y = pygame.mouse.get_pos() 5 color = (255,255,25) 6 color_red = (230, 0, 0) 7 for index in range(len(CIRCLES_POS)): 8 p_x = (CIRCLES_POS[index][0] - x)**2 9 p_y = (CIRCLES_POS[index][1] - y)**2 10 p_r = (CIRCLR_R)**2 11 if (p_x + p_y <= p_r): 12 if index == 0 and PLAY_FLAG: 13 button_stop(screen, color) 14 elif index == 0 and not PLAY_FLAG: 15 button_play(screen, color) 16 elif index == 1 and PREFER_FLAG: 17 button_perfer(screen, color) 18 elif index == 1 and not PREFER_FLAG: 19 button_perfer(screen, color_red) 20 elif index == 2: 21 button_del(screen, color) 22 elif index == 3: 23 button_next_song(screen, color) 24 elif index == 4: 25 button_authon_image(screen, color)
8.第二個監聽器
1 def mouse_down_listener(sound): 2 global PLAY_FLAG 3 global PREFER_FLAG 4 global SONG_FLAG 5 x, y = pygame.mouse.get_pos() 6 for index in range(len(CIRCLES_POS)): 7 p_x = (CIRCLES_POS[index][0] - x)**2 8 p_y = (CIRCLES_POS[index][1] - y)**2 9 p_r = (CIRCLR_R)**2 10 if (p_x + p_y <= p_r): 11 if index == 0 and PLAY_FLAG: 12 #print('stop now......') 13 sound.stop() 14 PLAY_FLAG = False 15 elif index == 0 and not PLAY_FLAG: 16 #print('play now ... ... ... ...') 17 sound.play(0) 18 PLAY_FLAG = True 19 elif index == 1 and PREFER_FLAG: 20 print('perfer song....<<', SONGS[SONG_FLAG][1], '>>') 21 PREFER_FLAG = False 22 elif index == 1 and not PREFER_FLAG: 23 print('not perfer song... <<', SONGS[SONG_FLAG][1], '>>') 24 PREFER_FLAG = True 25 elif index == 2: 26 sound.stop() 27 print('delete song....<<', SONGS[SONG_FLAG][1], '>>') 28 if SONG_FLAG > 0: 29 SONGS.pop(SONG_FLAG) 30 SONG_IMAGE.pop(SONG_FLAG) 31 SONG_ARRAY.pop(SONG_FLAG) 32 if SONG_FLAG >= len(SONGS) - 1: 33 SONG_FLAG -= 1 34 else: 35 print('This is the last song.') 36 elif index == 3: 37 sound.stop() 38 if SONG_FLAG < len(SONGS) - 1: 39 SONG_FLAG += 1 40 else: 41 SONG_FLAG = 0 42 #print('next song....') 43
9.鼠標按下事件
1 elif event.type == MOUSEBUTTONDOWN: 2 pressed_array = pygame.mouse.get_pressed() 3 for index in range(len(pressed_array)): 4 if pressed_array[index]: 5 if index == 0: #When the LEFT button down 6 mouse_down_listener(bg_sound)
上面的都是一些細節的地方...
=======================================
下面是完整代碼部分:
=======================================
1 #pygame music 2 3 import os, pygame 4 from pygame.locals import * 5 from sys import exit 6 from random import * 7 8 __des__ = ''' 9 Name: 10 PyMusic 11 ''' 12 __version__ = '2.0' 13 __author__ = {'name' : 'Hongten', 14 'mail' : 'hongtenzone@foxmail.com', 15 'blog' : 'http://www.cnblogs.com/hongten', 16 'version' : __version__} 17 18 if not pygame.mixer: print('Warning, sound disabled!') 19 if not pygame.font: print('Warning, fonts disabled!') 20 21 pygame.init() 22 23 SCREEN_W = 580 24 SCREEN_H = 450 25 SCREEN_DEFAULT_SIZE = (SCREEN_W, SCREEN_H + 20) 26 VOLUME = 5 27 IMAGE_START_POS = (60, 60) 28 IMAGE_END_POS = (245, 245) 29 CIRCLES_POS = [(85, 350), (150, 350), (215, 350), (280, 350), (555, 425)] 30 CIRCLR_R = 25 31 CIRCLR_W = 3 32 33 PLAY_FLAG = True 34 PREFER_FLAG = True 35 36 37 DATA_DIR = 'data' 38 IMAGE_DIR = 'image' 39 BG_IMAGE_DIR = 'image\\background' 40 FONT_DIR = 'font' 41 SOUND_DIR = 'sound' 42 43 BG_IMAGE = 'bg.jpg' 44 AUTHOR_IMAGE = 'author.png' 45 #size:(240*240) 46 BGS = [] 47 SONG_FLAG = 0 48 SONGS = [('1.OGG', 'You Raise Me Up', 'WestLife', '1.png'), 49 ('2.OGG', '不完整的旋律', '王力宏', '2.png'), 50 ('3.OGG', 'A Place Nearby' , 'Lene Marlin', '3.png'), 51 ('4.OGG', 'Just Give Me A Reason' , 'Pink', '4.png'), 52 ('5.OGG', '我 ' , '張國榮', '5.png'), 53 ('6.OGG', '大城小愛' , '王力宏', '6.png'), 54 ('7.OGG', '聊天' , '郭靜', '7.png')] 55 westlift = 'westlife.png' 56 57 VOLUME_POINTS = [] 58 VOLUME_POINTS_START = [] 59 VOLUME_RECT_COLORS = [] 60 for p in range(170, 250, 7): 61 VOLUME_POINTS.append([SCREEN_W - p,SCREEN_H + 20]) 62 for ps in range(175, 250, 7): 63 VOLUME_POINTS_START.append([SCREEN_W - ps, SCREEN_H]) 64 VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255))) 65 66 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32) 67 bg = pygame.image.load(os.path.join(DATA_DIR, BG_IMAGE_DIR, BG_IMAGE)).convert() 68 author_image = pygame.image.load(os.path.join(DATA_DIR, IMAGE_DIR, AUTHOR_IMAGE)).convert() 69 70 SONG_ARRAY = [] 71 SONG_IMAGE = [] 72 for song in range(len(SONGS)): 73 SONG_ARRAY.append(pygame.mixer.Sound(os.path.join(DATA_DIR, SOUND_DIR, SONGS[song][0]))) 74 SONG_IMAGE.append(pygame.image.load(os.path.join(DATA_DIR, IMAGE_DIR, SONGS[song][3])).convert()) 75 76 font = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'TORK____.ttf'), 14) 77 font_song_title = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyhbd.ttf'), 24) 78 font_song = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyh.ttf'), 16) 79 80 def draw_picture_rect(): 81 #picture rect 82 pygame.draw.rect(screen, 83 (255, 255, 255), 84 Rect(IMAGE_START_POS, IMAGE_END_POS)) 85 86 def button_play(screen, color): 87 pygame.draw.circle(screen, color, CIRCLES_POS[0], CIRCLR_R, CIRCLR_W) 88 points=[(77,340),(77,360),(95,350)] 89 pygame.draw.polygon(screen,color,points) 90 91 def button_stop(screen, color): 92 pygame.draw.circle(screen, color, CIRCLES_POS[0], CIRCLR_R, CIRCLR_W) 93 pygame.draw.rect(screen, 94 color, 95 Rect(77, 340, 5, 23 )) 96 pygame.draw.rect(screen, 97 color, 98 Rect(88, 340, 5, 23 )) 99 100 def button_perfer(screen, color): 101 pygame.draw.circle(screen, color, CIRCLES_POS[1], CIRCLR_R, CIRCLR_W) 102 points=[(138,340),(162,340),(150,363)] 103 pygame.draw.polygon(screen,color,points) 104 105 def button_del(screen, color): 106 pygame.draw.circle(screen, color, CIRCLES_POS[2], CIRCLR_R, CIRCLR_W) 107 pygame.draw.circle(screen, color, (215, 340), 6, 3) 108 pygame.draw.rect(screen, 109 color, 110 Rect(200, 340, 30, 6 )) 111 pygame.draw.rect(screen, 112 color, 113 Rect(204, 340, 3, 20 )) 114 pygame.draw.rect(screen, 115 color, 116 Rect(210, 340, 3, 20 )) 117 pygame.draw.rect(screen, 118 color, 119 Rect(217, 340, 3, 20 )) 120 pygame.draw.rect(screen, 121 color, 122 Rect(223, 340, 3, 20 )) 123 pygame.draw.rect(screen, 124 color, 125 Rect(204, 360, 22, 5 )) 126 127 def button_next_song(screen, color): 128 pygame.draw.circle(screen, color, CIRCLES_POS[3], CIRCLR_R, CIRCLR_W) 129 points_one =[(270,343),(270,357),(277,350)] 130 points_two =[(277,343),(277,357),(284,350)] 131 pygame.draw.polygon(screen,color,points_one) 132 pygame.draw.polygon(screen,color,points_two) 133 pygame.draw.rect(screen, 134 color, 135 Rect(284, 343, 5, 15 )) 136 137 def button_authon_image(screen, color): 138 pygame.draw.circle(screen, color, CIRCLES_POS[4], CIRCLR_R, CIRCLR_W) 139 pygame.draw.rect(screen, 140 (255, 255, 255), 141 Rect(418, 248, 144, 154 )) 142 screen.blit(author_image, (420, 250)) 143 luck = font_song_title.render('Yes, You are Luck :-)', True, (255,165,10)) 144 screen.blit(luck, (280, 416)) 145 146 def listener(): 147 global PLAY_FLAG 148 global PREFER_FLAG 149 x, y = pygame.mouse.get_pos() 150 color = (255,255,25) 151 color_red = (230, 0, 0) 152 for index in range(len(CIRCLES_POS)): 153 p_x = (CIRCLES_POS[index][0] - x)**2 154 p_y = (CIRCLES_POS[index][1] - y)**2 155 p_r = (CIRCLR_R)**2 156 if (p_x + p_y <= p_r): 157 if index == 0 and PLAY_FLAG: 158 button_stop(screen, color) 159 elif index == 0 and not PLAY_FLAG: 160 button_play(screen, color) 161 elif index == 1 and PREFER_FLAG: 162 button_perfer(screen, color) 163 elif index == 1 and not PREFER_FLAG: 164 button_perfer(screen, color_red) 165 elif index == 2: 166 button_del(screen, color) 167 elif index == 3: 168 button_next_song(screen, color) 169 elif index == 4: 170 button_authon_image(screen, color) 171 172 def mouse_down_listener(sound): 173 global PLAY_FLAG 174 global PREFER_FLAG 175 global SONG_FLAG 176 x, y = pygame.mouse.get_pos() 177 for index in range(len(CIRCLES_POS)): 178 p_x = (CIRCLES_POS[index][0] - x)**2 179 p_y = (CIRCLES_POS[index][1] - y)**2 180 p_r = (CIRCLR_R)**2 181 if (p_x + p_y <= p_r): 182 if index == 0 and PLAY_FLAG: 183 #print('stop now......') 184 sound.stop() 185 PLAY_FLAG = False 186 elif index == 0 and not PLAY_FLAG: 187 #print('play now ... ... ... ...') 188 sound.play(0) 189 PLAY_FLAG = True 190 elif index == 1 and PREFER_FLAG: 191 print('perfer song....<<', SONGS[SONG_FLAG][1], '>>') 192 PREFER_FLAG = False 193 elif index == 1 and not PREFER_FLAG: 194 print('not perfer song... <<', SONGS[SONG_FLAG][1], '>>') 195 PREFER_FLAG = True 196 elif index == 2: 197 sound.stop() 198 print('delete song....<<', SONGS[SONG_FLAG][1], '>>') 199 if SONG_FLAG > 0: 200 SONGS.pop(SONG_FLAG) 201 SONG_IMAGE.pop(SONG_FLAG) 202 SONG_ARRAY.pop(SONG_FLAG) 203 if SONG_FLAG >= len(SONGS) - 1: 204 SONG_FLAG -= 1 205 else: 206 print('This is the last song.') 207 elif index == 3: 208 sound.stop() 209 if SONG_FLAG < len(SONGS) - 1: 210 SONG_FLAG += 1 211 else: 212 SONG_FLAG = 0 213 #print('next song....') 214 215 def draw_button(sound): 216 color = (255,255,255) 217 color_red = (230, 0, 0) 218 #play or stop 219 if PLAY_FLAG: 220 sound.play(0) 221 button_stop(screen, color) 222 elif not PLAY_FLAG: 223 button_play(screen, color) 224 #perfer song 225 if PREFER_FLAG: 226 button_perfer(screen, color) 227 elif not PREFER_FLAG: 228 button_perfer(screen, color_red) 229 #delete 230 button_del(screen, color) 231 #next song 232 button_next_song(screen, color) 233 234 def draw_volume_info(): 235 #the background of volume 236 pygame.draw.rect(screen, 237 (255, 255, 255), 238 Rect((VOLUME_POINTS_START[-1][0], 239 VOLUME_POINTS_START[-1][1]), 240 (VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0], 241 20))) 242 #the size of volume 243 for v in range(VOLUME+1): 244 if v > 0: 245 pygame.draw.rect(screen, 246 VOLUME_RECT_COLORS[v], 247 Rect((VOLUME_POINTS_START[-v][0], 248 VOLUME_POINTS_START[-v][1]), 249 (VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0], 250 20))) 251 252 def draw_song_title(): 253 title = font_song_title.render(SONGS[SONG_FLAG][1], True, (255,165,0)) 254 songer = font_song.render(SONGS[SONG_FLAG][2], True, (255, 255, 255)) 255 screen.blit(title, (320, 60)) 256 screen.blit(songer, (320, 110)) 257 258 def draw_state_bar_info(): 259 pygame.draw.line(screen, (165,42,42),(0, SCREEN_H), (SCREEN_W, SCREEN_H)) 260 #music info 261 music_info = 'AllSongs: ' + str(len(SONGS)) +' Current: ' + str(SONG_FLAG + 1) 262 text = font.render(music_info, True, (255,255,255)) 263 screen.blit(text, (0, SCREEN_H+5)) 264 #author into 265 author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255)) 266 screen.blit(author_info, (SCREEN_W - 160, SCREEN_H+5)) 267 #volume info 268 volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255)) 269 screen.blit(volume_text, (SCREEN_W - 310, SCREEN_H+5)) 270 271 while True: 272 273 screen.blit(bg, (0, 0)) 274 pic = SONG_IMAGE[SONG_FLAG] 275 bg_sound = SONG_ARRAY[SONG_FLAG] 276 bg_sound.set_volume(0.1 * VOLUME) 277 draw_button(bg_sound) 278 listener() 279 for event in pygame.event.get(): 280 if event.type == QUIT: 281 bg_sound.stop() 282 exit() 283 elif event.type == KEYDOWN: 284 if event.key == K_UP: 285 pass 286 elif event.key == K_DOWN: 287 pass 288 elif event.key == K_LEFT: 289 if VOLUME > 0: 290 VOLUME -= 1 291 elif event.key == K_RIGHT: 292 if VOLUME <= 9: 293 VOLUME += 1 294 elif event.type == MOUSEMOTION: 295 pass 296 297 elif event.type == MOUSEBUTTONDOWN: 298 pressed_array = pygame.mouse.get_pressed() 299 for index in range(len(pressed_array)): 300 if pressed_array[index]: 301 if index == 0: #When the LEFT button down 302 mouse_down_listener(bg_sound) 303 304 #picture rect 305 draw_picture_rect() 306 #volume information 307 draw_volume_info() 308 #state bar information 309 draw_state_bar_info() 310 #song title 311 draw_song_title() 312 313 screen.blit(pic, (62.5, 62.5)) 314 pygame.display.update()
你在運行的時候,肯恩會遇到一些問題,系統找不到音樂和圖片....
這個由於文件中的音樂,字體和圖片有一點大,所以我把圖片和音樂刪掉..你可以進行添加即可
還請大家給我想想辦法,我好上傳源文件給大家伙下載...
========================================================
More reading,and english is important.
I'm Hongten
大哥哥大姐姐,覺得有用打賞點哦!多多少少沒關系,一分也是對我的支持和鼓勵。謝謝。
Hongten博客排名在100名以內。粉絲過千。
Hongten出品,必是精品。
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten
========================================================