Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson5_diy1
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
a76d4178
authored
Jun 11, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
f81f7074
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
12 deletions
my_music.py
my_music.py
View file @
a76d4178
import
pygame
from
pygame
import
locals
import
os
# 导入os模块
pygame
.
init
()
# 初始化
# 创建窗口
...
...
@@ -12,9 +13,16 @@ last_img = pygame.image.load('last.png') # 上一曲按钮
next_img
=
pygame
.
image
.
load
(
'next.png'
)
# 下一曲按钮
logo_img
=
pygame
.
image
.
load
(
'logo.png'
)
# 下一曲按钮
# 载入音乐
pygame
.
mixer
.
music
.
load
(
'歌曲4.ogg'
)
# 载入音乐
# 音乐
music_list
=
[]
# 建立空列表music_list
path
=
"C:
\\
Users
\\
HP
\\
Desktop
\\
test"
# 文件路径赋值给变量path(单斜线改为双斜线)
filelist
=
os
.
listdir
(
path
)
# 用os.listdir()方法读取文件夹的文件并赋值给变量filelist
for
i
in
filelist
:
# for循环
# if判断两种格式文件是否与i[-4]相等,用or组合
if
i
[
-
4
:]
==
".ogg"
or
i
[
-
4
:]
==
".wav"
:
music_list
.
append
(
i
)
# 用append()方法参数为i将test文件夹歌曲填进列表muisc_list里
num
=
-
1
# 建立变量num设为-1
volume
=
0.2
pygame
.
mixer
.
music
.
set_volume
(
volume
)
# 初始播放音量
click
=
0
# 记录鼠标点击次数
...
...
@@ -39,17 +47,46 @@ while True:
# 按下鼠标
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
# 次数增加
click
+=
1
if
click
%
2
==
0
:
play_button
=
stop_img
# 暂停按钮
pygame
.
mixer
.
music
.
unpause
()
else
:
play_button
=
play_img
# 播放按钮
pygame
.
mixer
.
music
.
pause
()
if
event
.
button
==
1
:
# if判断鼠标左击
x
,
y
=
event
.
pos
# event.pos赋值给坐标x, y
# if判断鼠标点击位置是否在播放暂停图标范围里
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
# 次数增加
click
+=
1
if
click
%
2
==
0
:
play_button
=
stop_img
# 暂停按钮
pygame
.
mixer
.
music
.
unpause
()
else
:
play_button
=
play_img
# 播放按钮
pygame
.
mixer
.
music
.
pause
()
# if判断鼠标点击位置是否在上一曲图标范围里
if
x
>
120
and
x
<
220
and
y
>
350
and
y
<
400
:
#上一曲
num
-=
1
# 变量num值减少1
if
num
<
0
:
# if判断num<0
num
=
len
(
music_list
)
-
1
# 列表长度-1并赋值给变量num
# 通过load()方法参数为path + "\\"+music_list[num]载入音乐
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
pygame
.
mixer
.
music
.
play
()
# 播放音乐
play_button
=
stop_img
# 暂停按钮
click
=
0
# 将鼠标点击次数变量click设为0
# if判断鼠标点击位置是否在下一曲图标范围里
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
400
:
#下一曲
num
+=
1
# 变量num值增加1
if
num
>
len
(
music_list
)
-
1
:
# if判断 变量num>列表长度-1
num
=
0
# 将变量num设为0
# 通过load()方法参数为path + "\\"+music_list[num]载入音乐
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
pygame
.
mixer
.
music
.
play
()
# 播放音乐
play_button
=
stop_img
# 暂停按钮
click
=
0
# 将鼠标点击次数变量click设为0
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
pygame
.
mixer
.
music
.
play
()
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
# if判断音乐是否正在播放
num
+=
1
# 变量num值增加1
if
num
>
len
(
filelist
)
-
1
:
# if判断变量num>列表filelist长度-1
num
=
0
# 将变量num设为0
# 通过load()方法参数为path + "\\"+music_list[num]载入音乐
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
pygame
.
mixer
.
music
.
play
()
# 播放音乐
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
...
...
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