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
848dbcb9
authored
Jun 18, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
27613fb0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
3 deletions
my_game.py
my_game.py
View file @
848dbcb9
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
import
random
bianliang
=
0
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
class
Block
(
pygame
.
sprite
.
Sprite
):
class
Block
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
...
@@ -9,6 +10,8 @@ class Block(pygame.sprite.Sprite):
...
@@ -9,6 +10,8 @@ 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
.
bianliang
=
1
self
.
rect
.
width
=
10
class
Player
(
pygame
.
sprite
.
Sprite
):
class
Player
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
):
def
__init__
(
self
,
image
):
super
()
.
__init__
()
super
()
.
__init__
()
...
@@ -16,12 +19,13 @@ class Player(pygame.sprite.Sprite):
...
@@ -16,12 +19,13 @@ class Player(pygame.sprite.Sprite):
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
150
self
.
rect
.
x
=
150
self
.
rect
.
y
=
400
self
.
rect
.
y
=
400
self
.
rect
.
width
=
3
0
self
.
rect
.
width
=
0
# 创建一个窗口
# 创建一个窗口
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_caption
(
"sha13游戏"
)
pygame
.
display
.
set_caption
(
"sha13游戏"
)
# 载入图片
# 载入图片
score_audio
=
pygame
.
mixer
.
Sound
(
'score.wav'
)
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'
)
# 石头
...
@@ -32,6 +36,8 @@ hero = [pygame.image.load('hero1.png'),
...
@@ -32,6 +36,8 @@ hero = [pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
image
.
load
(
'hero5.png'
)]
basic_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
18
)
score_audio
.
play
()
index
=
0
index
=
0
y
=
400
y
=
400
jumpState
=
"runing"
jumpState
=
"runing"
...
@@ -39,8 +45,10 @@ t = 30
...
@@ -39,8 +45,10 @@ t = 30
block_list
=
pygame
.
sprite
.
Group
()
block_list
=
pygame
.
sprite
.
Group
()
lu
=
0
lu
=
0
beijing
=
0
beijing
=
0
old_bianliang
=
0
time
=
0
time
=
0
panduan
=
True
panduan
=
True
speed
=
8
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
:
...
@@ -50,6 +58,7 @@ while True:
...
@@ -50,6 +58,7 @@ while True:
if
jumpState
==
"runing"
:
if
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpState
=
"up"
speed
=
8
+
bianliang
//
3
wukong
=
Player
(
hero
[
index
])
wukong
=
Player
(
hero
[
index
])
if
jumpState
==
"runing"
:
# 跑步状态下
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
index
+=
1
...
@@ -90,13 +99,23 @@ while True:
...
@@ -90,13 +99,23 @@ while True:
obstacle
=
Block
(
bush
,
cacti
,
stone
)
obstacle
=
Block
(
bush
,
cacti
,
stone
)
block_list
.
add
(
obstacle
)
block_list
.
add
(
obstacle
)
for
prop
in
block_list
:
for
prop
in
block_list
:
prop
.
rect
.
x
-=
8
prop
.
rect
.
x
-=
speed
screen
.
blit
(
prop
.
image
,(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
screen
.
blit
(
prop
.
image
,(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
if
prop
.
rect
.
x
<
0
-
prop
.
rect
.
width
:
if
prop
.
rect
.
x
<
0
-
prop
.
rect
.
width
:
prop
.
kill
()
# 刷新画面
prop
.
kill
()
# 刷新画面
if
pygame
.
sprite
.
collide_rect
(
wukong
,
prop
):
if
pygame
.
sprite
.
collide_rect
(
wukong
,
prop
):
gameover
=
pygame
.
image
.
load
(
"gameover.png"
)
gameover
=
pygame
.
image
.
load
(
"gameover.png"
)
screen
.
blit
(
gameover
,(
400
,
200
))
screen
.
blit
(
gameover
,(
400
,
200
))
panduan
=
False
panduan
=
False
else
:
if
(
prop
.
rect
.
x
+
prop
.
rect
.
width
)
<
wukong
.
rect
.
x
:
bianliang
+=
prop
.
bianliang
prop
.
bianliang
=
0
if
bianliang
>
old_bianliang
:
score_audio
.
play
()
old_bianliang
=
bianliang
sconreSurf
=
basic_font
.
render
(
"分数:"
+
str
(
bianliang
),
True
,(
255
,
255
,
255
))
screen
.
blit
(
sconreSurf
,(
880
,
20
))
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
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