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
35b208eb
authored
3 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
35db2ab9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
16 deletions
my_game.py
my_game.py
View file @
35b208eb
...
@@ -24,12 +24,12 @@ class Block(pygame.sprite.Sprite):
...
@@ -24,12 +24,12 @@ class Block(pygame.sprite.Sprite):
super
()
.
__init__
()
super
()
.
__init__
()
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
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
index
=
0
index
=
0
y
=
400
y
=
400
jumpState
=
"runing"
jumpState
=
"run
n
ing"
t
=
30
t
=
30
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
=
obstacle
.
get_rect
()
...
@@ -37,8 +37,10 @@ rect.x = 1000
...
@@ -37,8 +37,10 @@ rect.x = 1000
rect
.
y
=
500
-
rect
.
height
rect
.
y
=
500
-
rect
.
height
road_x
=
0
road_x
=
0
bg_x
=
0
bg_x
=
0
time
=
0
block_list
=
pygame
.
sprite
.
Group
()
obstacle
=
Block
(
bush
,
stone
,
cacti
)
#
obstacle=Block(bush, stone, cacti)
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
...
@@ -46,7 +48,7 @@ while True:
...
@@ -46,7 +48,7 @@ while True:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
jumpState
==
"runing"
:
if
jumpState
==
"run
n
ing"
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpState
=
"up"
...
@@ -61,12 +63,12 @@ while True:
...
@@ -61,12 +63,12 @@ while True:
y
+=
t
y
+=
t
t
+=
2
t
+=
2
else
:
else
:
jumpState
=
"runing"
jumpState
=
"run
n
ing"
t
=
30
t
=
30
# 悟空造型
# 悟空造型
wukong
=
hero
[
index
]
wukong
=
hero
[
index
]
if
jumpState
==
"runing"
:
# 跑步状态下
if
jumpState
==
"run
n
ing"
:
# 跑步状态下
index
+=
1
index
+=
1
if
index
>=
5
:
if
index
>=
5
:
index
=
0
index
=
0
...
@@ -75,19 +77,32 @@ while True:
...
@@ -75,19 +77,32 @@ while True:
if
bg_x
<-
1000
:
if
bg_x
<-
1000
:
bg_x
=
0
bg_x
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远处背景
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远处背景
road_x
-=
8
if
road_x
<-
1000
:
if
road_x
<-
1000
:
road_x
=
0
road_x
=
0
road_x
-=
8
screen
.
blit
(
road
,
(
road_x
,
500
))
# 路
screen
.
blit
(
road
,
(
road_x
,
500
))
# 路
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
if
obstacle
.
rect
.
x
<=
0
-
obstacle
.
rect
.
width
:
# 障碍物消失
# if obstacle.rect.x <= 0-obstacle.rect.width: # 障碍物消失
# 创建障碍物对象
# # 创建障碍物对象
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
#
# obstacle
time
+=
1
if
time
>=
60
:
t
=
random
.
randint
(
0
,
100
)
if
t
>
40
:
obstacle
=
Block
(
bush
,
stone
,
cacti
)
block_list
.
add
(
obstacle
)
time
=
0
obstacle
.
rect
.
x
-=
8
for
pop
in
block_list
:
screen
.
blit
(
obstacle
.
image
,
(
obstacle
.
rect
.
x
,
obstacle
.
rect
.
y
))
pop
.
rect
.
x
-=
8
screen
.
blit
(
pop
.
image
,(
pop
.
rect
.
x
,
pop
.
rect
.
y
))
if
pop
.
rect
.
x
<
0
-
pop
.
rect
.
width
:
pop
.
kill
()
# screen.blit(obstacle.image, (obstacle.rect.x, obstacle.rect.y))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
FPS
.
tick
(
30
)
\ No newline at end of file
\ 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