Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_diy4
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
63dd28f9
authored
2 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
d7fc96d1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
18 deletions
my_game.py
my_game.py
View file @
63dd28f9
...
@@ -9,6 +9,15 @@ class Block(pygame.sprite.Sprite):
...
@@ -9,6 +9,15 @@ class Block(pygame.sprite.Sprite):
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
class
Player
(
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
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
...
@@ -21,22 +30,16 @@ road = pygame.image.load('road.png') # 路
...
@@ -21,22 +30,16 @@ 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'
)
# 灌木丛
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
image
.
load
(
'hero5.png'
)]
index
=
0
class
Player
(
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
# shan_x=0
gamestate
=
True
index
=
0
y
=
400
y
=
400
jumpState
=
"runing"
jumpState
=
"runing"
t
=
30
t
=
30
...
@@ -54,30 +57,28 @@ while True:
...
@@ -54,30 +57,28 @@ while True:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpState
=
"up"
wukong
=
Player
(
hero
[
index
])
print
(
wukong
.
image
)
if
gamestate
:
if
jumpState
==
"up"
:
# 起跳状态
if
jumpState
==
"up"
:
# 起跳状态
if
t
>
0
:
if
t
>
0
:
y
-=
t
y
-=
t
# wukong.rect.y = y
t
-=
2
t
-=
2
else
:
else
:
jumpState
=
"down"
jumpState
=
"down"
if
jumpState
==
"down"
:
# 降落状态
if
jumpState
==
"down"
:
# 降落状态
if
t
<=
30
:
if
t
<=
30
:
y
+=
t
y
+=
t
# wukong.rect.y = y
t
+=
2
t
+=
2
else
:
else
:
jumpState
=
"runing"
jumpState
=
"runing"
t
=
30
t
=
30
# 悟空造型
# 悟空造型
wukong
=
Player
(
hero
[
index
])
if
jumpState
==
"runing"
:
# 跑步状态下
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
index
+=
1
if
index
>=
5
:
if
index
>=
5
:
index
=
0
index
=
0
screen
.
blit
(
wukong
.
image
,
(
wukong
.
rect
.
x
,
wukong
.
rect
.
y
))
# 悟空
# 将背景图画上去
# 将背景图画上去
mountain_x
-=
5
mountain_x
-=
5
if
mountain_x
<
-
1000
:
if
mountain_x
<
-
1000
:
...
@@ -88,7 +89,7 @@ while True:
...
@@ -88,7 +89,7 @@ while True:
if
road_x
<=
-
1000
:
if
road_x
<=
-
1000
:
road_x
=
0
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
# 路
screen
.
blit
(
road
,
(
road_x
,
500
))
# 路
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
# 悟空
# if things.rect.x <= 0-things.rect.width: # 障碍物消失
# if things.rect.x <= 0-things.rect.width: # 障碍物消失
# # 创建障碍物对象
# # 创建障碍物对象
...
@@ -104,13 +105,13 @@ while True:
...
@@ -104,13 +105,13 @@ while True:
block_list
.
add
(
things
)
block_list
.
add
(
things
)
time
=
0
time
=
0
for
i
in
block_list
:
for
i
in
block_list
:
i
.
rect
.
x
-=
8
i
.
rect
.
x
-=
8
screen
.
blit
(
i
.
image
,
(
i
.
rect
.
x
,
i
.
rect
.
y
))
screen
.
blit
(
i
.
image
,
(
i
.
rect
.
x
,
i
.
rect
.
y
))
if
i
.
rect
.
x
<
0
-
i
.
rect
.
width
:
if
i
.
rect
.
x
<
0
-
i
.
rect
.
width
:
i
.
kill
()
i
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
i
):
screen
.
blit
(
gameover
,(
400
,
240
))
gamestate
=
False
# screen.blit(things.image, (things.rect.x, things.rect.y))
# screen.blit(things.image, (things.rect.x, things.rect.y))
# 刷新画面
# 刷新画面
...
...
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