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
f123c64f
authored
Nov 19, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
190e9a14
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
49 deletions
my_game.py
score.wav
my_game.py
View file @
f123c64f
...
@@ -11,7 +11,15 @@ class Block(pygame.sprite.Sprite):
...
@@ -11,7 +11,15 @@ 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
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
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
...
@@ -27,6 +35,9 @@ hero = [pygame.image.load('hero1.png'),
...
@@ -27,6 +35,9 @@ 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
=
pygame
.
mixer
.
Sound
(
'score.wav'
)
index
=
0
index
=
0
y
=
400
y
=
400
jumpState
=
'runing'
jumpState
=
'runing'
...
@@ -34,6 +45,9 @@ t = 30
...
@@ -34,6 +45,9 @@ t = 30
road_x
=
0
road_x
=
0
bg_x
=
0
bg_x
=
0
time
=
0
time
=
0
gamestate
=
True
score
=
0
old_score
=
score
block_list
=
pygame
.
sprite
.
Group
()
block_list
=
pygame
.
sprite
.
Group
()
while
True
:
while
True
:
...
@@ -47,56 +61,76 @@ while True:
...
@@ -47,56 +61,76 @@ while True:
jumpState
=
'up'
jumpState
=
'up'
if
jumpState
==
'up'
:
speed
=
8
+
score
//
3
if
t
>
0
:
wukong
=
Player
(
hero
[
index
])
y
-=
t
if
jumpState
==
'runing'
:
t
-=
2
index
+=
1
else
:
if
index
>=
5
:
jumpState
=
'down'
index
=
0
if
jumpState
==
'down'
:
if
t
<=
30
:
if
gamestate
==
True
:
y
+=
t
if
jumpState
==
'up'
:
t
+=
2
if
t
>
0
:
else
:
y
-=
t
jumpState
=
'runing'
wukong
.
rect
.
y
=
y
t
=
30
t
-=
2
else
:
wukong
=
hero
[
index
]
jumpState
=
'down'
index
+=
1
if
jumpState
==
'down'
:
if
index
>
4
:
if
t
<=
30
:
index
=
0
y
+=
t
wukong
.
rect
.
y
=
y
t
+=
2
else
:
jumpState
=
'runing'
t
=
30
# 将背景图画上
# 将背景图画上
bg_x
-=
2
bg_x
-=
2
if
bg_x
<=
-
1000
:
if
bg_x
<=
-
1000
:
bg_x
=
0
bg_x
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
screen
.
blit
(
background
,
(
bg_x
,
0
))
road_x
-=
8
road_x
-=
speed
if
road_x
<=
-
1000
:
if
road_x
<=
-
1000
:
road_x
=
0
road_x
=
0
screen
.
blit
(
road
,(
road_x
,
500
))
screen
.
blit
(
road
,(
road_x
,
500
))
screen
.
blit
(
wukong
,
(
150
,
y
))
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
#if obstacle.rect.x <= 0-obstacle.rect.width:
#obstacle = Block(bush,stone,cacti)
#obstacle.rect.x -= 8
#screen.blit(obstacle.image,(obstacle.rect.x,obstacle.rect.y))
#if obstacle.rect.x <= 0-obstacle.rect.width:
time
+=
1
#obstacle = Block(bush,stone,cacti)
if
time
>=
60
:
#obstacle.rect.x -= 8
r
=
random
.
randint
(
0
,
100
)
#screen.blit(obstacle.image,(obstacle.rect.x,obstacle.rect.y))
if
r
>
40
:
obstacle
=
Block
(
bush
,
cacti
,
stone
)
block_list
.
add
(
obstacle
)
time
=
0
time
+=
1
for
sprite
in
block_list
:
if
time
>=
60
:
sprite
.
rect
.
x
-=
speed
r
=
random
.
randint
(
0
,
100
)
screen
.
blit
(
sprite
.
image
,(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
if
r
>
40
:
sprite
.
kill
()
obstacle
=
Block
(
bush
,
cacti
,
stone
)
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
block_list
.
add
(
obstacle
)
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
time
=
0
screen
.
blit
(
gameover
,(
400
,
200
))
gamestate
=
False
else
:
if
sprite
.
rect
.
x
+
sprite
.
rect
.
width
<
wukong
.
rect
.
x
:
score
+=
sprite
.
score
sprite
.
score
=
0
print
(
score
)
if
score
>
old_score
:
score_audio
.
play
()
old_score
=
score
for
prop
in
block_list
:
scoreSurf
=
basic_font
.
render
(
'分数:'
+
str
(
score
),
True
,(
255
,
255
,
255
))
prop
.
rect
.
x
-=
8
screen
.
blit
(
scoreSurf
,(
880
,
20
))
screen
.
blit
(
prop
.
image
,(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
# 刷新画面
if
prop
.
rect
.
x
<=
0
-
prop
.
rect
.
width
:
pygame
.
display
.
update
()
prop
.
kill
()
FPS
.
tick
(
60
)
# 刷新画面
\ No newline at end of file
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ No newline at end of file
score.wav
0 → 100644
View file @
f123c64f
File added
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