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
c9a45194
authored
Mar 27, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
080ee70a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
74 deletions
my_game.py
my_game.py
View file @
c9a45194
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
import
random
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建一个窗口
#
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
class
Block
(
pygame
.
sprite
.
Sprite
):
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
def
__init__
(
self
,
image1
,
image2
,
image3
):
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
super
()
.
__init__
()
# 载入图片
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
self
.
rect
=
self
.
image
.
get_rect
()
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
self
.
rect
.
x
=
1000
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
self
.
rect
.
y
=
500
-
self
.
rect
.
height
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
# 创建一个窗口
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
pygame
.
image
.
load
(
'hero3.png'
),
# 载入图片
pygame
.
image
.
load
(
'hero4.png'
),
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
pygame
.
image
.
load
(
'hero5.png'
)]
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
index
=
0
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
y
=
400
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
jumpState
=
"runing"
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
t
=
30
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
pygame
.
image
.
load
(
'hero2.png'
),
rect
=
obstacle
.
get_rect
()
pygame
.
image
.
load
(
'hero3.png'
),
rect
.
x
=
1000
pygame
.
image
.
load
(
'hero4.png'
),
rect
.
y
=
500
-
rect
.
height
pygame
.
image
.
load
(
'hero5.png'
)]
index
=
0
while
True
:
y
=
400
for
event
in
pygame
.
event
.
get
():
jumpState
=
"runing"
if
event
.
type
==
locals
.
QUIT
:
t
=
30
# 接收到退出事件后退出程序
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
exit
()
rect
=
obstacle
.
get_rect
()
if
event
.
type
==
locals
.
KEYDOWN
:
rect
.
x
=
1000
if
jumpState
==
"runing"
:
rect
.
y
=
500
-
rect
.
height
if
event
.
key
==
locals
.
K_SPACE
:
road_x
=
0
jumpState
=
"up"
bg_x
=
0
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
else
:
# 悟空造型
jumpState
=
"down"
wukong
=
hero
[
index
]
if
jumpState
==
"down"
:
# 降落状态
if
jumpState
==
"runing"
:
# 跑步状态下
if
t
<=
30
:
index
+=
1
y
+=
t
if
index
>=
5
:
t
+=
2
index
=
0
else
:
# 将背景图画上去
jumpState
=
"runing"
screen
.
blit
(
background
,
(
0
,
0
))
# 远处背景
t
=
30
screen
.
blit
(
road
,
(
0
,
500
))
# 路
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
# 悟空造型
wukong
=
hero
[
index
]
if
rect
.
x
<=
0
-
rect
.
width
:
# 障碍物消失
if
jumpState
==
"runing"
:
# 跑步状态下
# 创建障碍物对象
index
+=
1
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
if
index
>=
5
:
rect
=
obstacle
.
get_rect
()
index
=
0
rect
.
x
=
1000
# 将背景图画上去
rect
.
y
=
500
-
rect
.
height
bg_x
-=
1
rect
.
x
-=
8
if
bg_x
<=-
1000
:
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
bg_x
=
0
# 刷新画面
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远处背景
pygame
.
display
.
update
()
screen
.
blit
(
road
,
(
0
,
500
))
# 路
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
#道路
road_x
-=
8
if
road_x
<=-
1000
:
road_x
=
0
screen
.
blit
(
road
,(
road_x
,
500
))
#道路
if
rect
.
x
<=
0
-
rect
.
width
:
# 障碍物消失
# 创建障碍物对象
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
rect
.
x
-=
8
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
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