Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_diy3
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
e2a88b51
authored
Mar 28, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
8f8d478f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
16 deletions
my_game.py
my_game.py
View file @
e2a88b51
...
@@ -5,12 +5,16 @@ import random
...
@@ -5,12 +5,16 @@ import random
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
):
super
()
.
_init_
()
super
()
.
__init__
()
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
# 加载障碍物的图片
sef
.
rect
=
self
.
image
.
get_rect
()
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
rect
.
x
=
1000
# 根据障碍物位图的宽高设置矩形
self
.
rect
.
y
=
500
-
self
.
rect
.
height
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时钟,控制游戏速度(帧数)
...
@@ -26,16 +30,18 @@ hero = [pygame.image.load('hero1.png'),
...
@@ -26,16 +30,18 @@ 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'
)]
Block
=
pygame
.
sprite
.
Group
()
block_list
=
pygame
.
sprite
.
Group
()
#
Block=pygame.sprite.Group()
bgx
=
0
bgx
=
0
index
=
0
index
=
0
y
=
400
y
=
400
jumpState
=
"runing"
jumpState
=
"runing"
t
=
30
t
=
30
obstacle
=
Block
(
bush
,
cacti
,
stone
)
rect
=
obstacle
.
get_rect
()
# rect = obstacle.get_rect()
rect
.
x
=
1000
# rect.x = 1000
rect
.
y
=
500
-
rect
.
height
# rect.y = 500 - rect.height
roadx
=
0
roadx
=
0
time
=
0
time
=
0
pygame
.
display
.
set_caption
(
'悟空酷跑'
)
pygame
.
display
.
set_caption
(
'悟空酷跑'
)
...
@@ -78,26 +84,33 @@ while True:
...
@@ -78,26 +84,33 @@ while True:
# obstacle=Block(bush,cacti,stone)
# obstacle=Block(bush,cacti,stone)
# obstacle.rect.x-=8
# obstacle.rect.x-=8
# screen.blit(obstacle.image, (obstacle.rect.x,obstacle.rect.y))
# screen.blit(obstacle.image, (obstacle.rect.x,obstacle.rect.y))
# roadx-=8
# if roadx<=-1000:
roadx
-=
8
# roadx=0
if
roadx
<=-
1000
:
screen
.
blit
(
road
,(
roadx
,
500
))
roadx
=
0
screen
.
blit
(
road
,
(
roadx
,
500
))
bgx
-=
1
bgx
-=
1
if
bgx
<-
1000
:
if
bgx
<-
1000
:
bgx
=
0
bgx
=
0
screen
.
blit
(
background
,
(
bgx
,
0
))
screen
.
blit
(
background
,
(
bgx
,
0
))
screen
.
blit
(
wukong
,
(
150
,
y
))
time
+=
1
time
+=
1
if
time
>=
60
:
if
time
>=
60
:
r
=
random
.
randint
(
0
,
100
)
r
=
random
.
randint
(
0
,
100
)
if
r
>
40
:
if
r
>
40
:
obstacle
=
Block
(
bush
,
cacti
,
stone
)
obstacle
=
Block
(
bush
,
cacti
,
stone
)
block_list
=
add
(
obstacle
)
#block_list=add(obstacle)
block_list
.
add
(
obstacle
)
time
=
0
time
=
0
for
prop
in
block_list
:
for
prop
in
block_list
:
prop
.
rect
.
x
-=
8
prop
.
rect
.
x
-=
8
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
()
# 刷新画面
# 刷新画面
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