cool hit counter pygame series_original Baidu listen to music player_full version_Intefrankly

pygame series_original Baidu listen to music player_full version


Program Name.PyMusic

Explanation: pygame+music

Previously posted an applet written by myself at Baidu ListenSome effects of the music player

You can go here to see the results again at.

pygame series_Baidu Listen_ perfectUI design

This program was inspired by the Baidu Listen

Description.

The action buttons are all drawn and no images are used

The only images used are: background, singer image, author image

After a phase of debugging, it is now considered to be ready to take to the stage and share with you

Features.

A. Introduction to the four buttons

1.Complete stop, play music function

2.Like/dislike the music currently playing

3.Delete the currently playing music (it is not deleted in the physical environment, it is deleted in memory)

4.next song

5.When the mouse passes over a button, the button color changes

B. Status bar (bottom)

1.Show all songs (AllSongs)

2.The song currently playing is the first song

3.Current volume (range: 0 - 10)

4.Graphical display of volume (no picture used here, but drawn by the system :-))

5.My email information: hongtenzone@foxmail.com

C. Lower right corner

1. When the mouse moves to the yellow circle area, It will showcase me The photos and'Yes,You are Luck:)' model or template character

It automatically hides when the mouse is turned on (I think those of you who have come into contact with android phones may have experienced this, there is a place in the tools

When the three clicks, would appearandroid One of the pictures inside.... right me Here's where the inspiration came fromll)

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

Here's my thoughts on how I did it

me I think some of you will like it....:)

firstly, Theme function is music playback, or so me We should implement this music playing function

(onom.) laughing out loud, This one was inspired by the me Previously written mini-games:

pygame series_Ball Complete Elasticity Collision Game_Source Code Download

In this game, me Enables music to be played, therefore me Just trying to do something with the music playing...

Next is the program PyMusic's theme interface needs to be considered, I personally like to listen to songs... Baidu Music, Baidu Listen are the places to go...

The interface design of Baidu Listen With Me is simple and suits my style... So I chose to listen to Baidu...

therefore me Look, look. Baidu Listen code... One accidentally gets me Probably see the logic between the buttons....

For a moment, the prototype PyMusic came to mind ....

Why not make one?? me Just ask. me themselves, Slowly coming to mindPyMusic prototype...

Then started putting pen to paper and drew the pyMusic prototype on paper ....

Once the painting is done, the analysis begins... (The process is a little long...)

Then take the objects in the prototype (buttons, images loaded... ) One by one...

And finally add my little confidence... Oh, it's done...

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

Some details

Some details to keep in mind while doing PyMusic

me Here's a list of those places

1.Initialization of volume graphics

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.Loading songs and pictures

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.Font Loading

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.Stop/Play button

 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.Delete song button

 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.Author Information Display

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.The first listener

 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.Second listener

 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.Mouse press event

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)

The ones above are Some details destination...

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

The following is the complete code section.

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

  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', ' Incomplete melodies', ' Wang Lee-Hom (1976-), Taiwanese-American singer', '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', ' me ' , ' Leslie Cheung', '5.png'),
 53          ('6.OGG', ' great city, small love' , ' Wang Lee-Hom (1976-), Taiwanese-American singer', '6.png'),
 54          ('7.OGG', ' chats' , ' Guo Jing (1902-1976), Chinese writer', '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()

You're running into some problems with Kenn, the system can't find music and pictures ....

This one is a little big due to the music, font and pictures in the file, so I removed the pictures and music... You can just add it

Please also give me some ideas so I can upload the source file for you guys to download...

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten


Recommended>>
1、Python tells me how unreliable the Paris subway lines are
2、CSS3 Magic Hall Getting to know fontface and FontIcon
3、Random thoughts on the design package export interface
4、Android multithreading of HandlerThread source code analysis
5、Consistent hash principles and implementation

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号