-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpyspaceinvaders_collision.py
39 lines (34 loc) · 1.56 KB
/
pyspaceinvaders_collision.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# $LastChangedDate: 2009-08-19 15:06:10 -0500 (Wed, 19 Aug 2009) $
# Collision-detection functions.
# Author: Jim Brooks http://www.jimbrooks.org
# Date: initial 2004/08, rewritten 2009/08
# License: GNU General Public License Version 2 (GPL2).
#===============================================================================
from pyspaceinvaders_exception import *
#-------------------------------------------------------------------------------
# Collision-detection functions.
#-------------------------------------------------------------------------------
def Collided( obj1, obj2 ):
""" Return true if one object has collided into another. """
if obj1.valid and obj2.valid:
return obj1.rect.colliderect( obj2.rect )
else:
return False
def MissileMissileCollision( missile, missileIdx, oppMissileList ):
""" Missile/missile collisions. """
""" Detects if one missile has collided into any opposite missiles (plural). """
if missile.valid:
oppMissileIdx = -1
for oppMissile in oppMissileList:
oppMissileIdx += 1
if oppMissile.valid and Collided( missile, oppMissile ):
missile.valid = False
oppMissile.valid = False
def PlayerAlienCollision( player, alienList ):
""" Has any alien collided into the player? """
for alien in alienList:
# Ignore further collisions while both are exploding.
if alien.valid and Collided( alien, player ):
if (alien.hit <= 0) and (player.hit <= 0):
alien.Hit()
player.Hit()