Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_diy2
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
a0351b05
authored
Mar 05, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
212505d6
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
9 deletions
my_game.py
my_game.py
View file @
a0351b05
...
@@ -4,11 +4,11 @@ import random
...
@@ -4,11 +4,11 @@ import random
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
class
Zhangai
(
pygame
.
sprite
.
Sprite
):
class
Zhangai
(
pygame
.
sprite
.
Sprite
):
#子类Zhangai继承父类精灵
def
__init__
(
self
,
image1
,
image2
,
image3
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
#形参,后面再用实参
super
()
.
__init__
()
super
()
.
__init__
()
#继承
self
.
image
=
random
.
choice
([
stone
,
cacti
,
bush
])
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
#随机选,后面用实参
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
=
self
.
image
.
get_rect
()
#前面要加self
self
.
rect
.
x
=
1000
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
rect
.
y
=
500
-
self
.
rect
.
height
...
@@ -36,7 +36,10 @@ t = 30
...
@@ -36,7 +36,10 @@ t = 30
road_x
=
0
road_x
=
0
bg_x
=
0
bg_x
=
0
zhangai
=
Zhangai
(
stone
,
cacti
,
bush
)
time
=
0
#zhangai = Zhangai(stone,cacti,bush)#设置变量
zhangai_list
=
pygame
.
sprite
.
Group
()
#创建精灵组
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
...
@@ -79,10 +82,22 @@ while True:
...
@@ -79,10 +82,22 @@ while True:
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
,
(
150
,
y
))
screen
.
blit
(
wukong
,
(
150
,
y
))
if
zhangai
.
rect
.
x
<=
0
-
zhangai
.
rect
.
width
:
#障碍物完全消失在窗口
time
+=
1
if
time
>=
60
:
r
=
random
.
randint
(
0
,
100
)
if
r
>
40
:
zhangai
=
Zhangai
(
stone
,
cacti
,
bush
)
zhangai
=
Zhangai
(
stone
,
cacti
,
bush
)
zhangai
.
rect
.
x
-=
8
#左移
zhangai_list
.
add
(
zhangai
)
screen
.
blit
(
zhangai
.
image
,
(
zhangai
.
rect
.
x
,
zhangai
.
rect
.
y
))
#渲染
time
=
0
for
prop
in
zhangai_list
:
prop
.
rect
.
x
-=
8
screen
.
blit
(
prop
.
image
,
(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
#渲染
if
prop
.
rect
.
x
<=
0
-
prop
.
rect
.
width
:
prop
.
kill
()
#if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口
# zhangai = Zhangai(stone,cacti,bush)#刷新
#zhangai.rect.x -=8 #左移
#screen.blit(zhangai.image, (zhangai.rect.x, zhangai.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