pygame系列_原创百度随心听音乐播放器_完整版

程序名:PyMusic

解释:pygame+music

之前发布了自己写的小程序:百度随心听音乐播放器的一些效果图

你可以去到这里再次看看效果:

pygame系列_百度随心听_完美的UI设计

这个程序的灵感来自于百度随心听

hongten_pymusic

说明:

       动作按钮全部是画出来的,没有用到任何图片

       用到图片的只有:背景,歌手图片,作者图片

经过一个阶段的调试,现在算是可以拿上台面和大家交流

hongten_pymusic

 

功能介绍:

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)

=======================================

下面我说说我做的思路吧

我想有一部分朋友会喜欢的...:)

首先,主题功能是音乐播放,那么我们就应该实现这个音乐播放的功能

哈哈,这个灵感来自于我之前写的小游戏:

pygame系列_小球完全弹性碰撞游戏_源码下载

在这个游戏中,我实现了音乐的播放,于是乎我就想在音乐播放上面做一些文章...

接下来是程序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

========================================================

posted @ 2013-10-23 23:41  Hongten  阅读(8157)  评论(5编辑  收藏  举报
Fork me on GitHub