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
7fdd198f
authored
Oct 16, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
080ee70a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
140 additions
and
51 deletions
my_game.py
my_game.py
View file @
7fdd198f
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
# 载入图片
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
apple
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
index
=
0
G
=
5
y
=
400
IM
=
'running'
from
pygame
import
locals
import
random
from
random
import
*
pygame
.
init
()
# 初始化
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
# 载入图片
bg
=
pygame
.
image
.
load
(
'bg.png'
)
go
=
pygame
.
image
.
load
(
'gameover.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
apple
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
image
.
load
(
'hero5.png'
)]
index
=
0
index
=
0
y
=
400
G
=
40
jumpState
=
"runing"
y
=
400
t
=
30
IM
=
'running'
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
I_M
=
0
rect
=
obstacle
.
get_rect
()
rond_x
=
0
rect
.
x
=
1000
bg_x
=
0
rect
.
y
=
500
-
rect
.
height
ind
=
0
dw
,
s
=
150
,
0
time
=
0
PS
=
60
gametate
=
True
class
Rsc
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
im1
,
im2
,
im3
):
super
()
.
__init__
()
self
.
image
=
choice
([
im1
,
im2
,
im3
])
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
class
heros
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
hero
):
super
()
.
__init__
()
self
.
image
=
hero
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
150
self
.
rect
.
y
=
400
obg
=
Rsc
(
stone
,
cacti
,
apple
)
wukong
=
heros
(
hero
[
index
])
blist
=
pygame
.
sprite
.
Group
()
while
True
:
while
True
:
wukong
=
heros
(
hero
[
index
])
print
(
G
)
time
+=
1
if
time
>=
60
:
a
=
randint
(
1
,
60
)
if
a
>=
20
:
obg
=
Rsc
(
stone
,
cacti
,
apple
)
blist
.
add
(
obg
)
time
=
0
if
IM
==
'running'
:
ind
+=
1
if
ind
>=
5
:
index
+=
1
ind
=
0
if
index
==
5
:
index
=
0
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
if
IM
==
'running'
:
jumpState
=
"up"
I_M
=
0
if
IM
!=
'up'
and
IM
!=
'down'
:
if
jumpState
==
"up"
:
# 起跳状态
IM
=
'up'
if
t
>
0
:
I_M
=
1
y
-=
t
if
IM
!=
'running'
and
I_M
>=
1
:
t
-=
2
if
event
.
key
==
locals
.
K_w
:
else
:
IM
=
'dup'
jumpState
=
"down"
I_M
=
0
if
jumpState
==
"down"
:
# 降落状态
G
+=
2
if
t
<=
30
:
if
index
<=
4
:
y
+=
t
index
+=
1
t
+=
2
if
index
==
5
:
else
:
index
=
1
jumpState
=
"runing"
dws
=
150
,
y
t
=
30
if
gametate
==
True
:
if
IM
==
'up'
:
# 悟空造型
y
-=
G
wukong
=
hero
[
index
]
wukong
.
rect
.
y
=
y
if
jumpState
==
"runing"
:
# 跑步状态下
G
-=
2
index
+=
1
if
G
<=
-
42
:
if
index
>=
5
:
IM
=
'running'
index
=
0
G
=
40
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 远处背景
# if IM=='down':
screen
.
blit
(
road
,
(
0
,
500
))
# 路
# if G==0:
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
# y+=G
# G+=2
# elif G>=0:
# y+=G
# G+=2
# if IM=='dup':
# if G>=0:
# y-=G
# G-=0.15
# else:
# IM='down'
herorun
=
hero
[
index
]
if
rect
.
x
<=
0
-
rect
.
width
:
# 障碍物消失
# 将背景图画上去
# 创建障碍物对象
bg_x
-=
1
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
if
bg_x
<=-
1000
:
rect
=
obstacle
.
get_rect
()
bg_x
=
0
rect
.
x
=
1000
screen
.
blit
(
bg
,
(
bg_x
,
0
))
rect
.
y
=
500
-
rect
.
height
rond_x
-=
8
rect
.
x
-=
8
if
rond_x
<=
-
1000
:
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
rond_x
=
0
# 刷新画面
screen
.
blit
(
road
,
(
rond_x
,
500
))
pygame
.
display
.
update
()
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
FPS
.
tick
(
60
)
# if obg.rect.x<0-obg.rect.height:
\ No newline at end of file
# obg=Rsc(stone,cacti,apple)
for
pop
in
blist
:
pop
.
rect
.
x
-=
8
screen
.
blit
(
pop
.
image
,(
pop
.
rect
.
x
,
pop
.
rect
.
y
))
if
pygame
.
sprite
.
collide_rect
(
wukong
,
pop
):
screen
.
blit
(
go
,(
400
,
200
))
gametate
=
False
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_r
:
gametate
=
True
pygame
.
sprite
.
kill
(
pop
)
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
PS
)
\ 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