Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson4_diy3
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
b1f75d1c
authored
Jan 29, 2024
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
c7b1ad07
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
54 deletions
my_music.py
my_music.py
View file @
b1f75d1c
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
pygame
.
init
()
# 初始化
# 初始化pygame,为使用硬件做准备
# 创建窗口
pygame
.
init
()
screen
=
pygame
.
display
.
set_mode
((
640
,
480
))
# 载入图片、资源
# 创建一个窗口
bg_img
=
pygame
.
image
.
load
(
'background.png'
)
# 背景图
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
play_img
=
pygame
.
image
.
load
(
'play.png'
)
# 播放按钮
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
stop_img
=
pygame
.
image
.
load
(
'stop.png'
)
# 暂停按钮
last_img
=
pygame
.
image
.
load
(
'last.png'
)
# 上一曲按钮
# 背景
next_img
=
pygame
.
image
.
load
(
'next.png'
)
# 下一曲按钮
background
=
pygame
.
image
.
load
(
'bg.png'
)
logo_img
=
pygame
.
image
.
load
(
'logo.png'
)
# 中间logo
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
pygame
.
mixer
.
music
.
load
(
'歌曲4.ogg'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
# 食物 苹果
body
=
pygame
.
image
.
load
(
'body.png'
)
# 身体
while
True
:
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
for
event
in
pygame
.
event
.
get
():
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
if
event
.
type
==
locals
.
QUIT
:
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
exit
()
volume
=
0.2
x
,
y
=
240
,
120
pygame
.
mixer
.
music
.
set_volume
(
volume
)
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
apple_x
=
360
# 苹果横坐标
start
=
stop_img
apple_y
=
300
# 苹果纵坐标
click
=
0
setheading
=
"right"
snake_head
=
right
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_UP
:
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
"left"
:
volume
+=
0.1
setheading
=
'right'
if
volume
<
0
or
volume
==
0
:
snake_head
=
right
volume
=
0
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
"right"
:
pygame
.
mixer
.
music
.
set_volume
(
volume
)
setheading
=
'left'
if
event
.
key
==
locals
.
K_DOWN
:
snake_head
=
left
volume
-=
0.1
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
"down"
:
if
volume
<
0
<
or
volume
==
0
:
setheading
=
'up'
volume
=
0
snake_head
=
up
pygame
.
mixer
.
music
.
set_volume
(
volume
)
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
"up"
:
if
event
.
type
==
locals
.
MOUSEBUTTONDOWN
:
setheading
=
'down'
click
+=
1
snake_head
=
down
if
click
%
2
==
0
:
pygame
.
mixer
.
music
.
unpause
()
# 设置贪吃蛇的头部坐标
start
=
stop_img
if
setheading
==
"right"
:
else
:
x
+=
30
pygame
.
mixer
.
music
.
pause
()
elif
setheading
==
"left"
:
start
=
play_img
x
-=
30
elif
setheading
==
"up"
:
if
volume
<=
0
:
y
-=
30
volume
=
0
else
:
if
pygame
.
mixer
.
music
.
get_busy
()
==
False
:
y
+=
30
pygame
.
mixer
.
music
.
play
()
position
.
append
((
x
,
y
))
if
event
.
button
==
1
:
# 贪吃蛇吃到了苹果
x
,
y
=
event
.
pos
if
x
==
apple_x
and
y
==
apple_y
:
if
x
>
270
and
x
<
370
and
y
>
350
and
y
<
450
:
apple_x
=
random
.
randint
(
0
,
660
)
# 绘制画面
apple_y
=
random
.
randint
(
0
,
480
)
screen
.
blit
(
bg_img
,
(
0
,
0
))
# 填充背景
screen
.
blit
(
start
,
(
270
,
330
))
# 暂停按钮
position
.
pop
(
0
)
screen
.
blit
(
logo_img
,
(
170
,
60
))
# 中间logo图
# 将背景图画上去
screen
.
blit
(
last_img
,
(
120
,
350
))
# 上一曲
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
next_img
,
(
420
,
350
))
# 下一曲
# 将贪吃蛇的头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
\ 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