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
5eb3b139
authored
Jan 18, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
c7fbcf48
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
449 additions
and
7 deletions
1.py
3
my_music.py
周一17.py
周二19.py
1.py
0 → 100644
View file @
5eb3b139
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'
)
# 播放按钮
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按钮
#导入歌曲
v
=
0.5
#创建一个列表来存放所有的歌曲
list
=
[]
#初始化变量angle
angle
=
0
path
=
r'C:\Users\左闭右开\Desktop\test\\'
filelist
=
os
.
listdir
(
path
)
#遍历filelist
for
i
in
filelist
:
#判断切片是否等于.wav和.ogg
if
i
[
-
4
:]
==
'.wav'
or
i
[
-
4
:]
==
'.ogg'
:
list
.
append
(
i
)
#创建一个变量来存储当前的索引
num
=
-
1
pygame
.
mixer
.
music
.
set_volume
(
v
)
c
=
0
play_button
=
stop_img
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_UP
:
v
+=
0.1
if
v
>
1
:
v
=
1
pygame
.
mixer
.
music
.
set_volume
(
v
)
if
event
.
key
==
locals
.
K_DOWN
:
v
-=
0.1
if
v
<
0
:
v
=
0
pygame
.
mixer
.
music
.
set_volume
(
v
)
#鼠标点击相关
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
#先判断事件按键是否等于1(1左键,2,中间 3.右键)
if
event
.
button
==
1
:
#1.2获取到坐标位置
x
,
y
=
event
.
pos
#判断坐标有没有在范围内,(270,350) 范围:100
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
c
+=
1
if
c
%
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
<
450
:
num
-=
1
if
num
<
0
:
num
=
len
(
list
)
-
1
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
c
=
0
play_button
=
stop_img
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
450
:
num
+=
1
if
num
>
len
(
list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
c
=
0
play_button
=
stop_img
#判断音乐是否在播放,如果没有播放就播放
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
#改变num值 +1
num
+=
1
#判断num是否大于列表长度-1
if
num
>
len
(
list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
#旋转
new_logos
=
pygame
.
transform
.
rotate
(
logo_img
,
angle
)
new_rect
=
new_logos
.
get_rect
(
center
=
(
320
,
180
))
pos
=
(
new_rect
[
0
],
new_rect
[
1
])
if
play_button
==
stop_img
:
angle
+=
1
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
play_button
,
(
270
,
330
))
# 暂停按钮
screen
.
blit
(
new_logos
,
pos
)
# 中间logo图
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
# 刷新画面
pygame
.
display
.
update
()
\ No newline at end of file
3
0 → 100644
View file @
5eb3b139
1.载入歌曲
pygame.mixer.music.load(歌曲名)
2. 播放歌曲
pygame.mixer.music.play()
3. 设置音量
pygame.mixer.music.set_volume()
4.判断歌曲是否播放中
pygame.mixer.music.get_busy()
5. 鼠标按键被按下事件类型
MOUSEBUTTON
\ No newline at end of file
my_music.py
View file @
5eb3b139
import
pygame
from
pygame
import
locals
import
os
pygame
.
init
()
# 初始化
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
...
...
@@ -11,17 +11,121 @@ 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'
)
# 下一曲按钮
#导入歌曲
v
=
0.5
#2.1 创建一个列表来存放所有的歌曲
list
=
[]
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
12
)
path
=
r'C:\Users\左闭右开\Desktop\test\\'
filelist
=
os
.
listdir
(
path
)
#遍历filelist
for
i
in
filelist
:
#判断切片是否等于.wav和.ogg
if
i
[
-
4
:]
==
'.wav'
or
i
[
-
4
:]
==
'.ogg'
:
list
.
append
(
i
)
#2.2 创建一个变量来存储当前的索引
num
=
-
1
pygame
.
mixer
.
music
.
set_volume
(
v
)
c
=
0
play_button
=
stop_img
angle
=
0
while
True
:
for
event
in
pygame
.
event
.
get
():
??
??
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_UP
:
v
+=
0.1
if
v
>
1
:
v
=
1
pygame
.
mixer
.
music
.
set_volume
(
v
)
if
event
.
key
==
locals
.
K_DOWN
:
v
-=
0.1
if
v
<
0
:
v
=
0
pygame
.
mixer
.
music
.
set_volume
(
v
)
#鼠标点击相关
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
#1.1先判断事件按键是否等于1(1左键,2,中间 3.右键)
if
event
.
button
==
1
:
#1.2获取到坐标位置
x
,
y
=
event
.
pos
#判断坐标有没有在范围内,(270,350) 范围:100
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
c
+=
1
if
c
%
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
<
450
:
num
-=
1
if
num
<
0
:
num
=
len
(
list
)
-
1
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
c
=
0
play_button
=
stop_img
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
list
[
num
])
music_length
=
music1
.
get_length
()
music_length
=
int
(
music_length
)
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
450
:
num
+=
1
if
num
>
len
(
list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
c
=
0
play_button
=
stop_img
#载入歌曲
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
list
[
num
])
music_length
=
int
(
music1
.
get_length
())
#判断音乐是否在播放,如果没有播放就播放
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
#2.3改变num值 +1
num
+=
1
#2.4判断num是否大于列表长度-1
if
num
>
len
(
list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
music1
=
pygame
.
mixer
.
Sound
(
path
+
"
\\
"
+
list
[
num
])
music_length
=
music1
.
get_length
()
music_length
=
int
(
music_length
)
new_logo
=
pygame
.
transform
.
rotate
(
logo_img
,
angle
)
newRect
=
new_logo
.
get_rect
(
center
=
(
350
,
200
))
pos
=
(
newRect
[
0
],
newRect
[
1
])
if
play_button
==
stop_img
:
angle
+=
0.1
#播放时长
play_time
=
int
(
pygame
.
mixer
.
music
.
get_pos
()
/
1000
)
m
=
play_time
//
60
s
=
play_time
%
60
info
=
str
(
m
)
+
':'
+
str
(
s
)
if
s
<
10
:
s
=
"0"
+
str
(
s
)
info
=
str
(
m
)
+
":"
+
str
(
s
)
text
=
my_font
.
render
(
info
,
True
,(
255
,
255
,
255
))
#歌曲时长
music_m
=
music_length
//
60
music_s
=
music_length
%
60
if
music_s
<
10
:
music_s
=
"0"
+
str
(
music_s
)
info2
=
' / '
+
str
(
music_m
)
+
":"
+
str
(
music_s
)
text2
=
my_font
.
render
(
info2
,
True
,(
255
,
255
,
255
))
# 绘制画面
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
))
# 暂停按钮
screen
.
blit
(
new_logo
,
pos
)
# 中间logo图
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
screen
.
blit
(
text
,(
120
,
450
))
screen
.
blit
(
text2
,(
160
,
450
))
# 刷新画面
??
pygame
.
display
.
update
()
\ No newline at end of file
周一17.py
0 → 100644
View file @
5eb3b139
import
pygame
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'
)
# 播放按钮
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'
)
# 唱盘
#导入歌曲
v
=
0.5
#2.1 创建一个列表来存放所有的歌曲
list
=
[]
#3.1 设置一个变量用来存放旋转的角度
angle
=
0
path
=
r'C:\Users\左闭右开\Desktop\test\\'
filelist
=
os
.
listdir
(
path
)
#遍历filelist
for
i
in
filelist
:
#判断切片是否等于.wav和.ogg
if
i
[
-
4
:]
==
'.wav'
or
i
[
-
4
:]
==
'.ogg'
:
list
.
append
(
i
)
#2.2 创建一个变量来存储当前的索引
num
=
-
1
pygame
.
mixer
.
music
.
set_volume
(
v
)
c
=
0
play_button
=
stop_img
#载入字体
myFont
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
12
)
#第一个是字体名称,第二个是字体大小
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_UP
:
v
+=
0.1
if
v
>
1
:
v
=
1
pygame
.
mixer
.
music
.
set_volume
(
v
)
if
event
.
key
==
locals
.
K_DOWN
:
v
-=
0.1
if
v
<
0
:
v
=
0
pygame
.
mixer
.
music
.
set_volume
(
v
)
#鼠标点击相关
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
#1.1先判断事件按键是否等于1(1左键,2,中间 3.右键)
if
event
.
button
==
1
:
#1.2获取到坐标位置
x
,
y
=
event
.
pos
#判断坐标有没有在范围内,(270,350) 范围:100
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
c
+=
1
if
c
%
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
<
450
:
num
-=
1
if
num
<
0
:
num
=
len
(
list
)
-
1
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
c
=
0
play_button
=
stop_img
music1
=
pygame
.
mixer
.
Sound
(
path
+
list
[
num
])
m_t
=
int
(
music1
.
get_length
())
#以秒为单位
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
450
:
num
+=
1
if
num
>
len
(
list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
c
=
0
play_button
=
stop_img
music1
=
pygame
.
mixer
.
Sound
(
path
+
list
[
num
])
m_t
=
int
(
music1
.
get_length
())
#以秒为单位
#判断音乐是否在播放,如果没有播放就播放
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
#2.3改变num值 +1
num
+=
1
#2.4判断num是否大于列表长度-1
if
num
>
len
(
list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
pygame
.
mixer
.
music
.
play
()
music1
=
pygame
.
mixer
.
Sound
(
path
+
list
[
num
])
m_t
=
int
(
music1
.
get_length
())
#以秒为单位
#获取播放的时长
p_time
=
pygame
.
mixer
.
music
.
get_pos
()
//
1000
#获取到的是毫秒转换成秒除以1000
#将p_time拆分成分和秒
p_m
=
p_time
//
60
#分是整除60
p_s
=
p_time
%
60
if
p_s
<
10
:
p_s
=
'0'
+
str
(
p_s
)
info
=
str
(
p_m
)
+
':'
+
str
(
p_s
)
text
=
myFont
.
render
(
info
,
True
,(
255
,
255
,
255
))
#1.要加载的文字内容,2.是否抗锯齿 3.三原色
#截取总时长的分秒
m_m
=
m_t
//
60
m_s
=
m_t
%
60
if
m_s
<
10
:
m_s
=
'0'
+
str
(
m_s
)
info1
=
'/'
+
str
(
m_m
)
+
':'
+
str
(
m_s
)
text2
=
myFont
.
render
(
info1
,
True
,(
255
,
255
,
255
))
#图标旋转
newlogo
=
pygame
.
transform
.
rotate
(
logo_img
,
angle
)
#让一个对象旋转两个参数,第一个
newrect
=
newlogo
.
get_rect
(
center
=
(
320
,
180
))
#设置中心点坐标
#rect参数会返回4个参数,前两个是左上角的位置坐标,后两个是图片的大小
pos
=
(
newrect
[
0
],
newrect
[
1
])
if
play_button
==
stop_img
:
angle
+=
1
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
play_button
,
(
270
,
330
))
# 暂停按钮
screen
.
blit
(
newlogo
,
pos
)
# 中间logo图
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
#渲染
screen
.
blit
(
text
,(
120
,
450
))
screen
.
blit
(
text2
,(
160
,
450
))
# 刷新画面
pygame
.
display
.
update
()
\ No newline at end of file
周二19.py
0 → 100644
View file @
5eb3b139
import
pygame
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'
)
# 播放按钮
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'
)
# 下一曲按钮
#1.1创建列表存储所有的歌曲
list
=
[]
#获取列表的数据 列表名[索引]
num
=
0
#先创建一个volume变量 0 -- 1
volume
=
0.5
pygame
.
mixer
.
music
.
set_volume
(
volume
)
#创建一个变量click 记录的是鼠标点击次数
click
=
0
#创建一个变量play_button 记录播放和暂停的图片素材
play_button
=
stop_img
path
=
r'C:\Users\左闭右开\Desktop\test\\'
#使用filelist来存放os打开的文件夹
filelist
=
os
.
listdir
(
path
)
#遍历文件夹,然后判断当前的文件后缀是否是.wav 和 .ogg
for
i
in
filelist
:
if
i
[
-
4
:]
==
'.ogg'
or
i
[
-
4
:]
==
'.wav'
:
list
.
append
(
i
)
while
True
:
for
event
in
pygame
.
event
.
get
():
#如果时间类型是本地的QUIT
if
event
.
type
==
locals
.
QUIT
:
exit
()
#判断事件类型是否是键盘被按下
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_UP
:
volume
+=
0.1
#判断音量是否超过了最大值1,超过就赋值为1
if
volume
>
1
:
volume
=
1
pygame
.
mixer
.
music
.
set_volume
(
volume
)
#判断事件的键是否是down
if
event
.
key
==
locals
.
K_DOWN
:
volume
-=
0.1
#判断音量是否超过了最小值0,超过就赋值为0
if
volume
<
0
:
volume
=
0
pygame
.
mixer
.
music
.
set_volume
(
volume
)
#判断事件类型是否是鼠标按键被按下
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
#判断事件的button值是否等于1
if
event
.
button
==
1
:
#将鼠标的坐标分别赋值给x和y
x
,
y
=
event
.
pos
#判断x是否在270到370之间且y在350到450之间
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
click
+=
1
if
click
%
2
==
0
:
pygame
.
mixer
.
music
.
unpause
()
play_button
=
stop_img
else
:
pygame
.
mixer
.
music
.
pause
()
play_button
=
play_img
#exit()
#判断音乐的播放状态
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
#载入歌曲,通过列表名[索引]的方式去载入
pygame
.
mixer
.
music
.
load
(
path
+
list
[
num
])
#播放音乐
pygame
.
mixer
.
music
.
play
()
num
+=
1
#列表索引的范围是列表长度-1
#判断索引是否大于列表长度-1,大于就将列表索引设为0
if
num
>
len
(
list
)
-
1
:
num
=
0
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
play_button
,
(
270
,
330
))
# 暂停按钮
screen
.
blit
(
logo_img
,
(
170
,
60
))
# 中间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