Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson6_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
941f7002
authored
Aug 06, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
a5560ba9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
18 deletions
my_music.py
my_music.py
View file @
941f7002
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
os
import
os
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建窗口
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
# 载入图片
# 载入图片
pygame
.
display
.
set_mode
(
size
=
(
0
,
0
),
flags
=
0
,
depth
=
0
)
pygame
.
display
.
set_caption
(
"python音乐播放器"
)
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'
)
# 播放按钮
stop_img
=
pygame
.
image
.
load
(
'stop.png'
)
# 暂停按钮
stop_img
=
pygame
.
image
.
load
(
'stop.png'
)
# 暂停按钮
last_img
=
pygame
.
image
.
load
(
'last.png'
)
# 上一曲按钮
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图
bgsss
=
pygame
.
image
.
load
(
'G:
\\
python音乐播放器
\\
bgt.png'
)
basic_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
14
)
# 字体
basic_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
14
)
# 字体
b_font
=
pygame
.
font
.
Font
(
'SIMFANG.TTF'
,
40
)
b_font
=
pygame
.
font
.
Font
(
'SIMFANG.TTF'
,
40
)
music_list
=
[]
music_list
=
[]
path
=
"G:
\\
level3-lesson6-diy2
\
\
music"
path
=
r"G:\python音乐播放器
\music"
music_list
=
os
.
listdir
(
path
)
music_list
=
os
.
listdir
(
path
)
f
=
os
.
listdir
(
path
)
f
=
os
.
listdir
(
path
)
num
=
-
1
num
=
-
1
...
@@ -24,6 +26,7 @@ pygame.mixer.music.set_volume(volume) # 音乐播放器音量
...
@@ -24,6 +26,7 @@ pygame.mixer.music.set_volume(volume) # 音乐播放器音量
click
=
0
# 初始点击次数
click
=
0
# 初始点击次数
play_button
=
stop_img
play_button
=
stop_img
angle
=
0
angle
=
0
bfzt
=
"列表播放"
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
...
@@ -40,7 +43,6 @@ while True:
...
@@ -40,7 +43,6 @@ while True:
if
volume
<
0
:
if
volume
<
0
:
volume
=
0
volume
=
0
pygame
.
mixer
.
music
.
set_volume
(
volume
)
pygame
.
mixer
.
music
.
set_volume
(
volume
)
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
if
event
.
button
==
1
:
# 左击
if
event
.
button
==
1
:
# 左击
x
,
y
=
event
.
pos
x
,
y
=
event
.
pos
...
@@ -52,7 +54,7 @@ while True:
...
@@ -52,7 +54,7 @@ while True:
else
:
else
:
pygame
.
mixer
.
music
.
pause
()
pygame
.
mixer
.
music
.
pause
()
play_button
=
play_img
play_button
=
play_img
if
x
>
120
and
x
<
220
and
y
>
350
and
y
<
400
:
#上一曲
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
num
=
len
(
music_list
)
-
1
...
@@ -64,6 +66,16 @@ while True:
...
@@ -64,6 +66,16 @@ while True:
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
music_length
=
music1
.
get_length
()
music_length
=
music1
.
get_length
()
music_length
=
int
(
music_length
)
music_length
=
int
(
music_length
)
if
x
>
477
and
x
<
526
and
y
>
177
and
y
<
212
and
bfzt
==
"列表播放"
and
bfzt
!=
'单曲循环'
:
bfzt
=
"单曲循环"
print
(
bfzt
)
elif
x
>
477
and
x
<
526
and
y
>
177
and
y
<
212
and
bfzt
==
"单曲循环"
and
bfzt
!=
'列表播放'
:
print
(
"已经是单曲循环!"
)
if
x
>
122
and
x
<
177
and
y
>
176
and
y
<
209
and
bfzt
==
"单曲循环"
and
bfzt
!=
'列表播放'
:
bfzt
=
"列表播放"
print
(
bfzt
)
elif
x
>
122
and
x
<
177
and
y
>
176
and
y
<
209
and
bfzt
==
"列表播放"
and
bfzt
!=
'单曲循环'
:
print
(
"已经是列表播放!"
)
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
400
:
#下一曲
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
400
:
#下一曲
num
+=
1
num
+=
1
if
num
>
len
(
music_list
)
-
1
:
if
num
>
len
(
music_list
)
-
1
:
...
@@ -76,17 +88,20 @@ while True:
...
@@ -76,17 +88,20 @@ while True:
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
music_length
=
music1
.
get_length
()
music_length
=
music1
.
get_length
()
music_length
=
int
(
music_length
)
music_length
=
int
(
music_length
)
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
# 音乐是否正在播放
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
# 音乐是否正在播放
num
+=
1
if
bfzt
==
"列表播放"
:
if
num
>
len
(
music_list
)
-
1
:
num
+=
1
num
=
0
if
num
>
len
(
music_list
)
-
1
:
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
num
=
0
pygame
.
mixer
.
music
.
play
()
# 播放音乐
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
# 歌曲的时间
pygame
.
mixer
.
music
.
play
()
# 播放音乐
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
# 歌曲的时间
music_length
=
music1
.
get_length
()
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_list
[
num
])
music_length
=
int
(
music_length
)
music_length
=
music1
.
get_length
()
music_length
=
int
(
music_length
)
elif
bfzt
==
"单曲循环"
:
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
pygame
.
mixer
.
music
.
play
()
# 播放音乐
# 歌曲播放进度时间
# 歌曲播放进度时间
play_time
=
pygame
.
mixer
.
music
.
get_pos
()
play_time
=
pygame
.
mixer
.
music
.
get_pos
()
play_time
=
int
(
play_time
/
1000
)
play_time
=
int
(
play_time
/
1000
)
...
@@ -111,7 +126,7 @@ while True:
...
@@ -111,7 +126,7 @@ while True:
if
play_button
==
stop_img
:
if
play_button
==
stop_img
:
angle
+=
1
angle
+=
1
# 绘制画面
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
bg_img
,
(
0
,
0
))
screen
.
blit
(
play_button
,
(
270
,
330
))
# 暂停按钮
screen
.
blit
(
play_button
,
(
270
,
330
))
# 暂停按钮
screen
.
blit
(
new_logo
,
pos
)
# 中间logo图
screen
.
blit
(
new_logo
,
pos
)
# 中间logo图
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
...
@@ -121,7 +136,7 @@ while True:
...
@@ -121,7 +136,7 @@ while True:
screen
.
blit
(
scoreSurf
,
(
120
,
440
))
screen
.
blit
(
scoreSurf
,
(
120
,
440
))
scoreSurf2
=
basic_font
.
render
(
"/"
+
info2
,
True
,
(
0
,
0
,
0
))
scoreSurf2
=
basic_font
.
render
(
"/"
+
info2
,
True
,
(
0
,
0
,
0
))
screen
.
blit
(
scoreSurf2
,
(
170
,
440
))
screen
.
blit
(
scoreSurf2
,
(
170
,
440
))
ssss
=
b_font
.
render
(
f
[
num
],
True
,
(
0
,
0
,
0
)
)
ssss
=
b_font
.
render
(
f
[
num
],
True
,
(
0
,
0
,
0
),
)
screen
.
blit
(
ssss
,(
226
,
15
))
screen
.
blit
(
ssss
,(
40
,
0
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
\ No newline at end of file
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