Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_diy1
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
b4321700
authored
Aug 04, 2024
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
2742489a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
41 deletions
my_game.py
my_game.py
View file @
b4321700
...
@@ -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
))
...
@@ -31,7 +40,9 @@ index = 0
...
@@ -31,7 +40,9 @@ index = 0
y
=
400
y
=
400
jumpState
=
"runing"
jumpState
=
"runing"
t
=
30
t
=
30
aa
=
Block
(
bush
,
cacti
,
stone
)
time
=
0
zaw
=
pygame
.
sprite
.
Group
()
zt
=
True
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
...
@@ -43,44 +54,62 @@ while True:
...
@@ -43,44 +54,62 @@ while True:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpState
=
"up"
if
jumpState
==
"up"
:
# 起跳状态
if
zt
==
True
:
if
t
>
0
:
y
-=
t
# 悟空造型
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
else
:
jumpState
=
"runing"
t
=
30
# 悟空造型
wukong
=
hero
[
index
]
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
if
index
>=
5
:
index
=
0
# 将背景图画上去
shan_x
-=
2
if
shan_x
<-
1000
:
shan_x
=
0
screen
.
blit
(
background
,
(
shan_x
,
0
))
# 远处背景
lu_x
-=
8
if
lu_x
<-
1000
:
lu_x
=
0
screen
.
blit
(
road
,
(
lu_x
,
500
))
# 路
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
if
aa
.
rect
.
x
<=
0
-
aa
.
rect
.
width
:
# 障碍物消失
if
jumpState
==
"up"
:
# 起跳状态
# 创建障碍物对象
if
t
>
0
:
aa
=
Block
(
bush
,
cacti
,
stone
)
y
-=
t
aa
.
rect
.
x
-=
8
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
# 将背景图画上去
shan_x
-=
2
if
shan_x
<-
1000
:
shan_x
=
0
screen
.
blit
(
background
,
(
shan_x
,
0
))
# 远处背景
lu_x
-=
8
if
lu_x
<-
1000
:
lu_x
=
0
screen
.
blit
(
road
,
(
lu_x
,
500
))
# 路
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
time
+=
1
screen
.
blit
(
aa
.
image
,
(
aa
.
rect
.
x
,
aa
.
rect
.
y
))
if
time
>
60
:
# 刷新画面
time
=
0
pygame
.
display
.
update
()
num
=
random
.
randint
(
0
,
50
)
FPS
.
tick
(
60
)
if
num
>
20
:
\ No newline at end of file
aa
=
Block
(
bush
,
cacti
,
stone
)
zaw
.
add
(
aa
)
for
jingling
in
zaw
:
jingling
.
rect
.
x
-=
8
screen
.
blit
(
jingling
.
image
,
(
jingling
.
rect
.
x
,
jingling
.
rect
.
y
))
if
jingling
.
rect
.
x
<=
0
-
jingling
.
rect
.
width
:
jingling
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
jingling
):
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
screen
.
blit
(
gameover
,
(
400
,
200
))
zt
=
False
# 刷新画面
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