Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
13680905
Break the Stones3
Commits
b4e21983
Commit
b4e21983
authored
Oct 30, 2020
by
13680905
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
05a03cc6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
131 additions
and
0 deletions
+131
-0
planes_sprites.py
planes_sprites.py
+131
-0
No files found.
planes_sprites.py
0 → 100644
View file @
b4e21983
import
random
import
pygame
#屏幕大小的常量
SCREEN_RECT
=
pygame
.
Rect
(
0
,
0
,
480
,
700
)
#刷新的帧率
FRAME_PER_SEC
=
60
#创建敌机的定时器常量
CREATE_ENEMY_EVENT
=
pygame
.
USEREVENT
HERO_FIRE_EVENT
=
pygame
.
USEREVENT
+
1
class
GameSprite
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image_name
,
speed
=
1
):
super
().
__init__
()
self
.
image
=
pygame
.
image
.
load
(
image_name
)
self
.
rect
=
self
.
image
.
get_rect
()
self
.
speed
=
speed
def
update
(
self
):
#在屏幕的垂直方向上移动
self
.
rect
.
y
+=
self
.
speed
class
Background
(
GameSprite
):
'''游戏背景精灵'''
def
__init__
(
self
,
is_alt
=
False
):
#调用父类方法实现精灵的创建(image/rect/speed)
super
().
__init__
(
"./images/background.png"
)
if
is_alt
:
self
.
rect
.
y
=
-
self
.
rect
.
height
#判断是否是交替图案,如果是,需要设置初始化位置
def
update
(
self
):
#1.调用父类的方法实现
super
().
update
()
#2.判断是否移除屏幕,如果移除,回上
if
self
.
rect
.
y
>=
SCREEN_RECT
.
height
:
self
.
rect
.
y
=-
SCREEN_RECT
.
height
class
Enemy
(
GameSprite
):
'''敌机精灵'''
def
__init__
(
self
):
#1.调用父类方法调用敌机精灵,同时指定敌机图片
super
().
__init__
(
"./images/stone.png"
)
#2,指定敌机的初始随机速度
self
.
speed
=
random
.
randint
(
2
,
5
)
#3.指定敌机的初始随机位置
self
.
rect
.
bottom
=
0
max_x
=
SCREEN_RECT
.
width
-
self
.
rect
.
width
w
=
random
.
randint
(
0
,
max_x
)
self
.
rect
.
x
=
w
def
update
(
self
):
#1.调用父类方法,保持垂直方向的飞行
super
().
update
()
#2.判断是否废除屏幕,若是,从精灵组中删除敌机
if
self
.
rect
.
y
>=
SCREEN_RECT
.
height
:
#kill方法可以将精灵从所有精灵组中移出,精灵就会被自动销毁
self
.
kill
()
pass
def
__del__
(
self
):
'''print("敌机挂了%s" %self.rect)'''
#删除前调用三次敌机函数
global
q
q
=
self
.
rect
#销毁增加分数
pass
class
Hero
(
GameSprite
):
'''英雄精灵'''
def
__init__
(
self
):
#1.调用父类方法加载图片
super
().
__init__
(
"./images/dog.png"
)
#2.设置初始速度
self
.
speed
=
0
#3.设置初始位置
self
.
rect
.
centerx
=
SCREEN_RECT
.
centerx
self
.
rect
.
bottom
=
SCREEN_RECT
.
bottom
-
120
self
.
bulltes
=
pygame
.
sprite
.
Group
()
self
.
speed1
=
0
self
.
bullet
=
None
def
update
(
self
):
self
.
rect
.
x
+=
self
.
speed
self
.
rect
.
y
+=
self
.
speed1
#控制英雄不能离开屏幕
if
self
.
rect
.
x
<=
0
:
self
.
rect
.
x
=
0
elif
self
.
rect
.
x
>=
378
:
self
.
rect
.
x
=
378
def
fire
(
self
):
#1.创建子弹精灵
'''print("发射子弹")'''
self
.
bullet
=
Bullet
()
#2.设置精灵的位置
self
.
bullet
.
rect
.
bottom
=
self
.
rect
.
y
-
20
self
.
bullet
.
rect
.
centerx
=
self
.
rect
.
centerx
self
.
bulltes
.
add
(
self
.
bullet
)
#3.将精灵添加到精灵组
#创建子弹类
class
Bullet
(
GameSprite
):
def
__init__
(
self
):
super
().
__init__
(
"./images/bullet1.png"
,
-
2
)
def
update
(
self
):
super
().
update
()
#判断子弹是否飞出屏幕
if
self
.
rect
.
bottom
<
0
:
self
.
kill
()
def
__del__
(
self
):
'''print("子弹被销毁")'''
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