Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_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
4168e34c
authored
4 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
7cf60527
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
31 deletions
my_game.py
my_game.py
View file @
4168e34c
...
...
@@ -17,7 +17,9 @@ hero = [pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
number
=
0
y
=
400
t
=
30
class
Block
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
super
()
.
__init__
()
...
...
@@ -33,15 +35,14 @@ class Player(pygame.sprite.Sprite):
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
150
self
.
rect
.
y
=
400
group
=
pygame
.
sprite
.
Group
()
group
=
pygame
.
sprite
.
Group
()
index
=
0
y
=
400
jumpState
=
"runing"
t
=
30
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
time
=
0
changex
=
0
gamestate
=
False
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
...
...
@@ -51,45 +52,51 @@ while True:
if
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
if
changex
<=-
1000
:
if
changex
<=-
1000
:
# 路的移动
changex
=
0
else
:
changex
-=
8
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
==
False
:
if
jumpState
==
"up"
:
# 起跳状态
if
t
>
0
:
y
-=
t
wukong
.
rect
.
y
=
y
t
-=
2
else
:
jumpState
=
"down"
if
jumpState
==
"down"
:
# 降落状态
if
t
<=
30
:
y
+=
t
wukong
.
rect
.
y
=
y
t
+=
2
else
:
jumpState
=
"runing"
t
=
30
# 悟空造型
wukong
=
hero
[
index
]
wukong
=
Player
(
hero
[
index
])
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
if
index
>=
5
:
index
=
0
# 将背景图画上去
time
+=
1
if
time
>=
60
:
time
=
0
number
=
random
.
randint
(
0
,
50
)
if
number
>
20
:
obstacle
=
Block
(
stone
,
cacti
,
bush
)
group
.
add
(
obstacle
)
screen
.
blit
(
background
,
(
0
,
0
))
# 远处背景
screen
.
blit
(
road
,
(
changex
,
500
))
# 路
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
# 悟空
if
rect
.
x
<=
0
-
rect
.
width
:
# 障碍物消失
# 创建障碍物对象
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
rect
.
x
-=
8
for
sprite
in
group
:
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
pygame
.
sprite
.
collide_rect
()
screen
.
blit
(
sprite
.
image
,
(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
sprite
.
rect
.
x
-=
8
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
# 障碍物消失
sprite
.
kill
()
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
30
)
\ No newline at end of file
# pygame.sprite.collide_rect()
\ 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