Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson7_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
1fde446e
authored
Aug 02, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
b6278a34
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
53 deletions
my_game.py
my_game.py
View file @
1fde446e
...
...
@@ -10,6 +10,15 @@ class Block(pygame.sprite.Sprite):
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
y
=
400
class
Block2
(
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
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
zaw_list
=
pygame
.
sprite
.
Group
()
...
...
@@ -29,15 +38,15 @@ pygame.display.set_caption('悟空酷跑')
a
=
0
t
=
30
zaw
=
Block
(
stone
,
cacti
,
apple
)
wukong_1
=
hero
[
0
]
wukong_1
=
Block2
(
hero
[
0
])
wukong
=
'running'
y
=
400
#z=random.choice([stone,cacti,apple])
#rect=stone.get_rect()
#z_x=1000
time
=
0
road_x
=
0
bg_x
=
0
gameover
=
False
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
...
...
@@ -46,56 +55,65 @@ while True:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_SPACE
and
wukong
==
'running'
:
wukong
=
'up'
if
wukong
==
'up'
:
if
t
>
0
:
y
-=
t
t
-=
2
else
:
wukong
=
'down'
if
wukong
==
'down'
:
if
t
<=
30
:
y
+=
t
t
+=
2
else
:
wukong
=
'running'
t
=
30
#y=400
if
wukong
==
'running'
:
wukong_1
=
hero
[
a
]
a
+=
1
if
a
>=
5
:
a
=
0
# 将背景图画上去
screen
.
blit
(
background
,
(
bg_x
,
0
))
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong_1
,
(
150
,
y
))
#z_x-=8
road_x
-=
8
bg_x
-=
1
#if zaw.rect.x<= 0-zaw.rect.width:
time
+=
1
if
time
>=
60
:
time
=
0
num
=
random
.
randint
(
0
,
50
)
if
num
>
20
:
zaw
=
Block
(
stone
,
cacti
,
apple
)
zaw_list
.
add
(
zaw
)
for
i
in
zaw_list
:
i
.
rect
.
x
-=
8
screen
.
blit
(
i
.
image
,
(
i
.
rect
.
x
,
500
-
i
.
rect
.
height
))
if
i
.
rect
.
x
<=
0
-
i
.
rect
.
width
:
i
.
kill
()
if
gameover
==
False
:
if
wukong
==
'running'
:
wukong_1
=
Block2
(
hero
[
a
])
a
+=
1
if
a
>=
5
:
a
=
0
if
wukong
==
'up'
:
if
t
>
0
:
y
-=
t
wukong_1
.
rect
.
y
=
y
t
-=
2
else
:
wukong
=
'down'
if
wukong
==
'down'
:
if
t
<=
30
:
y
+=
t
wukong_1
.
rect
.
y
=
y
t
+=
2
else
:
wukong
=
'running'
t
=
30
#y=400
#z=random.choice([stone,cacti,apple])
#rect=stone.get_rect()
#z_x=1000
# 将背景图画上去
screen
.
blit
(
background
,
(
bg_x
,
0
))
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong_1
.
image
,
(
150
,
y
))
#z_x-=8
road_x
-=
8
bg_x
-=
1
#if zaw.rect.x<= 0-zaw.rect.width:
time
+=
1
if
time
>=
60
:
time
=
0
num
=
random
.
randint
(
0
,
50
)
if
num
>
20
:
zaw
=
Block
(
stone
,
cacti
,
apple
)
zaw_list
.
add
(
zaw
)
for
i
in
zaw_list
:
i
.
rect
.
x
-=
8
screen
.
blit
(
i
.
image
,
(
i
.
rect
.
x
,
500
-
i
.
rect
.
height
))
if
i
.
rect
.
x
<=
0
-
i
.
rect
.
width
:
i
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong_1
,
i
):
gmover
=
pygame
.
image
.
load
(
'gameover.png'
)
screen
.
blit
(
gmover
,(
400
,
250
))
gameover
=
True
#z=random.choice([stone,cacti,apple])
#rect=stone.get_rect()
#z_x=1000
if
road_x
<-
1000
:
road_x
=
0
if
bg_x
<-
1000
:
bg_x
=
0
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ No newline at end of file
if
road_x
<-
1000
:
road_x
=
0
if
bg_x
<-
1000
:
bg_x
=
0
# 刷新画面
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