Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson9_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
e10763ae
authored
Dec 11, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
e886cbe4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
223 additions
and
127 deletions
my_game.py
my_game.py
View file @
e10763ae
import
pygame
import
pygame
from
pygame
import
locals
import
sys
import
random
from
pygame.locals
import
*
from
random
import
*
pygame
.
init
()
# 初始化
class
Ball
(
pygame
.
sprite
.
Sprite
):
#创建类
class
Block
(
pygame
.
sprite
.
Sprite
):
# 障碍物精灵类
def
__init__
(
self
,
grayball_image
,
greenball_image
,
position
,
speed
,
bg_size
,
target
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
pygame
.
sprite
.
Sprite
.
__init__
(
self
)
super
()
.
__init__
()
# 加载障碍物的图片
self
.
grayball_image
=
pygame
.
image
.
load
(
grayball_image
)
.
convert_alpha
()
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
greenball_image
=
pygame
.
image
.
load
(
greenball_image
)
.
convert_alpha
()
# 根据障碍物位图的宽高设置矩形
self
.
rect
=
self
.
grayball_image
.
get_rect
()
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
left
,
self
.
rect
.
top
=
position
# 障碍物绘制坐标
self
.
speed
=
speed
self
.
rect
.
x
=
1000
self
.
target
=
target
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
control
=
False
self
.
score
=
1
self
.
width
,
self
.
height
=
bg_size
[
0
],
bg_size
[
1
]
self
.
radius
=
self
.
rect
.
width
/
2
class
Player
(
pygame
.
sprite
.
Sprite
):
# 悟空
def
__init__
(
self
,
image
):
super
()
.
__init__
()
# 加载悟空精灵图像
def
move
(
self
):
self
.
image
=
image
self
.
rect
=
self
.
rect
.
move
(
self
.
speed
)
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象
#球出边框后从另一边出现
self
.
rect
=
self
.
image
.
get_rect
()
if
self
.
rect
.
right
<
0
:
self
.
rect
.
x
=
150
self
.
rect
.
left
=
self
.
width
self
.
rect
.
y
=
400
elif
self
.
rect
.
left
>
self
.
width
:
# 创建一个窗口
self
.
rect
.
right
=
0
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
elif
self
.
rect
.
bottom
<
0
:
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
self
.
rect
.
bottom
=
self
.
height
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
score_audio
=
pygame
.
mixer
.
Sound
(
'score.wav'
)
basic_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
18
)
index
=
0
y
=
400
jumpState
=
"runing"
t
=
30
road_x
=
0
score
=
0
bg_x
=
0
time
=
0
gamestate
=
True
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
# 悟空造型
elif
self
.
rect
.
top
>
self
.
height
:
wukong
=
Player
(
hero
[
index
])
self
.
rect
.
top
=
0
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
#检测摩擦次数有没有达到球的标准
if
index
>=
5
:
def
check
(
self
,
motion
):
index
=
0
if
self
.
target
<
motion
<
self
.
target
+
5
:
return
True
if
gamestate
==
True
:
else
:
if
jumpState
==
"up"
:
# 起跳状态
return
False
if
t
>
0
:
y
-=
t
class
Glass
(
pygame
.
sprite
.
Sprite
):
#创建类
wukong
.
rect
.
y
=
y
def
__init__
(
self
,
glass_image
,
mouse_image
,
bg_size
):
t
-=
2
pygame
.
sprite
.
Sprite
.
__init__
(
self
)
else
:
jumpState
=
"down"
self
.
glass_image
=
pygame
.
image
.
load
(
glass_image
)
.
convert_alpha
()
if
jumpState
==
"down"
:
# 降落状态
self
.
glass_rect
=
self
.
glass_image
.
get_rect
()
if
t
<=
30
:
self
.
glass_rect
.
left
,
self
.
glass_rect
.
top
=
\
y
+=
t
(
bg_size
[
0
]
-
self
.
glass_rect
.
width
)
//
2
,
\
wukong
.
rect
.
y
=
y
bg_size
[
1
]
-
self
.
glass_rect
.
height
t
+=
2
self
.
mouse_image
=
pygame
.
image
.
load
(
mouse_image
)
.
convert_alpha
()
self
.
mouse_rect
=
self
.
mouse_image
.
get_rect
()
self
.
mouse_rect
.
left
,
self
.
mouse_rect
.
top
=
\
self
.
glass_rect
.
left
,
self
.
glass_rect
.
top
#鼠标箭头隐藏
pygame
.
mouse
.
set_visible
(
False
)
def
main
():
pygame
.
init
()
a
=
0
grayball_image
=
"gray_ball.png"
greenball_image
=
"green_ball.png"
bg_image
=
"background.png"
running
=
True
glass_image
=
"glass.png"
mouse_image
=
"hand.png"
bg_size
=
width
,
height
=
1024
,
681
screen
=
pygame
.
display
.
set_mode
(
bg_size
)
pygame
.
display
.
set_caption
(
"固定小球"
)
background
=
pygame
.
image
.
load
(
bg_image
)
.
convert_alpha
()
balls
=
[]
group
=
pygame
.
sprite
.
Group
()
hole
=
[(
100
,
130
,
180
,
210
),(
210
,
240
,
380
,
400
),
\
(
490
,
515
,
310
,
340
),(
680
,
720
,
180
,
215
),
\
(
890
,
920
,
400
,
435
)]
pygame
.
mixer
.
music
.
load
(
"bg_music.ogg"
)
pygame
.
mixer
.
music
.
play
()
#载入音乐
loser_sound
=
pygame
.
mixer
.
Sound
(
"loser.wav"
)
laugh_sound
=
pygame
.
mixer
.
Sound
(
"laugh.wav"
)
winner_sound
=
pygame
.
mixer
.
Sound
(
"winner.wav"
)
hole_sound
=
pygame
.
mixer
.
Sound
(
"hole.wav"
)
GAMEOVER
=
USEREVENT
#音乐播放完时发送游戏结束的事件
pygame
.
mixer
.
music
.
set_endevent
(
GAMEOVER
)
event1
=
USEREVENT
+
1
pygame
.
time
.
set_timer
(
event1
,
2
*
2000
)
#创建5个球
for
i
in
range
(
5
):
position
=
randint
(
0
,
width
-
100
),
randint
(
0
,
height
-
100
)
speed
=
[
randint
(
-
5
,
5
),
randint
(
-
5
,
5
)]
ball
=
Ball
(
grayball_image
,
greenball_image
,
position
,
speed
,
bg_size
,
5
*
(
i
+
1
))
balls
.
append
(
ball
)
while
pygame
.
sprite
.
spritecollide
(
ball
,
group
,
False
):
#生成时检测
ball
.
rect
.
left
,
ball
.
rect
.
top
=
randint
(
0
,
width
-
100
),
randint
(
0
,
height
-
100
)
balls
.
append
(
ball
)
group
.
add
(
ball
)
glass
=
Glass
(
glass_image
,
mouse_image
,
bg_size
)
motion
=
0
MYTIMER
=
USEREVENT
+
1
pygame
.
time
.
set_timer
(
MYTIMER
,
1000
)
clock
=
pygame
.
time
.
Clock
()
while
running
:
for
event
in
pygame
.
event
.
get
():
#点击差号退出程序
if
event
.
type
==
QUIT
:
exit
()
#游戏结束
elif
event
.
type
==
GAMEOVER
:
loser_sound
.
play
()
pygame
.
time
.
delay
(
2000
)
laugh_sound
.
play
()
running
=
False
elif
event
.
type
==
MYTIMER
:
if
motion
:
for
each
in
group
:
if
each
.
check
(
motion
):
each
.
speed
=
[
0
,
0
]
each
.
control
=
True
motion
=
0
elif
event
.
type
==
MOUSEMOTION
:
motion
+=
1
print
(
motion
)
elif
event
.
type
==
KEYDOWN
:
if
event
.
key
==
K_w
:
for
each
in
group
:
if
each
.
control
:
each
.
speed
[
1
]
-=
1
if
event
.
key
==
K_s
:
for
each
in
group
:
if
each
.
control
:
each
.
speed
[
1
]
+=
1
if
event
.
key
==
K_a
:
for
each
in
group
:
if
each
.
control
:
each
.
speed
[
0
]
-=
1
if
event
.
key
==
K_d
:
for
each
in
group
:
if
each
.
control
:
each
.
speed
[
0
]
+=
1
if
event
.
key
==
K_SPACE
:
#判断小球是否在坑内
for
each
in
group
:
if
each
.
control
:
for
i
in
hole
:
if
i
[
0
]
<=
each
.
rect
.
left
<=
i
[
1
]
and
\
i
[
2
]
<=
each
.
rect
.
top
<=
i
[
3
]:
#播放音效
hole_sound
.
play
()
each
.
speed
=
[
0
,
0
]
#从 group 中移出
group
.
remove
(
each
)
#一坑一个球
hole
.
remove
(
i
)
if
not
hole
:
pygame
.
mixer
.
music
.
stop
()
winner_sound
.
play
()
pygame
.
time
.
delay
(
3000
)
print
(
'游戏成功'
)
laugh_sound
.
play
()
screen
.
blit
(
background
,(
0
,
0
))
#渲染面板
screen
.
blit
(
glass
.
glass_image
,
glass
.
glass_rect
)
#获得鼠标坐标
glass
.
mouse_rect
.
left
,
glass
.
mouse_rect
.
top
=
pygame
.
mouse
.
get_pos
()
if
glass
.
mouse_rect
.
left
<
glass
.
glass_rect
.
left
:
glass
.
mouse_rect
.
left
=
glass
.
glass_rect
.
left
if
glass
.
mouse_rect
.
left
>
glass
.
glass_rect
.
right
:
glass
.
mouse_rect
.
right
=
glass
.
glass_rect
.
right
if
glass
.
mouse_rect
.
top
<
glass
.
glass_rect
.
top
:
glass
.
mouse_rect
.
top
=
glass
.
glass_rect
.
top
if
glass
.
mouse_rect
.
bottom
>
glass
.
glass_rect
.
bottom
:
glass
.
mouse_rect
.
bottom
=
glass
.
glass_rect
.
bottom
#渲染手
screen
.
blit
(
glass
.
mouse_image
,
glass
.
mouse_rect
)
for
each
in
balls
:
each
.
move
()
if
each
.
control
:
screen
.
blit
(
each
.
greenball_image
,
each
.
rect
)
else
:
else
:
jumpState
=
"runing"
screen
.
blit
(
each
.
grayball_image
,
each
.
rect
)
t
=
30
for
each
in
group
:
group
.
remove
(
each
)
if
pygame
.
sprite
.
spritecollide
(
each
,
group
,
False
,
pygame
.
sprite
.
collide_circle
):
each
.
speed
[
0
]
=
-
each
.
speed
[
0
]
each
.
speed
[
1
]
=
-
each
.
speed
[
1
]
if
each
.
control
:
each
.
speed
[
0
]
=
randint
(
-
10
,
10
)
each
.
speed
[
1
]
=
randint
(
-
10
,
10
)
each
.
control
=
False
group
.
add
(
each
)
# 将背景图画上去
pygame
.
display
.
flip
()
bg_x
-=
1
clock
.
tick
(
35
)
if
bg_x
<=-
1000
:
bg_x
=
0
main
()
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远景
\ No newline at end of file
road_x
-=
8
if
road_x
<=-
1000
:
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
# 道路
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
# 悟空
time
+=
1
if
time
>=
60
:
# 创建障碍物精灵
time
=
0
num
=
random
.
randint
(
0
,
50
)
if
num
>
20
:
obstacle
=
Block
(
bush
,
cacti
,
stone
)
block_list
.
add
(
obstacle
)
for
sprite
in
block_list
:
# 遍历、展示障碍物精灵
sprite
.
rect
.
x
-=
8
screen
.
blit
(
sprite
.
image
,
(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
sprite
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
# 精灵碰撞检测
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
# 游戏结束
screen
.
blit
(
gameover
,
(
400
,
200
))
gamestate
=
False
else
:
if
sprite
.
rect
.
x
+
sprite
.
rect
.
width
<
wukong
.
rect
.
x
:
score
=
score
+
sprite
.
score
sprite
.
score
=
0
scoreSurf
=
basic_font
.
render
(
"分数:"
+
str
(
score
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
scoreSurf
,(
880
,
20
))
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ 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