comparison pytouhou/game/game.py @ 152:86807b8a63bd

Add collisions with enemies and items.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 09 Oct 2011 15:32:43 -0700
parents 5cf927cbd9c5
children 37df8c618c2e
comparison
equal deleted inserted replaced
151:5cf927cbd9c5 152:86807b8a63bd
68 # 1. VMs. 68 # 1. VMs.
69 self.ecl_runner.run_iter() 69 self.ecl_runner.run_iter()
70 70
71 # 2. Filter out destroyed enemies 71 # 2. Filter out destroyed enemies
72 self.enemies = [enemy for enemy in self.enemies if not enemy._removed] 72 self.enemies = [enemy for enemy in self.enemies if not enemy._removed]
73 self.bullets = [bullet for bullet in self.bullets if not bullet._removed]
74 self.items = [item for item in self.items if not item._removed]
73 75
74 # 3. Let's play! 76 # 3. Let's play!
75 #TODO: check update orders 77 #TODO: check update orders
76 for player in self.players: 78 for player in self.players:
77 player.update(keystate) #TODO: differentiate keystates (multiplayer mode) 79 player.update(keystate) #TODO: differentiate keystates (multiplayer mode)
98 for player in self.players: 100 for player in self.players:
99 px, py = player.x, player.y 101 px, py = player.x, player.y
100 phalf_size = player.hitbox_half_size 102 phalf_size = player.hitbox_half_size
101 px1, px2 = px - phalf_size, px + phalf_size 103 px1, px2 = px - phalf_size, px + phalf_size
102 py1, py2 = py - phalf_size, py + phalf_size 104 py1, py2 = py - phalf_size, py + phalf_size
105
103 for bullet in self.bullets: 106 for bullet in self.bullets:
104 half_size = bullet.hitbox_half_size 107 half_size = bullet.hitbox_half_size
105 bx, by = bullet.x, bullet.y 108 bx, by = bullet.x, bullet.y
106 bx1, bx2 = bx - half_size, bx + half_size 109 bx1, bx2 = bx - half_size, bx + half_size
107 by1, by2 = by - half_size, by + half_size 110 by1, by2 = by - half_size, by + half_size
108 111
109 if not (bx2 < px1 or bx1 > px2 112 if not (bx2 < px1 or bx1 > px2
110 or by2 < py1 or by1 > py2): 113 or by2 < py1 or by1 > py2):
111 print('collided!') #TODO 114 bullet.collide(player)
112 115
113 #TODO: enemy-player collision 116 for enemy in self.enemies:
114 #TODO: item-player collision 117 half_size_x, half_size_y = enemy.hitbox_half_size
118 bx, by = enemy.x, enemy.y
119 bx1, bx2 = bx - half_size_x, bx + half_size_x
120 by1, by2 = by - half_size_y, by + half_size_y
121
122 if not (bx2 < px1 or bx1 > px2
123 or by2 < py1 or by1 > py2):
124 enemy.collide(player)
125
126 for item in self.items:
127 half_size = item.hitbox_half_size
128 bx, by = item.x, item.y
129 bx1, bx2 = bx - half_size, bx + half_size
130 by1, by2 = by - half_size, by + half_size
131
132 if not (bx2 < px1 or bx1 > px2
133 or by2 < py1 or by1 > py2):
134 item.collect(player)
115 135
116 # 5. Cleaning 136 # 5. Cleaning
117 self.cleanup() 137 self.cleanup()
118 138
119 self.frame += 1 139 self.frame += 1