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
b9e693ee
authored
Jan 02, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
c1849fb7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
52 deletions
my_game.py
my_game.py
View file @
b9e693ee
...
...
@@ -2,7 +2,7 @@
import
pygame
from
pygame
import
locals
import
random
gamestate
=
True
pygame
.
init
()
# 初始化
# 创建一个窗口
...
...
@@ -48,7 +48,6 @@ class Player(pygame.sprite.Sprite):
self
.
rect
.
x
=
150
self
.
rect
.
y
=
400
group_list
=
pygame
.
sprite
.
Group
()
while
True
:
for
event
in
pygame
.
event
.
get
():
...
...
@@ -58,59 +57,71 @@ while True:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_SPACE
and
state
==
'running'
:
state
=
'jumping'
# 切换跑步动作
if
state
==
'running'
:
wukong
=
hero
[
index
]
index
+=
1
if
index
>
4
:
index
=
0
# 跳跃
if
state
==
'jumping'
:
if
y
>
150
:
y
-=
t
t
-=
1.8
if
gamestate
==
True
:
if
state
==
'running'
:
wukong
=
hero
[
index
]
index
+=
1
if
index
>
4
:
index
=
0
wukong3
=
Player
(
wukong
)
# 跳跃
if
state
==
'jumping'
:
if
y
>
150
:
y
-=
t
t
-=
1.8
wukong3
.
rect_y
=
y
else
:
state
=
'downing'
# 下落
if
state
==
'downing'
:
if
y
<
400
:
y
+=
t
t
+=
1.8
wukong3
.
rect_y
=
y
else
:
state
=
'running'
t
=
30
y
=
400
wukong3
.
rect_y
=
y
# 障碍物移动
# if obstacle.rect.x <= 0 - obstacle.rect.width:
# obstacle = Block(bush,stone,cacti)
# obstacle.rect.x = obstacle.rect.x -10
# 路
if
road_x
>-
1000
:
road_x
=
road_x
-
10
else
:
state
=
'downing'
# 下落
if
state
==
'downing'
:
if
y
<
400
:
y
+=
t
t
+=
1.8
road_x
=
0
# 背景
if
background_x
>
-
1000
:
background_x
=
background_x
-
0.3
else
:
state
=
'running'
t
=
30
y
=
400
# 障碍物移动
# if obstacle.rect.x <= 0 - obstacle.rect.width:
# obstacle = Block(bush,stone,cacti)
# obstacle.rect.x = obstacle.rect.x -10
# 路
if
road_x
>-
1000
:
road_x
=
road_x
-
10
else
:
road_x
=
0
# 背景
if
background_x
>
-
1000
:
background_x
=
background_x
-
0.3
else
:
background_x
=
0
# 将背景图画上去
screen
.
blit
(
background
,
(
background_x
,
0
))
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
,
(
150
,
y
))
time
+=
1
if
time
>=
60
:
r
=
random
.
randint
(
0
,
5
)
if
r
>
2
:
obstacle
=
Block
(
bush
,
stone
,
cacti
)
group_list
.
add
(
obstacle
)
time
=
0
for
sprite
in
group_list
:
sprite
.
rect
.
x
-=
10
screen
.
blit
(
sprite
.
image
,(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
sprite
.
kill
()
background_x
=
0
# 将背景图画上去
screen
.
blit
(
background
,
(
background_x
,
0
))
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
,(
150
,
y
))
time
+=
1
if
time
>=
60
:
r
=
random
.
randint
(
0
,
5
)
if
r
>
2
:
obstacle
=
Block
(
bush
,
stone
,
cacti
)
group_list
.
add
(
obstacle
)
time
=
0
for
sprite
in
group_list
:
sprite
.
rect
.
x
-=
10
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
(
wukong3
,
sprite
)
==
True
:
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
screen
.
blit
(
gameover
,(
400
,
200
))
gamestate
=
False
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
50
)
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