Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_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
c6885375
authored
a year ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
2742489a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
62 deletions
my_game.py
my_game.py
View file @
c6885375
import
pygame
import
pygame
from
pygame
import
locals
import
random
import
random
from
pygame
import
locals
pygame
.
init
()
# 初始化
# 创建一个窗口
class
Block
(
pygame
.
sprite
.
Sprite
):
class
Block
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
super
()
.
__init__
()
super
()
.
__init__
()
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
1000
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
rect
.
y
=
500
-
self
.
rect
.
height
pygame
.
init
()
# 初始化
# 创建一个窗口
class
Plaer
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
):
super
()
.
__init__
()
self
.
image
=
image
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
150
self
.
rect
.
y
=
400
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
# 载入图片
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
bush
=
pygame
.
image
.
load
(
'bush.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'
)]
lu_x
=
0
shan_x
=
0
index
=
0
y
=
400
jumpState
=
"runing"
t
=
30
aa
=
Block
(
bush
,
cacti
,
stone
)
hero1
=
pygame
.
image
.
load
(
'hero1.png'
)
hero2
=
pygame
.
image
.
load
(
'hero2.png'
)
hero3
=
pygame
.
image
.
load
(
'hero3.png'
)
hero4
=
pygame
.
image
.
load
(
'hero4.png'
)
hero5
=
pygame
.
image
.
load
(
'hero5.png'
)
hero_list
=
[
hero1
,
hero2
,
hero3
,
hero4
,
hero5
]
index
=
0
jumpState
=
"running"
y
=
400
#坐标
V
=
30
#速度
obj
=
Block
(
stone
,
cacti
,
bush
)
road_x
=
0
gamestate
=
True
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
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
and
jumpState
==
"running"
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpState
=
"up"
if
jumpState
==
"up"
:
# 起跳状态
if
t
>
0
:
y
-=
t
t
-=
2
else
:
jumpState
=
"down"
if
jumpState
==
"down"
:
# 降落状态
if
t
<=
30
:
y
+=
t
t
+=
2
else
:
jumpState
=
"runing"
t
=
30
# 悟空造型
if
gamestate
==
True
:
wukong
=
hero
[
index
]
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
if
index
>=
5
:
index
=
0
# 将背景图画上去
# 将背景图画上去
shan_x
-=
2
road_x
-=
8
if
shan_x
<-
1000
:
if
road_x
<=-
1000
:
shan_x
=
0
road_x
=
0
screen
.
blit
(
background
,
(
shan_x
,
0
))
# 远处背景
screen
.
blit
(
background
,
(
0
,
0
))
lu_x
-=
8
screen
.
blit
(
road
,
(
road_x
,
500
))
if
lu_x
<-
1000
:
lu_x
=
0
if
obj
.
rect
.
x
<
0
-
obj
.
rect
.
width
:
screen
.
blit
(
road
,
(
lu_x
,
500
))
# 路
obj
=
Block
(
stone
,
cacti
,
bush
)
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
obj
.
rect
.
x
-=
8
screen
.
blit
(
obj
.
image
,
(
obj
.
rect
.
x
,
obj
.
rect
.
y
))
wukong
=
Plaer
(
hero_list
[
index
])
if
aa
.
rect
.
x
<=
0
-
aa
.
rect
.
width
:
# 障碍物消失
if
jumpState
==
"up"
:
# 创建障碍物对象
if
V
>=
0
:
aa
=
Block
(
bush
,
cacti
,
stone
)
y
-=
V
aa
.
rect
.
x
-=
8
wukong
.
rect
.
y
=
y
print
(
wukong
.
rect
.
y
)
V
-=
2
else
:
jumpState
=
"down"
elif
jumpState
==
"down"
:
if
V
<
30
:
y
+=
V
wukong
.
rect
.
y
=
y
V
+=
2
else
:
V
=
30
y
=
400
wukong
.
rect
.
y
=
y
jumpState
=
"running"
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
if
jumpState
==
"running"
:
index
+=
1
if
index
>=
5
:
index
=
0
if
pygame
.
sprite
.
collide_rect
(
wukong
,
obj
):
screen
.
blit
(
aa
.
image
,
(
aa
.
rect
.
x
,
aa
.
rect
.
y
))
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
# 刷新画面
screen
.
blit
(
gameover
,(
400
,
200
))
pygame
.
display
.
update
()
gamestate
=
False
FPS
.
tick
(
60
)
# 刷新画面
\ No newline at end of file
pygame
.
display
.
update
()
FPS
.
tick
(
45
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
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