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

时间:2021-06-29 19:26:38

程序名:PyMusic

解释:pygame+music

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

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

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

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

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

说明:

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

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

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

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

功能介绍:

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.音量图形的初始化

 VOLUME_POINTS = []
VOLUME_POINTS_START = []
VOLUME_RECT_COLORS = []
for p in range(170, 250, 7):
VOLUME_POINTS.append([SCREEN_W - p,SCREEN_H + 20])
for ps in range(175, 250, 7):
VOLUME_POINTS_START.append([SCREEN_W - ps, SCREEN_H])
VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))

2.加载歌曲和图片

 SONG_ARRAY = []
SONG_IMAGE = []
for song in range(len(SONGS)):
SONG_ARRAY.append(pygame.mixer.Sound(os.path.join(DATA_DIR, SOUND_DIR, SONGS[song][0])))
SONG_IMAGE.append(pygame.image.load(os.path.join(DATA_DIR, IMAGE_DIR, SONGS[song][3])).convert())

3.字体加载

 font = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'TORK____.ttf'), 14)
font_song_title = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyhbd.ttf'), 24)
font_song = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyh.ttf'), 16)

4.停止/播放按钮

 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) 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 ))

5.删除歌曲按钮

 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 ))

6.作者信息展示

 def button_authon_image(screen, color):
pygame.draw.circle(screen, color, CIRCLES_POS[4], CIRCLR_R, CIRCLR_W)
pygame.draw.rect(screen,
(255, 255, 255),
Rect(418, 248, 144, 154 ))
screen.blit(author_image, (420, 250))
luck = font_song_title.render('Yes, You are Luck :-)', True, (255,165,10))
screen.blit(luck, (280, 416))

7.第一个监听器

 def listener():
global PLAY_FLAG
global PREFER_FLAG
x, y = pygame.mouse.get_pos()
color = (255,255,25)
color_red = (230, 0, 0)
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:
button_stop(screen, color)
elif index == 0 and not PLAY_FLAG:
button_play(screen, color)
elif index == 1 and PREFER_FLAG:
button_perfer(screen, color)
elif index == 1 and not PREFER_FLAG:
button_perfer(screen, color_red)
elif index == 2:
button_del(screen, color)
elif index == 3:
button_next_song(screen, color)
elif index == 4:
button_authon_image(screen, color)

8.第二个监听器

 def mouse_down_listener(sound):
global PLAY_FLAG
global PREFER_FLAG
global SONG_FLAG
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:
#print('stop now......')
sound.stop()
PLAY_FLAG = False
elif index == 0 and not PLAY_FLAG:
#print('play now ... ... ... ...')
sound.play(0)
PLAY_FLAG = True
elif index == 1 and PREFER_FLAG:
print('perfer song....<<', SONGS[SONG_FLAG][1], '>>')
PREFER_FLAG = False
elif index == 1 and not PREFER_FLAG:
print('not perfer song... <<', SONGS[SONG_FLAG][1], '>>')
PREFER_FLAG = True
elif index == 2:
sound.stop()
print('delete song....<<', SONGS[SONG_FLAG][1], '>>')
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.')
elif index == 3:
sound.stop()
if SONG_FLAG < len(SONGS) - 1:
SONG_FLAG += 1
else:
SONG_FLAG = 0
#print('next song....')

9.鼠标按下事件

 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
mouse_down_listener(bg_sound)

上面的都是一些细节的地方...

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

下面是完整代码部分:

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

 #pygame music

 import os, pygame
from pygame.locals import *
from sys import exit
from random import * __des__ = '''
Name:
PyMusic
'''
__version__ = '2.0'
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'blog' : 'http://www.cnblogs.com/hongten',
'version' : __version__} if not pygame.mixer: print('Warning, sound disabled!')
if not pygame.font: print('Warning, fonts disabled!') pygame.init() SCREEN_W = 580
SCREEN_H = 450
SCREEN_DEFAULT_SIZE = (SCREEN_W, SCREEN_H + 20)
VOLUME = 5
IMAGE_START_POS = (60, 60)
IMAGE_END_POS = (245, 245)
CIRCLES_POS = [(85, 350), (150, 350), (215, 350), (280, 350), (555, 425)]
CIRCLR_R = 25
CIRCLR_W = 3 PLAY_FLAG = True
PREFER_FLAG = True DATA_DIR = 'data'
IMAGE_DIR = 'image'
BG_IMAGE_DIR = 'image\\background'
FONT_DIR = 'font'
SOUND_DIR = 'sound' BG_IMAGE = 'bg.jpg'
AUTHOR_IMAGE = 'author.png'
#size:(240*240)
BGS = []
SONG_FLAG = 0
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')]
westlift = 'westlife.png' VOLUME_POINTS = []
VOLUME_POINTS_START = []
VOLUME_RECT_COLORS = []
for p in range(170, 250, 7):
VOLUME_POINTS.append([SCREEN_W - p,SCREEN_H + 20])
for ps in range(175, 250, 7):
VOLUME_POINTS_START.append([SCREEN_W - ps, SCREEN_H])
VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255))) screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
bg = pygame.image.load(os.path.join(DATA_DIR, BG_IMAGE_DIR, BG_IMAGE)).convert()
author_image = pygame.image.load(os.path.join(DATA_DIR, IMAGE_DIR, AUTHOR_IMAGE)).convert() SONG_ARRAY = []
SONG_IMAGE = []
for song in range(len(SONGS)):
SONG_ARRAY.append(pygame.mixer.Sound(os.path.join(DATA_DIR, SOUND_DIR, SONGS[song][0])))
SONG_IMAGE.append(pygame.image.load(os.path.join(DATA_DIR, IMAGE_DIR, SONGS[song][3])).convert()) font = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'TORK____.ttf'), 14)
font_song_title = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyhbd.ttf'), 24)
font_song = pygame.font.Font(os.path.join(DATA_DIR, FONT_DIR, 'msyh.ttf'), 16) def draw_picture_rect():
#picture rect
pygame.draw.rect(screen,
(255, 255, 255),
Rect(IMAGE_START_POS, IMAGE_END_POS)) 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) 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 )) 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) 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 )) 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 )) def button_authon_image(screen, color):
pygame.draw.circle(screen, color, CIRCLES_POS[4], CIRCLR_R, CIRCLR_W)
pygame.draw.rect(screen,
(255, 255, 255),
Rect(418, 248, 144, 154 ))
screen.blit(author_image, (420, 250))
luck = font_song_title.render('Yes, You are Luck :-)', True, (255,165,10))
screen.blit(luck, (280, 416)) def listener():
global PLAY_FLAG
global PREFER_FLAG
x, y = pygame.mouse.get_pos()
color = (255,255,25)
color_red = (230, 0, 0)
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:
button_stop(screen, color)
elif index == 0 and not PLAY_FLAG:
button_play(screen, color)
elif index == 1 and PREFER_FLAG:
button_perfer(screen, color)
elif index == 1 and not PREFER_FLAG:
button_perfer(screen, color_red)
elif index == 2:
button_del(screen, color)
elif index == 3:
button_next_song(screen, color)
elif index == 4:
button_authon_image(screen, color) def mouse_down_listener(sound):
global PLAY_FLAG
global PREFER_FLAG
global SONG_FLAG
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:
#print('stop now......')
sound.stop()
PLAY_FLAG = False
elif index == 0 and not PLAY_FLAG:
#print('play now ... ... ... ...')
sound.play(0)
PLAY_FLAG = True
elif index == 1 and PREFER_FLAG:
print('perfer song....<<', SONGS[SONG_FLAG][1], '>>')
PREFER_FLAG = False
elif index == 1 and not PREFER_FLAG:
print('not perfer song... <<', SONGS[SONG_FLAG][1], '>>')
PREFER_FLAG = True
elif index == 2:
sound.stop()
print('delete song....<<', SONGS[SONG_FLAG][1], '>>')
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.')
elif index == 3:
sound.stop()
if SONG_FLAG < len(SONGS) - 1:
SONG_FLAG += 1
else:
SONG_FLAG = 0
#print('next song....') 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) def draw_volume_info():
#the background of volume
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:
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))) def draw_song_title():
title = font_song_title.render(SONGS[SONG_FLAG][1], True, (255,165,0))
songer = font_song.render(SONGS[SONG_FLAG][2], True, (255, 255, 255))
screen.blit(title, (320, 60))
screen.blit(songer, (320, 110)) def draw_state_bar_info():
pygame.draw.line(screen, (165,42,42),(0, SCREEN_H), (SCREEN_W, SCREEN_H))
#music info
music_info = 'AllSongs: ' + str(len(SONGS)) +' Current: ' + str(SONG_FLAG + 1)
text = font.render(music_info, True, (255,255,255))
screen.blit(text, (0, SCREEN_H+5))
#author into
author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
screen.blit(author_info, (SCREEN_W - 160, SCREEN_H+5))
#volume info
volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255))
screen.blit(volume_text, (SCREEN_W - 310, SCREEN_H+5)) 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:
if event.key == K_UP:
pass
elif event.key == K_DOWN:
pass
elif event.key == K_LEFT:
if VOLUME > 0:
VOLUME -= 1
elif event.key == K_RIGHT:
if VOLUME <= 9:
VOLUME += 1
elif event.type == MOUSEMOTION:
pass 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
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() screen.blit(pic, (62.5, 62.5))
pygame.display.update()

你在运行的时候,肯恩会遇到一些问题,系统找不到音乐和图片....

这个由于文件中的音乐,字体和图片有一点大,所以我把图片和音乐删掉..你可以进行添加即可

还请大家给我想想办法,我好上传源文件给大家伙下载...

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

More reading,and english is important.

I'm Hongten

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

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

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

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