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
f52d26b9
authored
May 07, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
fc662e6e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
77 deletions
my_music.py
my_music.py
View file @
f52d26b9
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
os
import
random
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
# 载入图片、资源
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
bg_img
=
pygame
.
image
.
load
(
'background.png'
)
# 背景图
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
play_img
=
pygame
.
image
.
load
(
'play.png'
)
# 播放按钮
# 载入图片
stop_img
=
pygame
.
image
.
load
(
'stop.png'
)
# 暂停按钮
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
last_img
=
pygame
.
image
.
load
(
'last.png'
)
# 上一曲按钮
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
next_img
=
pygame
.
image
.
load
(
'next.png'
)
# 下一曲按钮
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
logo_img
=
pygame
.
image
.
load
(
'logo.png'
)
# 下一曲按钮
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
music_list
=
[]
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
path
=
"C:
\\
Users
\\
bellcode
\\
Desktop
\\
test"
pygame
.
image
.
load
(
'hero2.png'
),
filelist
=
os
.
listdir
(
path
)
pygame
.
image
.
load
(
'hero3.png'
),
num
=
-
1
pygame
.
image
.
load
(
'hero4.png'
),
for
i
in
filelist
:
pygame
.
image
.
load
(
'hero5.png'
)]
if
i
[
-
4
:]
==
".wav"
or
i
[
-
4
:]
==
".ogg"
:
index
=
0
music_list
.
append
(
i
)
y
=
400
volume
=
0.2
jumpState
=
"runing"
pygame
.
mixer
.
music
.
set_volume
(
volume
)
# 初始播放音量
t
=
30
click
=
0
# +++++++++++++++++++++++
play_button
=
stop_img
bg_x
=
0
#背景
road_x
=
0
#道路
# +++++++++++++++++++++++
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
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
:
# 接收到退出事件后退出程序
exit
()
exit
()
# 按键,控制声音大小
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_w
:
if
jumpState
==
"runing"
:
volume
+=
0.1
if
event
.
key
==
locals
.
K_SPACE
:
if
volume
>
1
:
jumpState
=
"up"
volume
=
1
pygame
.
mixer
.
music
.
set_volume
(
volume
)
if
event
.
key
==
locals
.
K_s
:
volume
-=
0.1
if
volume
<
0
:
volume
=
0
pygame
.
mixer
.
music
.
set_volume
(
volume
)
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
if
jumpState
==
"up"
:
# 起跳状态
if
event
.
button
==
1
:
# 左击
if
t
>
0
:
x
,
y
=
event
.
pos
y
-=
t
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
# 播放按钮
t
-=
2
click
+=
1
if
click
%
2
==
0
:
play_button
=
stop_img
pygame
.
mixer
.
music
.
unpause
()
else
:
play_button
=
play_img
pygame
.
mixer
.
music
.
pause
()
if
x
>
420
and
x
<
520
and
y
>
350
and
y
<
400
:
# 下一曲
num
+=
1
if
num
>
len
(
music_list
)
-
1
:
num
=
0
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
pygame
.
mixer
.
music
.
play
()
play_button
=
stop_img
if
click
%
2
==
0
:
click
+=
2
else
:
else
:
click
+=
1
jumpState
=
"down"
if
jumpState
==
"down"
:
# 降落状态
if
x
>
120
and
x
<
220
and
y
>
350
and
y
<
400
:
# 上一曲
if
t
<=
30
:
num
-=
1
y
+=
t
if
num
<
0
:
t
+=
2
num
=
len
(
music_list
)
-
1
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
pygame
.
mixer
.
music
.
play
()
play_button
=
stop_img
if
click
%
2
==
0
:
click
+=
2
else
:
else
:
click
+=
1
jumpState
=
"runing"
t
=
30
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
# 悟空造型
num
+=
1
wukong
=
hero
[
index
]
if
num
>
len
(
music_list
)
-
1
:
if
jumpState
==
"runing"
:
# 跑步状态下
num
=
0
index
+=
1
pygame
.
mixer
.
music
.
load
(
path
+
"
\\
"
+
music_list
[
num
])
if
index
>=
5
:
pygame
.
mixer
.
music
.
play
()
index
=
0
# 将背景图画上去
# +++++++++++++++++++++++
bg_x
-=
1
if
bg_x
<=-
1000
:
bg_x
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
road_x
-=
8
if
road_x
<=-
1000
:
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
# +++++++++++++++++++++++
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
# 绘制画面
if
rect
.
x
<=
0
-
rect
.
width
:
# 障碍物消失
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
# 创建障碍物对象
screen
.
blit
(
play_button
,
(
270
,
330
))
# 暂停按钮
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
screen
.
blit
(
logo_img
,
(
170
,
60
))
# 中间logo图
rect
=
obstacle
.
get_rect
()
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
rect
.
x
=
1000
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
rect
.
y
=
500
-
rect
.
height
rect
.
x
-=
8
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
30
)
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