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
fda3cd94
authored
Dec 11, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
2ffb9ec5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
130 additions
and
6 deletions
my_music.py
my_music.py
View file @
fda3cd94
import
pygame
from
pygame
import
locals
import
os
#导入操作系统接口模块
pygame
.
init
()
# 初始化
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
# 载入图片、资源
bg_img
=
pygame
.
image
.
load
(
'background.png'
)
# 背景图
play_img
=
pygame
.
image
.
load
(
'play.png'
)
# 播放按钮
...
...
@@ -11,16 +13,138 @@ stop_img = pygame.image.load('stop.png') # 暂停按钮
last_img
=
pygame
.
image
.
load
(
'last.png'
)
# 上一曲按钮
next_img
=
pygame
.
image
.
load
(
'next.png'
)
# 下一曲按钮
logo_img
=
pygame
.
image
.
load
(
'logo.png'
)
# 中间logo
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
#上传字体对象
music_zong
=
[]
#将所有歌曲放入列表中
#设置音乐播放音量变量
num
=
0
#设置鼠标点击次数
click
=
0
play_button
=
stop_img
#设置播放音乐的索引
music_index
=
-
1
#创建变量,保存本地文件的路径
path
=
'C:
\\
Users
\\
Lenovo
\\
Desktop
\\
music'
music_bendi
=
os
.
listdir
(
path
)
#导入本地文件
#创建图片旋转角度变量
angel
=
0
angel_0
=
0
#判断是否是音频文件
for
i
in
music_bendi
:
if
i
[
-
4
:]
==
'.ogg'
or
i
[
-
4
:]
==
'.wav'
:
#只筛选出这两种格式的音乐放入musicz_zong列表中
music_zong
.
append
(
i
)
while
True
:
for
event
in
pygame
.
event
.
get
():
????
for
event
in
pygame
.
event
.
get
():
#pygame.event.get(),获取pygame中发生的事件
#点击关闭按钮,程序结束
if
event
.
type
==
locals
.
QUIT
:
#接收到退出事件后即点击窗口关闭,退出程序
exit
()
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
#当音乐停止时,再次播放,实现循环播放
music_index
+=
1
if
music_index
>
len
(
music_zong
)
-
1
:
#如果播放完最后一首音乐
music_index
=
0
#从头播放
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_zong
[
music_index
])
#按列表索引播放歌曲 path为目录,
#播放音乐
pygame
.
mixer
.
music
.
play
()
music_1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_zong
[
music_index
])
#用mixer方法载入音乐,创建声音对象
music_len
=
music_1
.
get_length
()
#获取音乐长度
music_len
=
int
(
music_len
)
#取整
if
event
.
type
==
locals
.
KEYDOWN
:
#获取键键盘点击事件
if
event
.
key
==
locals
.
K_UP
:
#设置音量+
num
=
num
+
0.1
#每次按上键,音量只增加0.1
if
num
>
1
:
#设置最大音量为1
num
=
1
pygame
.
mixer
.
music
.
set_volume
(
num
)
if
event
.
key
==
locals
.
K_DOWN
:
#设置音量-
num
=
num
-
0.1
#每次按下键,音量只减少0.1
if
num
<
0
:
#设置最小音量为0
num
=
0
pygame
.
mixer
.
music
.
set_volume
(
num
)
#获取鼠标点击事件
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
if
event
.
button
==
1
:
#button == 1表示鼠标左键点击的状态
x
,
y
=
event
.
pos
#获取鼠标的坐标
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
#判断鼠标左键点击范围是否在播放按钮图片范围内。
click
+=
1
#鼠标点击次数+1
if
click
%
2
==
0
:
pygame
.
mixer
.
music
.
unpause
()
#继续播放播放音乐
play_button
=
stop_img
#当音乐播放时,换为暂停图像
else
:
pygame
.
mixer
.
music
.
pause
()
#暂停播放音乐
play_button
=
play_img
#当音乐暂停时,换为播放图像
if
x
>
120
and
x
<
220
and
y
>
350
and
y
<
400
:
#当鼠标点击到上一曲图标范围内
music_last
=
music_index
-
1
#切换到列表歌曲中上一个歌曲的索引
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_zong
[
music_last
])
#用music方法载入音乐
print
(
music_last
)
click
=
0
#点击上一曲,将点击值换为0
play_button
=
stop_img
#播放按钮换为暂停图片
music_1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_zong
[
music_index
])
#用mixer方法载入音乐,创建声音对象
music_len
=
music_1
.
get_length
()
#获取音乐长度
music_len
=
int
(
music_len
)
#取整
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
400
:
#当鼠标点击到下一曲图标范围内
music_next
=
music_index
#切换到列表歌曲中下一个歌曲的索引
print
(
music_next
)
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_zong
[
music_next
])
#
click
=
0
#点击上一曲,将点击值换为0
play_button
=
stop_img
#播放按钮换为暂停图片
music_1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
music_zong
[
music_index
])
#用mixer方法载入音乐,创建声音对象
music_len
=
music_1
.
get_length
()
#获取音乐长度
music_len
=
int
(
music_len
)
#取整
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
stop_img
,
(
270
,
330
))
# 暂停按钮
screen
.
blit
(
logo_img
,
(
170
,
60
))
# 中间logo图
screen
.
blit
(
play_button
,
(
270
,
330
))
# 播放或暂停按钮
#获取歌曲播放时间
play_time
=
pygame
.
mixer
.
music
.
get_pos
()
#单位毫秒
play_time
=
int
(
play_time
/
1000
)
#将事件取整
play_min
=
play_time
//
60
play_s
=
play_time
%
60
if
play_s
<
10
:
#当秒数少于10
play_s
=
'0'
+
str
(
play_s
)
#让其显示 ‘0x’秒
info
=
str
(
play_min
)
+
':'
+
str
(
play_s
)
#分秒显示结果
#绘制播放进度
text1
=
my_font
.
render
(
info
,
True
,(
255
,
255
,
255
))
#字体设置
screen
.
blit
(
text1
,(
120
,
440
))
#将文本信息渲染到窗口
#获取歌曲总时长
music_m
=
music_len
//
60
music_s
=
music_len
%
60
if
music_s
<
10
:
#当秒数少于10
music_s
=
'0'
+
str
(
music_s
)
#让其显示 ‘0x’秒
info2
=
'/'
+
str
(
music_m
)
+
':'
+
str
(
music_s
)
#分秒显示结果
#绘制总时长
text2
=
my_font
.
render
(
info2
,
True
,(
0
,
0
,
0
))
#字体设置
screen
.
blit
(
text2
,(
180
,
440
))
#将文本信息渲染到窗口
#设置旋转图标
new_log
=
pygame
.
transform
.
rotate
(
logo_img
,
angel
)
#数字为负数,顺时针转动;为正数,逆时针转动
new_rect
=
new_log
.
get_rect
(
center
=
(
320
,
200
))
#设置图片旋转中心点坐标,同时返回一个元组(左上角坐标,图片大小)
pos
=
(
new_rect
[
0
],
new_rect
[
1
])
#取出元组中的坐标
if
play_button
==
stop_img
:
angel
+=
1
screen
.
blit
(
new_log
,
pos
)
# 中间logo图,将元组坐标设置
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
# 刷新画面
???
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