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
1d701d50
authored
Nov 05, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
3bb2e58c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
6 deletions
my_game.py
record.txt
my_game.py
View file @
1d701d50
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
import
random
import
json
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
...
@@ -48,10 +50,23 @@ road_x = 0
...
@@ -48,10 +50,23 @@ road_x = 0
bg_x
=
0
bg_x
=
0
time
=
0
time
=
0
gamestate
=
True
gamestate
=
True
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
score
=
0
#初始化分数
my_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
25
)
# 设置字体
speed
=
10
#初始速度
music
=
pygame
.
mixer
.
Sound
(
'score.wav'
)
with
open
(
'record.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
recode
=
file
.
read
()
recode
=
json
.
loads
(
recode
)
one
=
recode
[
'第1名'
]
two
=
recode
[
'第2名'
]
three
=
recode
[
'第3名'
]
print
(
one
,
two
,
three
)
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
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
...
@@ -60,7 +75,7 @@ while True:
...
@@ -60,7 +75,7 @@ while True:
if
jumpState
==
"runing"
:
if
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpState
=
"up"
speed
=
10
+
score
//
3
# 悟空造型
# 悟空造型
wukong
=
Player
(
hero
[
index
])
wukong
=
Player
(
hero
[
index
])
if
jumpState
==
"runing"
:
# 跑步状态下
if
jumpState
==
"runing"
:
# 跑步状态下
...
@@ -91,7 +106,7 @@ while True:
...
@@ -91,7 +106,7 @@ while True:
bg_x
=
0
bg_x
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远景
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远景
road_x
-=
8
road_x
-=
speed
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
))
# 道路
...
@@ -106,15 +121,47 @@ while True:
...
@@ -106,15 +121,47 @@ while True:
obstacle
=
Block
(
bush
,
cacti
,
stone
)
obstacle
=
Block
(
bush
,
cacti
,
stone
)
block_list
.
add
(
obstacle
)
block_list
.
add
(
obstacle
)
for
sprite
in
block_list
:
# 遍历、展示障碍物精灵
for
sprite
in
block_list
:
# 遍历、展示障碍物精灵
sprite
.
rect
.
x
-=
8
sprite
.
rect
.
x
-=
speed
screen
.
blit
(
sprite
.
image
,
(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
screen
.
blit
(
sprite
.
image
,
(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
score
+=
1
music
.
play
()
sprite
.
kill
()
sprite
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
# 精灵碰撞检测
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
# 精灵碰撞检测
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
if
score
>
three
:
if
score
>
one
:
recode
[
'第1名'
]
=
score
recode
[
'第2名'
]
=
one
recode
[
'第3名'
]
=
two
elif
score
>
two
:
recode
[
'第2名'
]
=
score
recode
[
'第3名'
]
=
two
else
:
recode
[
'第3名'
]
=
score
content
=
json
.
dumps
(
recode
,
ensure_ascii
=
False
)
with
open
(
'record.txt'
,
'w'
,
encoding
=
'utf-8'
)
as
file
:
file
.
write
(
content
)
text
=
my_font
.
render
(
'分数:'
+
str
(
score
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
text
,(
900
,
20
))
text1
=
my_font
.
render
(
'第1名:'
+
str
(
one
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
text1
,(
900
,
60
))
text2
=
my_font
.
render
(
'第2名:'
+
str
(
two
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
text2
,(
900
,
100
))
text3
=
my_font
.
render
(
'第3名:'
+
str
(
three
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
text3
,(
900
,
140
))
text4
=
my_font
.
render
(
'速度:'
+
str
(
speed
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
text4
,(
10
,
20
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
\ No newline at end of file
record.txt
View file @
1d701d50
{"第1名": 0, "第2名": 0, "第3名": 0}
{"第1名": 4, "第2名": 3, "第3名": 3}
\ 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