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
b0c0bb4c
authored
Apr 09, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
022ec8df
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
151 additions
and
18 deletions
my_game.py
score.txt
my_game.py
View file @
b0c0bb4c
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
time
import
random
import
json
with
open
(
"score.txt"
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
txt
=
f
.
read
()
dtxt
=
json
.
loads
(
txt
)
one
=
dtxt
[
"第1名"
]
two
=
dtxt
[
"第2名"
]
three
=
dtxt
[
"第3名"
]
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
fps
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
# 载入图片
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
apple
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
hero
=
pygame
.
image
.
load
(
'hero1.png'
)
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
index
=
0
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
display
.
set_caption
(
'悟空酷跑 -(@>_<@)-'
)
index
=
0
y
=
410
jumpstate
=
'r'
t
=
20
road_x
=
0
bg_x
=
0
time
=
0
gamestate
=
True
score
=
0
my_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
25
)
speed
=
8
bg_speed
=
1
class
Block
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
super
()
.
__init__
()
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
,
self
.
rect
.
y
=
1000
,
500
-
self
.
rect
.
height
self
.
score
=
1
class
Player
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
):
super
()
.
__init__
()
self
.
image
=
image
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
,
self
.
rect
.
y
=
150
,
400
Block_list
=
pygame
.
sprite
.
Group
()
while
True
:
while
True
:
speed
=
8
+
score
//
5
bg_speed
=
speed
//
5
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
.
key
==
locals
.
K_SPACE
or
event
.
key
==
locals
.
K_UP
or
event
.
key
==
locals
.
K_w
)
and
jumpstate
==
'r'
:
screen
.
blit
(
background
,
(
0
,
0
))
jumpstate
=
'u'
screen
.
blit
(
road
,
(
0
,
500
))
if
event
.
key
==
locals
.
K_0
:
screen
.
blit
(
hero
,
(
150
,
400
))
if
gamestate
==
False
:
# 刷新画面
gamestate
=
True
pygame
.
display
.
update
()
# prop.kill()
FPS
.
tick
(
60
)
score
=
0
\ No newline at end of file
if
event
.
key
==
locals
.
K_z
:
score
=
500
if
gamestate
==
True
:
wukong
=
Player
(
hero
[
index
])
road_x
-=
speed
if
road_x
<-
999
:
road_x
=
0
bg_x
-=
bg_speed
if
bg_x
<-
999
:
bg_x
=
0
# 将背景图画上去
screen
.
blit
(
background
,
(
bg_x
,
0
))
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
.
image
,
(
170
,
y
))
if
jumpstate
==
'r'
:
index
+=
1
y
=
410
if
index
==
5
:
index
=
0
if
jumpstate
==
'u'
:
if
t
>
0
:
y
-=
t
wukong
.
rect
.
y
=
y
t
-=
0.99
else
:
jumpstate
=
'd'
if
jumpstate
==
'd'
:
if
t
<=
20
:
y
+=
t
wukong
.
rect
.
y
=
y
t
+=
0.99
else
:
jumpstate
=
'r'
t
=
20
time
+=
1
if
time
>=
60
:
time
=
0
r
=
random
.
randint
(
0
,
50
)
if
r
>
16
:
obstacle
=
Block
(
stone
,
cacti
,
bush
)
Block_list
.
add
(
obstacle
)
for
prop
in
Block_list
:
prop
.
rect
.
x
-=
speed
if
prop
.
rect
.
x
<
0
-
prop
.
rect
.
width
:
prop
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
prop
):
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
gamestate
=
False
screen
.
blit
(
gameover
,(
400
,
200
))
print
(
'哈哈!…………………………………………………………………………………………………………………………'
)
if
score
>
three
:
if
score
>
one
:
dtxt
[
'第1名'
]
=
score
dtxt
[
'第2名'
]
=
one
dtxt
[
'第3名'
]
=
two
elif
score
>
two
:
dtxt
[
'第2名'
]
=
score
dtxt
[
'第3名'
]
=
two
else
:
dtxt
[
'第3名'
]
=
score
txt
=
json
.
dumps
(
dtxt
,
ensure_ascii
=
False
)
with
open
(
"score.txt"
,
'w'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
txt
)
else
:
if
wukong
.
rect
.
x
>
prop
.
rect
.
x
+
prop
.
rect
.
width
:
score
+=
prop
.
score
prop
.
score
=
0
screen
.
blit
(
prop
.
image
,(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
# 刷新画面
font
=
my_font
.
render
(
'score: '
+
str
(
score
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
font
,(
880
,
15
))
font
=
my_font
.
render
(
'one: '
+
str
(
one
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
font
,(
880
,
45
))
font
=
my_font
.
render
(
'two: '
+
str
(
two
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
font
,(
880
,
75
))
font
=
my_font
.
render
(
'three: '
+
str
(
three
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
font
,(
880
,
105
))
pygame
.
display
.
update
()
fps
.
tick
(
60
)
\ No newline at end of file
score.txt
View file @
b0c0bb4c
{"第1名": 0, "第2名": 0, "第3名": 0}
{"第1名": 4, "第2名": 2, "第3名": 1}
\ 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