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
f40a308e
authored
Feb 25, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
4dd6ff52
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
57 deletions
my_game.py
my_game.py
View file @
f40a308e
...
@@ -8,9 +8,16 @@ class Block(pygame.sprite.Sprite):
...
@@ -8,9 +8,16 @@ 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
.
width
self
.
rect
.
y
=
500
-
self
.
rect
.
width
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
block_list
=
pygame
.
sprite
.
Group
()
block_list
=
pygame
.
sprite
.
Group
()
gameover
=
True
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
...
@@ -36,59 +43,60 @@ obstacle = Block(bush,stone,cacti)
...
@@ -36,59 +43,60 @@ obstacle = Block(bush,stone,cacti)
road_x
=
0
road_x
=
0
bg_x
=
0
bg_x
=
0
if
gameover
==
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
while
True
:
if
jumpState
==
"up"
:
# 起跳状态
for
event
in
pygame
.
event
.
get
():
if
t
>
0
:
if
event
.
type
==
locals
.
QUIT
:
y
-=
t
# 接收到退出事件后退出程序
t
-=
2
exit
()
else
:
if
event
.
type
==
locals
.
KEYDOWN
:
jumpState
=
"down"
if
jumpState
==
"runing"
:
if
jumpState
==
"down"
:
# 降落状态
if
event
.
key
==
locals
.
K_SPACE
:
if
t
<=
30
:
jumpState
=
"up"
y
+=
t
t
+=
2
if
jumpState
==
"up"
:
# 起跳状态
else
:
if
t
>
0
:
jumpState
=
"runing"
y
-=
t
t
=
30
t
-=
2
wukong
=
Player
(
hero
[
index
])
else
:
if
jumpState
==
"runing"
:
jumpState
=
"down"
index
+=
1
if
jumpState
==
"down"
:
# 降落状态
if
index
>=
5
:
if
t
<=
30
:
index
=
0
y
+=
t
# 将背景图画上去
t
+=
2
bg_x
-=
1
else
:
if
bg_x
<=
-
1000
:
jumpState
=
"runing"
bg_x
=
0
t
=
30
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远处背景
road_x
-=
8
# 悟空造型
if
road_x
<=
-
1000
:
wukong
=
hero
[
index
]
road_x
=
0
if
jumpState
==
"runing"
:
# 跑步状态下
screen
.
blit
(
road
,
(
road_x
,
500
))
# 路
index
+=
1
time
+=
1
if
index
>=
5
:
if
time
>=
60
:
index
=
0
time
=
0
# 将背景图画上去
num
=
random
.
randint
(
0
,
50
)
bg_x
-=
1
if
num
>
20
:
if
bg_x
<=
-
1000
:
obstacle
=
Block
(
bush
,
cacti
,
stone
)
bg_x
=
0
block_list
.
add
(
obstacle
)
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远处背景
for
sprite
in
block_list
:
road_x
-=
8
sprite
.
rect
.
x
-=
8
if
road_x
<=
-
1000
:
screen
.
blit
(
sprite
.
image
,(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
road_x
=
0
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
# 障碍物消失
screen
.
blit
(
road
,
(
road_x
,
500
))
# 路
sprite
.
kill
()
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
time
+=
1
gameover
=
pygame
.
image
.
load
(
"gameover.png"
)
if
time
>=
60
:
screen
.
blit
(
gameover
,(
400
,
200
))
time
=
0
gameover
=
Flase
num
=
random
.
randint
(
0
,
50
)
# 刷新画面
if
num
>
20
:
pygame
.
display
.
update
()
obstacle
=
Block
(
bush
,
cacti
,
stone
)
FPS
.
tick
(
60
)
block_list
.
add
(
obstacle
)
\ No newline at end of file
for
sprite
in
block_list
:
sprite
.
rect
.
x
-=
8
screen
.
blit
(
sprite
.
image
,(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
# 障碍物消失
sprite
.
kill
()
# 刷新画面
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