Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_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
bd0a6f4b
authored
Oct 30, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
92662126
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
16 deletions
my_game.py
my_game.py
View file @
bd0a6f4b
import
pygame
from
pygame
import
locals
import
json
pygame
.
init
()
# 初始化
# 创建一个窗口
...
...
@@ -41,6 +42,9 @@ hero = [pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
a_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
18
)
s_v
=
pygame
.
mixer
.
Sound
(
'score.wav'
)
index
=
0
G
=
40
y
=
400
...
...
@@ -55,8 +59,11 @@ PS=60
gametate
=
True
yy
=
0
yyy
=
8
clock
=
1
l
=
0
score
=
0
speed
=
8
os
=
score
killer
=
False
class
Rsc
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
im1
,
im2
,
im3
):
...
...
@@ -65,6 +72,7 @@ class Rsc(pygame.sprite.Sprite):
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
score
=
1
class
heros
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
hero
):
super
()
.
__init__
()
...
...
@@ -77,15 +85,23 @@ wukong=heros(hero[index])
blist
=
pygame
.
sprite
.
Group
()
while
True
:
with
open
(
'record.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
cont
=
f
.
read
()
text
=
json
.
loads
(
cont
)
print
(
cont
)
one
=
text
[
'第1名'
]
two
=
text
[
'第2名'
]
three
=
text
[
'第3名'
]
wukong
=
heros
(
hero
[
index
])
print
(
clock
)
time
+=
0.5
*
clock
if
time
>=
60
:
a
=
randint
(
1
,
60
)
if
a
>=
20
:
obg
=
Rsc
(
stone
,
cacti
,
apple
)
blist
.
add
(
obg
)
time
=
0
if
gametate
==
True
:
time
+=
1
if
time
>=
60
:
a
=
randint
(
0
,
65
)
if
a
>=
20
:
obg
=
Rsc
(
stone
,
cacti
,
apple
)
blist
.
add
(
obg
)
time
=
10
speed
=
8
+
score
//
3
if
IM
==
'running'
:
ind
+=
1
if
ind
>=
5
:
...
...
@@ -107,6 +123,14 @@ while True:
if
IM
!=
'running'
and
I_M
>=
1
:
if
event
.
key
==
locals
.
K_w
:
IM
=
'dup'
if
event
.
key
==
locals
.
K_s
:
gametate
=
True
if
event
.
key
==
locals
.
K_q
:
killer
=
True
if
event
.
key
==
locals
.
K_e
:
killer
=
False
if
event
.
key
==
locals
.
K_p
:
speed
+=
10
if
gametate
==
True
:
if
IM
==
'up'
:
y
-=
G
...
...
@@ -134,11 +158,11 @@ while True:
herorun
=
hero
[
index
]
# 将背景图画上去
bg_x
-=
(
2
+
0.25
*
clock
)
bg_x
-=
2
if
bg_x
<=-
1000
:
bg_x
=
0
screen
.
blit
(
bg
,
(
bg_x
,
0
))
rond_x
-=
(
8
+
0.5
*
clock
)
rond_x
-=
speed
if
rond_x
<=
-
1000
:
rond_x
=
0
screen
.
blit
(
road
,
(
rond_x
,
500
))
...
...
@@ -146,14 +170,35 @@ while True:
# if obg.rect.x<0-obg.rect.height:
# obg=Rsc(stone,cacti,apple)
for
pop
in
blist
:
pop
.
rect
.
x
-=
(
8
+
clock
)
pop
.
rect
.
x
-=
speed
screen
.
blit
(
pop
.
image
,(
pop
.
rect
.
x
,
pop
.
rect
.
y
))
if
pop
.
rect
.
x
<=
0
-
pop
.
rect
.
width
:
pop
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
pop
):
screen
.
blit
(
go
,(
400
,
200
))
gametate
=
False
if
killer
==
True
:
pop
.
kill
()
score
+=
pop
.
score
speed
+=
pop
.
score
pop
.
score
=
0
else
:
screen
.
blit
(
go
,(
400
,
200
))
gametate
=
False
else
:
if
(
pop
.
rect
.
x
+
pop
.
rect
.
y
)
<
wukong
.
rect
.
x
-
wukong
.
rect
.
height
:
clock
+=
0.1
if
(
pop
.
rect
.
x
+
pop
.
rect
.
width
)
<=
wukong
.
rect
.
x
:
score
+=
pop
.
score
speed
+=
pop
.
score
pop
.
score
=
0
if
os
<
score
:
s_v
.
play
()
os
=
score
sF
=
a_font
.
render
(
"分数"
+
str
(
score
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
sF
,(
880
,
20
))
sF
=
a_font
.
render
(
"第一名"
+
str
(
one
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
sF
,(
880
,
40
))
sF
=
a_font
.
render
(
"第二名"
+
str
(
two
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
sF
,(
880
,
60
))
sF
=
a_font
.
render
(
"第三名"
+
str
(
three
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
sF
,(
880
,
80
))
# 刷新画面
...
...
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