# HG changeset patch # User Thibaut Girka # Date 1316976974 -7200 # Node ID c7f0fd9d2145d4999489ec7cdb1304293a97d742 # Parent 982b212226028fe00a6a4e2a2f159c98a022e4c3 Simple collision detection diff --git a/pytouhou/game/bullet.py b/pytouhou/game/bullet.py --- a/pytouhou/game/bullet.py +++ b/pytouhou/game/bullet.py @@ -29,6 +29,8 @@ class Bullet(object): self._launched = False self._bullet_type = bullet_type + self.hitbox_half_size = bullet_type.hitbox_size / 2. + self.speed_interpolator = None self.frame = 0 self.grazed = False diff --git a/pytouhou/game/game.py b/pytouhou/game/game.py --- a/pytouhou/game/game.py +++ b/pytouhou/game/game.py @@ -75,8 +75,9 @@ class Game(object): self.enemies = [enemy for enemy in self.enemies if not enemy._removed] # 3. Let's play! + #TODO: check update orders for player in self.players: - player.update(keystate) #TODO: differentiate keystates + player.update(keystate) #TODO: differentiate keystates (multiplayer mode) if player.state.x < 8.: player.state.x = 8. if player.state.x > 384.-8: #TODO @@ -92,7 +93,24 @@ class Game(object): for bullet in self.game_state.bullets: bullet.update() - # 4. Cleaning + # 4. Check for collisions! + #TODO + for player in self.players: + px, py = player.x, player.y + phalf_size = player.hitbox_half_size + px1, px2 = px - phalf_size, px + phalf_size + py1, py2 = py - phalf_size, py + phalf_size + for bullet in self.game_state.bullets: + half_size = bullet.hitbox_half_size + bx, by = bullet.x, bullet.y + bx1, bx2 = bx - half_size, bx + half_size + by1, by2 = by - half_size, by + half_size + + if not (bx2 < px1 or bx1 > px2 + or by2 < py1 or by1 > py2): + print('collided!') #TODO + + # 5. Cleaning self.cleanup() self.game_state.frame += 1 diff --git a/pytouhou/game/player.py b/pytouhou/game/player.py --- a/pytouhou/game/player.py +++ b/pytouhou/game/player.py @@ -43,6 +43,8 @@ class Player(object): self._sprite = None self._anmrunner = None + self.hitbox_half_size = character.hitbox_size / 2. + self.state = state self.character = character self.anm_wrapper = character.anm_wrapper