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
5fddf660
authored
Jul 29, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
4d36d640
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
39 deletions
my_game.py
my_game.py
View file @
5fddf660
...
@@ -15,6 +15,19 @@ cacti = pygame.image.load('cacti.png') # 仙人掌
...
@@ -15,6 +15,19 @@ cacti = pygame.image.load('cacti.png') # 仙人掌
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
if
jumpstate
==
"run"
:
index
+=
1
if
index
==
5
:
index
=
0
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
if
jumpstate
==
"up"
:
#跳跃状态
if
t
>=
0
:
y
-=
t
wukong
.
rect
.
y
=
y
t
-=
2
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
else
:
music
=
pygame
.
mixer
.
sound
(
""
)
class
Block
(
pygame
.
sprite
.
Sprite
):
class
Block
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
super
()
.
__init__
()
super
()
.
__init__
()
...
@@ -22,15 +35,29 @@ class Block(pygame.sprite.Sprite):
...
@@ -22,15 +35,29 @@ class Block(pygame.sprite.Sprite):
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
1000
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
score
=
1
class
Wukong
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
):
super
()
.
__init__
()
self
.
image
=
image
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
150
self
.
rect
.
y
=
400
score
=
0
index
=
0
index
=
0
y
=
400
y
=
400
time
=
0
t
=
30
t
=
30
jumpstate
=
"run"
jumpstate
=
"run"
obstacle
=
Block
(
stone
,
cacti
,
bush
)
lu
=
road
.
get_rect
()
lu
=
road
.
get_rect
()
lu
.
x
=
0
lu
.
x
=
0
bg_x
=
0
bg_x
=
0
gamestate
=
True
jingling_list
=
pygame
.
sprite
.
Group
()
my_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
18
)
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
:
...
@@ -40,40 +67,62 @@ while True:
...
@@ -40,40 +67,62 @@ while True:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
if
jumpstate
==
"run"
:
if
jumpstate
==
"run"
:
jumpstate
=
"up"
jumpstate
=
"up"
# 将背景图画上去
if
gamestate
==
True
:
obstacle
.
rect
.
x
-=
8
# 将背景图画上去
lu
.
x
-=
8
bg_x
-=
1
bg_x
-=
1
screen
.
blit
(
background
,
(
bg_x
,
0
))
screen
.
blit
(
background
,
(
bg_x
,
0
))
if
bg_x
<-
1000
:
if
bg_x
<-
1000
:
bg_x
=
0
bg_x
=
0
screen
.
blit
(
road
,
(
lu
.
x
,
500
))
if
lu
.
x
<-
1000
:
lu
.
x
-=
8
lu
.
x
=
0
screen
.
blit
(
road
,
(
lu
.
x
,
500
))
screen
.
blit
(
obstacle
.
image
,(
obstacle
.
rect
.
x
,
obstacle
.
rect
.
y
))
if
lu
.
x
<-
1000
:
#判断是否超出画面左边缘,重新创建障碍物
lu
.
x
=
0
if
obstacle
.
rect
.
x
<
0
-
obstacle
.
rect
.
width
:
obstacle
=
Block
(
stone
,
cacti
,
bush
)
if
jumpstate
==
"up"
:
#跳跃状态
if
t
>=
0
:
y
-=
t
#判断是否超出画面左边缘,重新创建障碍物
t
-=
2
wukong
=
Wukong
(
hero
[
index
])
screen
.
blit
(
hero
[
index
],
(
150
,
y
))
jumestate
=
"down"
else
:
if
jumpstate
==
"down"
:
#跳跃状态
jumestate
=
"down"
if
t
<=
30
:
if
jumpstate
==
"down"
:
#跳跃状态
y
+=
t
if
t
<=
30
:
wukong
.
rect
.
y
=
y
y
+=
t
t
+=
2
t
+=
2
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
screen
.
blit
(
hero
[
index
],
(
150
,
y
))
else
:
else
:
jumestate
=
"run"
jumestate
=
"run"
t
=
30
t
=
30
if
jumpstate
==
"run"
:
index
+=
1
time
+=
1
if
index
==
5
:
if
time
>=
60
:
index
=
0
time
=
0
screen
.
blit
(
hero
[
index
],
(
150
,
y
))
yun
=
random
.
randint
(
0
,
50
)
# 刷新画面
if
yun
>
25
:
pygame
.
display
.
update
()
obstacle
=
Block
(
stone
,
bush
,
cacti
)
FPS
.
tick
(
60
)
jingling_list
.
add
(
obstacle
)
\ No newline at end of file
for
sprite
in
jingling_list
:
sprite
.
rect
.
x
-=
8
screen
.
blit
(
sprite
.
image
,
(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
gamewu
=
pygame
.
image
.
load
(
'gameover.png'
)
screen
.
blit
(
gamewu
,
(
400
,
200
))
gamestate
=
False
if
sprite
.
rect
.
x
<
0
-
sprite
.
rect
.
width
:
sprite
.
kill
()
else
:
if
sprite
.
rect
.
x
-
sprite
.
rect
.
width
<
wukong
.
rect
.
x
:
music
.
play
()
score
+=
sprite
.
score
sprite
.
score
=
0
text
=
my_font
.
render
(
"分数"
+
str
(
score
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
text
,(
900
,
0
))
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ 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