Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson7_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
f2aea7bf
authored
Jun 30, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
64044261
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
10 deletions
my_game.py
my_game.py
View file @
f2aea7bf
...
@@ -20,6 +20,11 @@ hero = [pygame.image.load('hero1.png'), # 五张悟空跑步图片放入一
...
@@ -20,6 +20,11 @@ hero = [pygame.image.load('hero1.png'), # 五张悟空跑步图片放入一
index
=
0
# 作为列表的下标
index
=
0
# 作为列表的下标
y
=
400
# 纵坐标
y
=
400
# 纵坐标
jumpState
=
"runing"
# 表示正在跑步
jumpState
=
"runing"
# 表示正在跑步
t
=
30
# 表示跳跃时每次移动的距离
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
...
@@ -27,27 +32,42 @@ while True:
...
@@ -27,27 +32,42 @@ while True:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
# 发生键盘按键事件
if
event
.
type
==
locals
.
KEYDOWN
:
# 发生键盘按键事件
if
event
.
key
==
locals
.
K_SPACE
:
# 检测是否按下空格键
if
jumpState
==
"runing"
:
jumpState
=
"up"
# 表示上升
if
event
.
key
==
locals
.
K_SPACE
:
# 检测是否按下空格键
if
jumpState
==
"up"
:
# 上升
jumpState
=
"up"
# 表示上升
if
y
>
150
:
y
-=
5
if
jumpState
==
"up"
:
# 起跳
if
y
>
0
:
y
-=
t
t
-=
2
else
:
else
:
jumpState
=
"down"
# 表示降落
jumpState
=
"down"
# 表示降落
if
jumpState
=
"down"
:
# 降落
if
jumpState
=
"down"
:
# 降落
if
y
<
400
:
if
y
<
30
:
y
+=
5
y
+=
t
t
+=
2
else
:
else
:
jumpState
=
"runing"
jumpState
=
"runing"
t
=
30
wukong
=
hero
[
index
]
# 按照下标,取出悟空的造型并展示出来
wukong
=
hero
[
index
]
# 按照下标,取出悟空的造型并展示出来
index
+=
1
# 变量增加1
if
jumpState
==
"runing"
:
if
index
>=
5
:
# 诺超出列表长度
index
+=
1
# 变量增加1
index
=
0
# 重新设置为0
if
index
>=
5
:
# 诺超出列表长度
index
=
0
# 重新设置为0
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
road
,
(
0
,
500
))
screen
.
blit
(
road
,
(
0
,
500
))
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
if
rect
.
x
<=
0
-
rect
.
width
:
# 判段障碍物x坐标超过0减去障碍物的宽度(说明障碍物完全消失在画面中)
# 创建障碍物对象
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
rect
.
x
-=
8
# 让障碍物的x坐标不断减小
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
# 将它渲染出来
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
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