Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson9_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
ede7d8e4
authored
Jul 11, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
3bb2e58c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
11 deletions
my_game.py
my_game.py
View file @
ede7d8e4
...
@@ -9,6 +9,7 @@ class Block(pygame.sprite.Sprite): # 障碍物精灵类
...
@@ -9,6 +9,7 @@ class Block(pygame.sprite.Sprite): # 障碍物精灵类
super
()
.
__init__
()
super
()
.
__init__
()
# 加载障碍物的图片
# 加载障碍物的图片
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
score
=
20
# 根据障碍物位图的宽高设置矩形
# 根据障碍物位图的宽高设置矩形
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
=
self
.
image
.
get_rect
()
# 障碍物绘制坐标
# 障碍物绘制坐标
...
@@ -42,28 +43,30 @@ hero = [pygame.image.load('hero1.png'),
...
@@ -42,28 +43,30 @@ hero = [pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
image
.
load
(
'hero5.png'
)]
index
=
0
index
=
0
y
=
400
y
=
400
jumpState
=
"runing"
jumpState
=
"run
n
ing"
t
=
30
t
=
30
road_x
=
0
road_x
=
0
b
g
_x
=
0
b
ackground
_x
=
0
time
=
0
time
=
0
score
=
0
gamestate
=
True
gamestate
=
True
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
basic_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
13
)
while
True
:
while
True
:
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
jumpState
==
"run
n
ing"
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpState
=
"up"
# 悟空造型
# 悟空造型
wukong
=
Player
(
hero
[
index
])
wukong
=
Player
(
hero
[
index
])
if
jumpState
==
"runing"
:
# 跑步状态下
if
jumpState
==
"run
n
ing"
:
# 跑步状态下
index
+=
1
index
+=
1
if
index
>=
5
:
if
index
>=
5
:
index
=
0
index
=
0
...
@@ -82,21 +85,21 @@ while True:
...
@@ -82,21 +85,21 @@ while True:
wukong
.
rect
.
y
=
y
wukong
.
rect
.
y
=
y
t
+=
2
t
+=
2
else
:
else
:
jumpState
=
"runing"
jumpState
=
"run
n
ing"
t
=
30
t
=
30
# 将背景图画上去
# 将背景图画上去
b
g
_x
-=
1
b
ackground
_x
-=
1
if
b
g
_x
<=-
1000
:
if
b
ackground
_x
<=-
1000
:
b
g
_x
=
0
b
ackground
_x
=
0
screen
.
blit
(
background
,
(
bg
_x
,
0
))
# 远景
screen
.
blit
(
background
,
(
background
_x
,
0
))
# 远景
road_x
-=
8
road_x
-=
8
if
road_x
<=-
1000
:
if
road_x
<=-
1000
:
road_x
=
0
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
# 道路
screen
.
blit
(
road
,(
road_x
,
500
))
# 道路
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
# 悟空
screen
.
blit
(
wukong
.
image
,(
150
,
y
))
# 悟空
time
+=
1
time
+=
1
if
time
>=
60
:
# 创建障碍物精灵
if
time
>=
60
:
# 创建障碍物精灵
...
@@ -114,6 +117,13 @@ while True:
...
@@ -114,6 +117,13 @@ while True:
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
# 游戏结束
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
# 游戏结束
screen
.
blit
(
gameover
,
(
400
,
200
))
screen
.
blit
(
gameover
,
(
400
,
200
))
gamestate
=
False
gamestate
=
False
else
:
if
sprite
.
rect
.
x
+
sprite
.
rect
.
width
<
wukong
.
rect
.
x
:
score
+=
sprite
.
score
#分数
sprite
.
score
=
0
scoreSurf
=
basic_font
.
render
(
"分数:"
+
str
(
score
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
scoreSurf
,(
880
,
20
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
...
...
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