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
25c702a5
authored
Mar 19, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
2ffb9ec5
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
171 additions
and
5 deletions
1.py
my_music.py
test.wav
1.py
0 → 100644
View file @
25c702a5
dict
=
{
1
:
'a'
,
2
:
'b'
,
3
:
'c'
}
#字典名.pop(键)功能:删除指定的键值对
#如果将字典名.pop(键)赋值给一个变量,变量里边存放的是键对应的值
d
=
dict
.
pop
(
1
)
print
(
dict
)
print
(
d
)
#字典名.clear() 功能:清空字典中的数据,字典就变成了一个空字典
dict
.
clear
()
print
(
dict
)
#del 字典名 功能:将整个字典删除,如果调用会报错,显示字典名未定义
dict1
=
{
1
:
'a'
,
2
:
'b'
,
3
:
'c'
}
del
dict1
print
(
dict1
)
name
=
input
(
"名字:"
)
if
name
in
score
:
for
k
,
v
in
score
[
name
]:
print
(
k
,
v
)
else
:
print
(
"not found"
)
my_music.py
View file @
25c702a5
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
os
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建窗口
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
...
@@ -11,16 +11,155 @@ stop_img = pygame.image.load('stop.png') # 暂停按钮
...
@@ -11,16 +11,155 @@ 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
'''
图片导入:pygame.image.load("文件名.后缀名")
字体导入:pygame.font.Font("文件名.后缀名",字体大小)
歌曲导入:pygame.mixer.music.load("文件名.后缀名")
'''
#设置一个变量表示音量 volume的取值范围是0 --- 1
volume
=
0.2
#pygame.mixer.music.set_volume(0到1之间的数字,可以包含0和1) 设置音量大小
#如果是0就代表静音了,如果是1就代表最大音量
pygame
.
mixer
.
music
.
set_volume
(
volume
)
#click 代表点击次数 click为奇数时为暂停 偶数代表播放
click
=
0
#创建play_button来存放当前的按钮
#播放状态下使用,暂停按钮 暂停状态下使用,就是播放按钮
play_button
=
stop_img
#创建列表musci_list 来存放所有的歌曲
music_list
=
[]
# 创建一个num表示列表music_list的索引
num
=
0
#创建一个filelist来存放来存放当前文件夹的所有内容
filelist
=
os
.
listdir
(
'./'
)
#遍历filelist,来 查看列表中是否有后缀名为.wav或者是.ogg的文件,如果有则添加到列表music_list里
for
i
in
filelist
:
if
i
[
-
4
:]
==
'.wav'
or
i
[
-
4
:]
==
'.ogg'
:
music_list
.
append
(
i
)
print
(
music_list
)
#设置angle为旋转角度,初始化为0
angle
=
0
#字体载入
my_font
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
12
)
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
:
volume
=
1
if
event
.
key
==
locals
.
K_DOWN
:
volume
-=
0.1
if
volume
<
0
:
volume
=
0
pygame
.
mixer
.
music
.
set_volume
(
volume
)
#判断是否有鼠标按钮被按下
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
#要保证按下的一定是鼠标左键
if
event
.
button
==
1
:
x
,
y
=
event
.
pos
#暂停和播放
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
#让点击次数click增加1
click
+=
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
:
num
-=
1
if
num
<
0
:
num
=
len
(
music_list
)
-
1
pygame
.
mixer
.
music
.
load
(
'./'
+
music_list
[
num
%
len
(
music_list
)])
pygame
.
mixer
.
music
.
play
()
#音乐播放
click
=
0
play_button
=
stop_img
sound
=
pygame
.
mixer
.
Sound
(
'./'
+
music_list
[
num
%
len
(
music_list
)])
sound_len
=
int
(
sound
.
get_length
())
#下一曲
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
400
:
sound
=
pygame
.
mixer
.
Sound
(
'./'
+
music_list
[
num
%
len
(
music_list
)])
sound_len
=
int
(
sound
.
get_length
())
num
+=
1
if
num
>
len
(
music_list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
'./'
+
music_list
[
num
%
len
(
music_list
)])
pygame
.
mixer
.
music
.
play
()
#音乐播放
click
=
0
play_button
=
stop_img
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
#判断音乐是否在播放,如果是False表示当前无音乐播放
#如果是True表示现在正在播放
#如果num是-1,就要在判断音乐是否播放的条件语句中的第一行来进行改变num的值
sound
=
pygame
.
mixer
.
Sound
(
'./'
+
music_list
[
num
%
len
(
music_list
)])
sound_len
=
int
(
sound
.
get_length
())
# if num > len(music_list) - 1:
# num = 0
pygame
.
mixer
.
music
.
load
(
'./'
+
music_list
[
num
%
len
(
music_list
)])
pygame
.
mixer
.
music
.
play
()
#音乐播放
num
+=
1
#唱盘旋转
#pygame.transform.rotate(要旋转的图片,旋转角度) 让指定的图片朝着指定的方向来进行旋转
new_logo
=
pygame
.
transform
.
rotate
(
logo_img
,
angle
)
#new_logo.get_rect()的返回值有4个,前两个分别为左上角的x坐标和y坐标,后两个图片的宽和高
newRect
=
new_logo
.
get_rect
(
center
=
(
350
,
200
))
pos
=
(
newRect
[
0
],
newRect
[
1
])
#左上角的x坐标和y坐标 pos通过赋值变成了一个元组
if
play_button
==
stop_img
:
angle
+=
-
1
#播放时长
# pygame.mixer.music.get_pos()获取音乐播放进度
play_time
=
pygame
.
mixer
.
music
.
get_pos
()
//
1000
#播放的分钟数
play_m
=
play_time
//
60
#播放的秒数
play_s
=
play_time
%
60
if
play_s
<
10
:
play_s
=
"0"
+
str
(
play_s
)
#连接成一个字符串,方便生成可渲染的文字对象
info
=
str
(
play_m
)
+
' : '
+
str
(
play_s
)
#生成可渲染的文字对象
text
=
my_font
.
render
(
info
,
True
,(
255
,
255
,
255
))
#音乐总时长
sound_m
=
sound_len
//
60
#分
sound_s
=
sound_len
%
60
#秒
if
sound_s
<
10
:
sound_s
=
"0"
+
str
(
sound_s
)
info1
=
' / '
+
str
(
sound_m
)
+
' : '
+
str
(
sound_s
)
#生成可渲染的文字对象
text1
=
my_font
.
render
(
info1
,
True
,(
255
,
255
,
255
))
# 绘制画面
# 绘制画面
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
stop_img
,
(
270
,
330
))
# 暂停按钮
screen
.
blit
(
text
,(
120
,
450
))
screen
.
blit
(
logo_img
,
(
170
,
60
))
# 中间logo图
screen
.
blit
(
text1
,(
170
,
450
))
screen
.
blit
(
play_button
,
(
270
,
330
))
# 暂停按钮
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
))
# 下一曲
# 刷新画面
# 刷新画面
???
pygame
.
display
.
update
()
test.wav
deleted
100644 → 0
View file @
2ffb9ec5
File deleted
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