使用python中的pygame实现飞机大战(三)
前言:
接上篇
碰撞检测与游戏音效
5.碰撞检测
(1)碰撞测试
#测试碰撞
for enermy in self.enemiesgroup.sprites():
enermy.speed = 0
enermy.rect.y += 400
self.zhanji_sprite.speed =10
def check_collide(self):
"""检查是否碰撞"""
if not self.zhanji_sprite.is_power: # 非无敌检测碰撞
collide_enermies = py.sprite.spritecollide(self.zhanji_sprite,
self.enemiesgroup,False,py.sprite.collide_mask)
# 摧毁敌机和战机
if collide_enermies:
self.zhanji_sprite.hp = 0
for enermy in collide_enermies:
enermy.hp = 0
(2)玩家飞机重置后无敌2S
"""module.py"""
def reset_plane(self):
"""玩家飞机重置"""
super(Hero, self).reset_plane()
self.is_power = True
self.bomb_count = HERO_BOMB_COUNT
self.bullets_kind = 0
# 发布事件 游戏主逻辑更新界面
py.event.post(py.event.Event(HERO_DEAD_EVENT))
# 无敌事件定时
py.time.set_timer(py.event.Event(HERO_POWER_OFF_EVENT),4000)
"""game.py"""
def event_handler(self):
for event in py.event.get():
self.panel.save_bestscore()
if event.type == py.QUIT:#窗口叉叉退出
return True
elif event.type == py.KEYDOWN and event.key == py.K_ESCAPE: #ESC退出
return True
elif event.type == py.KEYDOWN and event.key == py.K_SPACE: #空格暂停与游戏重新开始
if self.over:
self.reset()
else:
self.pause = not self.pause
if not self.pause and not self.over:
# 游戏期间释放炸弹
if event.type == py.KEYDOWN and event.key == py.K_b:
score = self.zhanji_sprite.blowup(self.enemiesgroup)
self.panel.revise_bombs(self.zhanji_sprite.bomb_count)
if self.panel.increase_score(score):
self.create_enermies()
elif event.type == HERO_DEAD_EVENT:
# 飞机销毁之后恢复在原位置
self.panel.lives -= 1
self.panel.revise_lives()
self.panel.revise_bombs(self.zhanji_sprite.bomb_count)
elif event.type == HERO_POWER_OFF_EVENT:
# 无敌时间结束
self.zhanji_sprite.is_power = False
py.time.set_timer(HERO_POWER_OFF_EVENT,0) # 延时为零取消定时器
return False
效果:
(3)子弹类与飞机发射子弹
"""module.py """
class Bullet(GameSprite):
"""子弹类"""
def __init__(self,kind,*group):
# 子弹类型
image_name = 'bullet1.png' if kind == 0 else 'bullet2.png'
super(Bullet, self).__init__(image_name,-12,*group)
self.damage = 1 # 伤害
self.image = py.image.load(self.path+image_name)
def update(self, *args, **kwargs):
super(Bullet, self).update(*args) # 子弹位置
# 屏幕外销毁子弹
if self.rect.bottom < 0:
self.kill()
"""战机类中定义战机发射子弹"""
def fire(self,display_group):
"""战机发射子弹"""
groups = (display_group,self.bullets_group)
for i in range(3): # 创建子弹
bullet1 = Bullet(self.bullets_kind,*groups)
y = self.rect.y - i*15
if self.bullets_kind == 0:
bullet1.rect.midbottom = (self.rect.centerx,y)
else:
bullet1.rect.midbottom = (self.rect.centerx - 20,y)
bullet2 = Bullet(self.bullets_kind,*groups)
bullet2.rect.midbottom = (self.rect.centerx + 20,y)
效果:
(3)子弹类与飞机的碰撞
def check_collide(self):
"""检查是否碰撞"""
if not self.zhanji_sprite.is_power: # 非无敌检测碰撞
# 战机与敌群
collide_enermies = py.sprite.spritecollide(self.zhanji_sprite,
self.enemiesgroup,False,py.sprite.collide_mask)
collide_enermies = list(filter(lambda x: x.hp>0,collide_enermies)) # 过滤掉已炸毁飞机
# 摧毁战机
if collide_enermies:
self.zhanji_sprite.hp = 0
# 摧毁敌机
for enermy in collide_enermies:
enermy.hp = 0
hit_enermies = py.sprite.groupcollide(self.enemiesgroup,self.zhanji_sprite.bullets_group,
False,False,py.sprite.collide_mask)
for enermy in hit_enermies:
# 多颗子弹摧毁一敌机
if enermy.hp < 0:
continue
for bullet in hit_enermies[enermy]:
bullet.kill()
enermy.hp -= bullet.damage
if enermy.hp > 0:
continue
if self.panel.increase_score(enermy.value):
self.create_enermies()
break
效果:
(4)游戏重置
def reset(self):#游戏重置
self.pause = False
self.over = False
self.panel.panel_reset()
self.zhanji_sprite.rect.midbottom = HERO_DEFAULT_BOTTOM # 位置
for i in self.enemiesgroup: # 移除飞机
i.kill()
for i in self.zhanji_sprite.bullets_group: # 移除子弹
i.kill()
self.create_enermies()
效果:
6.游戏音效
(1)背景音乐音效
'''背景音乐'''
import pygame as py
class PlayMusic(object):
res_path = './static/sound/'
def __init__(self,music_path):
py.mixer.music.load(self.res_path + music_path)
py.mixer.music.set_volume(0.2)
@staticmethod
def playmusic():
py.mixer.music.play(-1)
@staticmethod
def pausemusic(is_pause):
if is_pause:
py.mixer.music.pause()
else:
py.mixer.music.unpause()
(2)细节爆炸音效
# 配乐
import pygame as py
import os
class PlayMusic(object):
res_path = './static/sound/'
def __init__(self,music_path):
py.mixer.music.load(self.res_path + music_path)
py.mixer.music.set_volume(0.2)
self.sound_dict = {}
files = os.listdir(self.res_path)
for i in files:
if i == music_path: # 背景音乐
continue
sound = py.mixer.Sound(self.res_path + i)
self.sound_dict[i] = sound
@staticmethod
def playmusic():
py.mixer.music.play(-1)
@staticmethod
def pausemusic(is_pause):
if is_pause:
py.mixer.music.pause()
else:
py.mixer.music.unpause()
def play_sound(self,wav_name):
self.sound_dict[wav_name].play()
在需要的地方调用声音,完成声音的匹配。
小结:
对pygame模块的理解更深一层。