Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_diy4
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
076c766d
authored
Oct 29, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
802b6353
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
18 deletions
my_game.py
my_game.py
View file @
076c766d
...
...
@@ -42,8 +42,8 @@ hero = [pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
score_audio
=
pygame
.
mixer
.
Sound
(
'score.wav'
)
basic_font
=
pygame
.
font
.
Font
(
'STKAITI.TTV'
,
18
)
#
score_audio = pygame.mixer.Sound('score.wav')
index
=
0
y
=
400
...
...
@@ -58,12 +58,12 @@ old_score=score
block_list
=
pygame
.
sprite
.
Group
()
with
open
(
'
score
.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
with
open
(
'
record
.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
content
=
f
.
read
()
record
=
json
.
loads
(
content
)
one
=
content
[
"第1名"
]
two
=
content
[
"第2名"
]
thr
=
content
[
"第3名"
]
one
=
record
[
"第1名"
]
two
=
record
[
"第2名"
]
thr
=
record
[
"第3名"
]
while
True
:
for
event
in
pygame
.
event
.
get
():
...
...
@@ -75,7 +75,7 @@ while True:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
speed
=
8
+
score
//
3
speed
=
8
+
score
//
3
wukong
=
Player
(
hero
[
index
])
if
jumpState
==
"runing"
:
# 跑步状态下
...
...
@@ -106,7 +106,7 @@ while True:
bg_
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远处背景
road_x
-=
speed
road_x
-=
speed
if
road_x
<-
1000
:
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
...
...
@@ -122,14 +122,43 @@ while True:
block_list
.
add
(
obstacle
)
for
sprite
in
block_list
:
sprite
.
rect
.
x
-=
speed
#10.22ri
if
aa
.
rect
.
x
<=
0
-
aa
.
rect
.
width
:
# 障碍物消失
# 创建障碍物对象
aa
=
Block
(
bush
,
cacti
,
stone
)
aa
.
rect
.
x
-=
8
screen
.
blit
(
aa
.
image
,
(
aa
.
rect
.
x
,
aa
.
rect
.
y
))
screen
.
blit
(
sprint
.
image
,(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
sprite
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
screen
.
blit
(
gameover
,(
400
,
200
))
gamestate
=
False
if
score
>
three
:
if
score
>
one
:
record
[
"第1名"
]
=
score
record
[
"第2名"
]
=
one
record
[
"第3名"
]
=
two
elif
score
>
two
:
record
[
"第2名"
]
=
score
record
[
"第3名"
]
=
two
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
:
if
sprite
.
rect
.
x
+
sprite
.
rect
.
width
<
wukong
.
rect
.
x
:
score
+=
sprite
.
score
sprint
.
score
=
0
if
score
>
old_score
:
score_audio
.
play
()
old_score
=
score
scoreSurf
=
basic_font
.
render
(
"分数:"
+
str
(
score
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
scoreSurf
,(
880
,
20
))
scoreSurf
=
basic_font
.
render
(
"第1名:"
+
str
(
one
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
scoreSurf
,(
880
,
50
))
scoreSurf
=
basic_font
.
render
(
"第2名:"
+
str
(
two
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
scoreSurf
,(
880
,
80
))
scoreSurf
=
basic_font
.
render
(
"第3名:"
+
str
(
three
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
scoreSurf
,(
880
,
110
))
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ No newline at end of file
FPS
.
tick
(
60
)
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