pygame中的sprite加上sprite.Group,进行批量的对象碰撞检测太简单了,不仅有多种的碰撞检测模式,还能以各种方式控制处理结果,今天先学习groupcollide的rect碰撞检测,明天再看看几个圆形碰撞,以及对象与group的碰撞。
def _CheckCollide(self):
# print('Check collide.')
# 英雄和敌机碰撞检测(不管双方血量,直接结束游戏)
collide = pg.sprite.groupcollide(self.gs_herolist, self.gs_enemylist, False, True)
if collide:
self._GamePause()
# 英雄和敌机子弹碰撞检测(判断英雄血量,少于1则结束游戏)
collide = pg.sprite.groupcollide(self.gs_enemybulletlist, self.gs_herolist, True, False)
if collide:
# 防止NoneType
try:
for _heros_ in collide.values():
for _hero_ in _heros_:
# 扣血1格,如果死亡则游戏暂停。
self._hero_life_count = _hero_.hit()
except Exception as e:
print('发生异常:', e)
# 英雄子弹和敌机碰撞检测(判断敌机血量,少于1则kill相应敌机)
collide = pg.sprite.groupcollide(self.gs_herobulletlist, self.gs_enemylist, T