Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson04_diy
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
33945f6f
authored
Jun 14, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
2ffb9ec5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
104 additions
and
6 deletions
my_music.py
my_music.py
View file @
33945f6f
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
os
#调用Python的系统模块,用于对电脑操作,该程序是读取歌曲文件
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建窗口
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
click
=
0
#鼠标点击次数,用来确定是播放还是暂停
volume
=
0.2
#设置音量变量,实现音量可调节
pygame
.
mixer
.
music
.
set_volume
(
volume
)
#设置初始音量
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
12
)
#设置字体样式和大小
# 载入图片、资源
# 载入图片、资源
bg_img
=
pygame
.
image
.
load
(
'background.png'
)
# 背景图
bg_img
=
pygame
.
image
.
load
(
'background.png'
)
# 背景图
play_img
=
pygame
.
image
.
load
(
'play.png'
)
# 播放按钮
play_img
=
pygame
.
image
.
load
(
'play.png'
)
# 播放按钮
...
@@ -12,15 +17,108 @@ last_img = pygame.image.load('last.png') # 上一曲按钮
...
@@ -12,15 +17,108 @@ last_img = pygame.image.load('last.png') # 上一曲按钮
next_img
=
pygame
.
image
.
load
(
'next.png'
)
# 下一曲按钮
next_img
=
pygame
.
image
.
load
(
'next.png'
)
# 下一曲按钮
logo_img
=
pygame
.
image
.
load
(
'logo.png'
)
# 中间logo
logo_img
=
pygame
.
image
.
load
(
'logo.png'
)
# 中间logo
#music_list1=['歌曲1.wav','歌曲2.wav','歌曲3.wav','歌曲4.ogg']#定义歌单名字列表
music_list
=
[]
#定义新列表,保存本地读取的歌曲文件
num
=-
1
#定义一个变量,用来记数播放歌曲的索引位置
play_button
=
stop_img
#初始化一个按钮形状变量,实现播放、暂停图片切换
path
=
"D:
\\
Users
\\
MDS
\\
PycharmProjects
\\
p2
\\
音乐播放器"
#歌曲本地地址
filelist
=
os
.
listdir
(
path
)
#导入指定文件夹全部内容,保存为列表
for
i
in
filelist
:
#遍历列表,找出歌曲文件,方法,识别后缀名
if
i
[
-
4
:]
==
'.wav'
or
i
[
-
4
:]
==
'.ogg'
:
music_list
.
append
(
i
)
#将歌曲装进歌曲列表
angle
=
0
#唱盘旋转角度初始化
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
#检测是否关闭窗口
????
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
#检测是否按下按键
if
event
.
key
==
locals
.
K_UP
:
#检测是否按下上键
volume
+=
0.1
if
volume
>
1
:
#音量>1,设置为1,因为音量>和=1都是全音量
volume
=
1
pygame
.
mixer
.
music
.
set_volume
(
volume
)
#重新设置音量
if
event
.
key
==
locals
.
K_DOWN
:
#检测是否按下上键
volume
-=
0.1
if
volume
<
0
:
#音量<0,设置为0
volume
=
0
pygame
.
mixer
.
music
.
set_volume
(
volume
)
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
#检测鼠标是否按下,注意鼠标和按键是不同的event.type
if
event
.
button
==
1
:
#是否点击鼠标左键;鼠标右键是3
x
,
y
=
event
.
pos
#把鼠标的坐标赋值给x,y
if
x
>
270
and
x
<
370
and
y
>
330
and
y
<
430
:
#坐标在图片坐标范围内
click
+=
1
#记录点击次数
if
click
%
2
==
0
:
#默认一开始就是播放,所以点击奇数次就是暂停,偶数次就是播放
pygame
.
mixer
.
music
.
unpause
()
#播放
play_button
=
stop_img
#播放显示暂停图片
bfzt
=
1
#播放时播放状态为1
else
:
pygame
.
mixer
.
music
.
pause
()
#暂停
play_button
=
play_img
#暂停显示播放图片
bfzt
=
0
#暂停时播放状态为0
if
x
>
120
and
x
<
220
and
y
>
350
and
y
<
400
:
#上一曲功能
num
-=
1
if
num
<
0
:
num
=
len
(
music_list
)
-
1
click
=
0
#重新赋值点击次数,实现点击上一曲,就直接播放上一曲
play_button
=
stop_img
#暂停显示播放图片
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
#加载本地音乐,注意:路径和歌曲之间也有层级
pygame
.
mixer
.
music
.
play
()
#播放音乐
#获取歌曲总时长,有播放歌曲的地方都要添加这段代码
music
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
#重新用mixer完整加载音乐 ,统计时长
music_len
=
music
.
get_length
()
#获取总时长
music_len_min
=
int
(
music_len
)
//
60
#总时长分钟
music_len_sec
=
int
(
music_len
)
%
60
#总时长秒钟
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
400
:
#下一曲功能
num
+=
1
if
num
>
len
(
music_list
)
-
1
:
#大于最后一首歌的索引
num
=
0
#回到第一首歌的索引
click
=
0
#重新赋值点击次数,实现点击上一曲,就直接播放上一曲
play_button
=
stop_img
#暂停显示播放图片
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
#加载本地音乐,注意:路径和歌曲之间也有层级
pygame
.
mixer
.
music
.
play
()
#播放音乐
music
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
#重新用mixer完整加载音乐 ,统计时长
music_len
=
music
.
get_length
()
#获取总时长
music_len_min
=
int
(
music_len
)
//
60
#总时长分钟
music_len_sec
=
int
(
music_len
)
%
60
#总时长秒钟
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
#检测歌曲是否播放完了,播放中=True,
num
+=
1
#播放完num加1
if
num
>
len
(
music_list
)
-
1
:
#如果超出歌单列表,重新num=0
num
=
0
#pygame.mixer.music.load(music_list1[num]) #加载对应music_list1[]中的音乐
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
#加载本地音乐,注意:路径和歌曲之间也有层级
pygame
.
mixer
.
music
.
play
()
#播放音乐
music
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
#重新用mixer完整加载音乐 ,统计时长
music_len
=
music
.
get_length
()
#获取总时长
music_len_min
=
int
(
music_len
)
//
60
#总时长分钟
music_len_sec
=
int
(
music_len
)
%
60
#总时长秒钟
new_logo
=
pygame
.
transform
.
rotate
(
logo_img
,
angle
)
#设置旋转图片及旋转角度,并保存为new_logo
#angle+=1#每次旋转一度
if
play_button
==
stop_img
:
#播放按钮显示暂停图片,也就是正在播放中,实现了播放旋转,暂停不旋转
angle
+=
1
#每次旋转1度
new_rect
=
new_logo
.
get_rect
(
center
=
(
320
,
200
))
#获取新logo围绕中心点(320,200)旋转的坐标和图片大小
pos
=
(
new_rect
[
0
],
new_rect
[
1
])
#元组前两位是坐标,后两位是图片大小,把坐标赋值给pos
playtime
=
pygame
.
mixer
.
music
.
get_pos
()
#获取播放时间,返回值是整数毫秒,
min
=
int
(
playtime
/
1000
)
//
60
#换算成秒,取整(返回值经常不能被1000整除),/60取整即为分钟
sec
=
int
(
playtime
/
1000
)
%
60
#换算成秒,取整(返回值经常不能被1000整除),%60取模即为秒钟
if
sec
<
10
:
#优化秒钟显示,始终保持2位数显示
sec
=
'0'
+
str
(
sec
)
if
music_len_sec
<
10
:
#优化秒钟显示,始终保持2位数显示
music_len_sec
=
'0''0'
+
str
(
music_len_sec
)
info
=
str
(
min
)
+
':'
+
str
(
sec
)
+
' /'
+
str
(
music_len_min
)
+
':'
+
str
(
music_len_sec
)
#分钟和秒钟连接起来
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
#文字渲染
# 绘制画面
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
stop_img
,
(
270
,
330
))
# 暂停
按钮
screen
.
blit
(
play_button
,
(
270
,
330
))
# 刷新暂停/播放
按钮
screen
.
blit
(
logo_img
,
(
170
,
60
)
)
# 中间logo图
screen
.
blit
(
new_logo
,
pos
)
# 中间logo图
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
screen
.
blit
(
text
,(
140
,
400
))
# 刷新画面
# 刷新画面
???
pygame
.
display
.
update
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment