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
f6f84259
authored
Jun 10, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
d807163f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
40 deletions
my_game.py
my_game.py
View file @
f6f84259
...
@@ -2,72 +2,92 @@ import pygame
...
@@ -2,72 +2,92 @@ import pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
import
random
pygame
.
init
()
# 鍒濆鍖?
pygame
.
init
()
# 鍒濆鍖?
# 鍒涘缓涓€涓獥鍙?
class
Block
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
super
()
.
__init__
()
# 鍔犺浇闅滅鐗╃殑鍥剧墖
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
# 鏍规嵁闅滅鐗╀綅鍥剧殑瀹介珮璁剧疆鐭╁舰
self
.
rect
=
self
.
image
.
get_rect
()
# 闅滅鐗╃粯鍒跺潗鏍?
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
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
(
"
悟空跑酷
"
)
pygame
.
display
.
set_caption
(
"
鎮熺┖閰疯窇
"
)
# 杞藉叆鍥剧墖
# 杞藉叆鍥剧墖
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'
)
# 鐭冲ご
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 浠欎汉鎺?
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 浠欎汉鎺?
apple
=
pygame
.
image
.
load
(
'bush.png'
)
# 鐏屾湪涓
?
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 鐏屾湪涓L
?
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero2.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'
)]
index
=
0
index
=
0
y
=
400
y
=
400
jump
=
"runing"
jumpState
=
"runing"
t
=
30
t
=
30
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
bg_x
_
=
0
rect
.
x
=
1000
road_x
=
0
rect
.
y
=
500
-
rect
.
height
time
=
0
rreg6e64
# 鍒涘缓闅滅鐗?
Block_list
=
pygame
.
sprite
.
Group
()
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
:
# 鎺ユ敹鍒伴€€鍑轰簨浠跺悗閫€鍑
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
jump
==
"runing"
:
if
jump
State
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jump
=
"up"
jumpState
=
"up"
if
jump
==
"up"
:
if
t
>
0
:
y
-=
t
t
-=
2
if
jumpState
==
"up"
:
# 璧疯烦鐘舵€?
if
t
>
0
:
y
-=
t
t
-=
2
else
:
else
:
jump
=
"down"
jump
State
=
"down"
if
jump
==
"down"
:
if
jump
State
==
"down"
:
# 闄嶈惤鐘舵€?
if
t
<=
30
:
if
t
<=
30
:
y
+=
t
y
+=
t
t
+=
2
t
+=
2
else
:
else
:
jump
=
"runing"
jumpState
=
"runing"
t
=
30
t
=
30
wukong
=
hero
[
index
]
# 鎮熺┖閫犲瀷
if
jump
==
"runing"
:
wukong
=
hero
[
index
]
if
jumpState
==
"runing"
:
# 璺戞鐘舵€佷笅
index
+=
1
index
+=
1
if
index
=
=
5
:
if
index
>
=
5
:
index
=
0
index
=
0
# 灏嗚儗鏅浘鐢讳笂鍘?
# 灏嗚儗鏅浘鐢讳笂鍘?
screen
.
blit
(
background
,
(
0
,
0
))
bg_x
-=
1
screen
.
blit
(
road
,
(
0
,
500
))
if
bg_x
<=-
1000
:
screen
.
blit
(
wukong
,
(
150
,
y
))
bg_x
=
0
if
rect
.
x
<
0
-
rect
.
width
:
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
screen
.
blit
(
background
,
(
bg_x
))
rect
=
obstacle
.
get_rect
road_x
-=
8
rect
.
x
=
1000
if
road_x
<=-
1000
:
rect
.
y
=
500
-
rect
.
height
road_x
=
0
rect
.
x
-=
8
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
screen
.
blit
(
wukong
,
(
150
,
y
))
# 鎮熺┖
obstacle
=
Block
(
bush
,
cacti
,
stone
)
Block_list
.
add
(
obstacle
)
screen
.
blit
(
obstacle
.
image
,
(
obstacle
.
rect
.
x
,
obstacle
.
rect
.
y
))
# 鍒锋柊鐢婚潰
# 鍒锋柊鐢婚潰
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