Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson9_diy3
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
26ccace0
authored
Mar 19, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
05a496e8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
9 deletions
my_game.py
record.txt
my_game.py
View file @
26ccace0
...
@@ -58,7 +58,7 @@ speed = 8#初始速度
...
@@ -58,7 +58,7 @@ speed = 8#初始速度
zhangai_list
=
pygame
.
sprite
.
Group
()
#创建精灵组
zhangai_list
=
pygame
.
sprite
.
Group
()
#创建精灵组
with
open
(
'record.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
#打开文件
with
open
(
'record.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
#打开文件
content
=
f
.
read
()
content
=
f
.
read
()
#读取
record
=
json
.
loads
(
content
)
#把json字符串转换为python字典
record
=
json
.
loads
(
content
)
#把json字符串转换为python字典
one
=
record
[
'第1名'
]
#第一名的值
one
=
record
[
'第1名'
]
#第一名的值
two
=
record
[
'第2名'
]
#第二名的值
two
=
record
[
'第2名'
]
#第二名的值
...
@@ -74,7 +74,7 @@ while True:
...
@@ -74,7 +74,7 @@ while True:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpstate
=
'up'
#按下空格,模式为跳跃
jumpstate
=
'up'
#按下空格,模式为跳跃
speed
=
8
+
score
//
3
#分数每
speed
=
8
+
score
//
3
#分数每
增加三分障碍物移速加一
wukong
=
Player
(
hero
[
index
])
#变向遍历列表
wukong
=
Player
(
hero
[
index
])
#变向遍历列表
if
jumpstate
==
'runing'
:
#只有在跑步才能切换造型
if
jumpstate
==
'runing'
:
#只有在跑步才能切换造型
index
+=
1
#索引增加
index
+=
1
#索引增加
...
@@ -85,14 +85,14 @@ while True:
...
@@ -85,14 +85,14 @@ while True:
if
t
>
0
:
#小于起跳最大值
if
t
>
0
:
#小于起跳最大值
y
-=
t
#起跳
y
-=
t
#起跳
wukong
.
rect
.
y
=
y
#避免出现人在天上飞却死了的bug
wukong
.
rect
.
y
=
y
#避免出现人在天上飞却死了的bug
t
-=
2
t
-=
1.5
else
:
else
:
jumpstate
=
'down'
#落下
jumpstate
=
'down'
#落下
if
jumpstate
==
'down'
:
#降落
if
jumpstate
==
'down'
:
#降落
if
t
<=
30
:
#大于落地最大值
if
t
<=
30
:
#大于落地最大值
y
+=
t
#落下
y
+=
t
#落下
wukong
.
rect
.
y
=
y
#同理
wukong
.
rect
.
y
=
y
#同理
t
+=
2
t
+=
1.5
else
:
else
:
jumpstate
=
'runing'
#跑步
jumpstate
=
'runing'
#跑步
t
=
30
#重定义t
t
=
30
#重定义t
...
@@ -127,6 +127,19 @@ while True:
...
@@ -127,6 +127,19 @@ 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
#变为False
gamestate
=
False
#变为False
if
score
>
thr
:
if
score
>
one
:
record
[
'第1名'
]
=
score
record
[
'第2名'
]
=
one
record
[
'第3名'
]
=
two
elif
score
>
two
:
record
[
'第2名'
]
=
score
record
[
'第3名'
]
=
thr
else
:
record
[
'第3名'
]
=
score
record
=
json
.
dumps
(
record
,
ensure_ascii
=
False
)
with
open
(
'record.txt'
,
'w'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
record
)
else
:
else
:
if
prop
.
rect
.
x
+
prop
.
rect
.
width
<
wukong
.
rect
.
x
:
if
prop
.
rect
.
x
+
prop
.
rect
.
width
<
wukong
.
rect
.
x
:
score
+=
prop
.
score
score
+=
prop
.
score
...
@@ -144,11 +157,11 @@ while True:
...
@@ -144,11 +157,11 @@ while True:
scoreSurf
=
basic_font
.
render
(
'分数:'
+
str
(
score
),
True
,(
200
,
200
,
200
))
scoreSurf
=
basic_font
.
render
(
'分数:'
+
str
(
score
),
True
,(
200
,
200
,
200
))
screen
.
blit
(
scoreSurf
,(
880
,
20
))
screen
.
blit
(
scoreSurf
,(
880
,
20
))
scoreSurf
=
basic_font
.
render
(
'第1名:'
+
str
(
one
),
True
,(
200
,
200
,
200
))
scoreSurf
=
basic_font
.
render
(
'第1名:'
+
str
(
one
),
True
,(
200
,
200
,
200
))
#渲染第一名
screen
.
blit
(
scoreSurf
,(
880
,
50
))
screen
.
blit
(
scoreSurf
,(
880
,
50
))
scoreSurf
=
basic_font
.
render
(
'第2名:'
+
str
(
two
),
True
,(
200
,
200
,
200
))
scoreSurf
=
basic_font
.
render
(
'第2名:'
+
str
(
two
),
True
,(
200
,
200
,
200
))
#渲染第二名
screen
.
blit
(
scoreSurf
,(
880
,
80
))
screen
.
blit
(
scoreSurf
,(
880
,
80
))
scoreSurf
=
basic_font
.
render
(
'第3名:'
+
str
(
thr
),
True
,(
200
,
200
,
200
))
scoreSurf
=
basic_font
.
render
(
'第3名:'
+
str
(
thr
),
True
,(
200
,
200
,
200
))
#渲染第三名
screen
.
blit
(
scoreSurf
,(
880
,
110
))
screen
.
blit
(
scoreSurf
,(
880
,
110
))
# 刷新画面
# 刷新画面
...
...
record.txt
View file @
26ccace0
{"第1名": 0, "第2名": 0, "第3名": 0}
{"第1名": 1, "第2名": 0, "第3名": 0}
\ No newline at end of file
\ 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