Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson7_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
a2dffbaa
authored
2 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
fe699405
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
39 deletions
my_game.py
my_game.py
View file @
a2dffbaa
...
@@ -2,10 +2,27 @@ import pygame
...
@@ -2,10 +2,27 @@ import pygame
import
random
import
random
from
pygame
import
locals
from
pygame
import
locals
class
Block
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
super
()
.
__init__
()
self
.
image
=
random
.
choice
([
stone
,
cacti
,
apple
])
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
class
Player
(
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
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时钟,控制游戏速度(帧数)
#pygame.display.set.
# 载入图片
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
...
@@ -18,21 +35,25 @@ hero =[pygame.image.load('hero1.png'),
...
@@ -18,21 +35,25 @@ hero =[pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
image
.
load
(
'hero5.png'
)]
index
=
0
index
=
0
t
=
30
gamestat
=
True
road_x
=
0
time
=
0
background_x
=
0
t
=
35
jumpstat
=
'running'
jumpstat
=
'running'
y
=
400
y
=
400
obstacle
=
random
.
choice
([
stone
,
cacti
,
apple
])
obstacle
=
Block
(
stone
,
apple
,
cacti
)
rect
=
obstacle
.
get_rect
()
Block_list
=
pygame
.
sprite
.
Group
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
while
True
:
while
True
:
index
+=
1
if
index
==
5
:
index
=
0
wukong
=
hero
[
index
]
wukong
=
Player
(
hero
[
index
])
if
jumpstat
==
'running'
:
index
+=
1
if
index
==
5
:
index
=
0
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
...
@@ -41,34 +62,56 @@ while True:
...
@@ -41,34 +62,56 @@ while True:
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
if
gamestat
==
True
:
if
jumpstat
==
'jumpup'
:
if
t
>
0
:
y
-=
t
t
-=
2
else
:
jumpstat
=
'jumpdown'
if
jumpstat
==
'jumpdown'
:
if
t
<=
30
:
y
+=
t
t
+=
2
else
:
jumpstat
=
'running'
t
=
30
# 将背景图画上去
road_x
-=
8
screen
.
blit
(
background
,
(
0
,
0
))
if
road_x
<=-
1000
:
#小路移动
screen
.
blit
(
road
,
(
0
,
500
))
road_x
=
0
screen
.
blit
(
wukong
,
(
150
,
y
))
if
rect
.
x
<
0
-
rect
.
width
:
background_x
-=
2
obstacle
=
random
.
choice
([
stone
,
cacti
,
apple
])
if
background_x
<=-
1000
:
rect
=
obstacle
.
get_rect
()
background_x
=
0
rect
.
x
=
1000
if
jumpstat
==
'jumpup'
:
rect
.
y
=
500
-
rect
.
height
if
t
>
0
:
rect
.
x
-=
8
y
-=
t
screen
.
blit
(
obstacle
,(
rect
.
x
,
rect
.
y
))
wukong
.
rect
.
y
=
y
# 刷新画面
t
-=
2
pygame
.
display
.
update
()
else
:
FPS
.
tick
(
60
)
jumpstat
=
'jumpdown'
\ No newline at end of file
if
jumpstat
==
'jumpdown'
:
if
t
<=
35
:
y
+=
t
wukong
.
rect
.
y
=
y
t
+=
2
else
:
jumpstat
=
'running'
t
=
35
# 将背景图画上去
screen
.
blit
(
background
,
(
background_x
,
0
))
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
time
+=
1
if
time
>=
60
:
time
=
0
num
=
random
.
randint
(
0
,
50
)
if
num
>
20
:
obstacle
=
Block
(
stone
,
apple
,
cacti
)
Block_list
.
add
(
obstacle
)
for
sprite
in
Block_list
:
sprite
.
rect
.
x
-=
8
screen
.
blit
(
sprite
.
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
,
150
))
gamestat
=
False
screen
.
blit
(
obstacle
.
image
,(
obstacle
.
rect
.
x
,
obstacle
.
rect
.
y
))
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
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