comparison skaapsteker/physics.py @ 416:3db2fc263d11

Kill sprites that leave the level area.
author Simon Cross <hodgestar@gmail.com>
date Sat, 09 Apr 2011 17:42:20 +0200
parents 55e00f186c6f
children 7c0643e51f33
comparison
equal deleted inserted replaced
415:cfbf1bf9a54c 416:3db2fc263d11
166 166
167 self.image = old_image 167 self.image = old_image
168 self.rect = old_rect 168 self.rect = old_rect
169 return not bool(new_collisions - old_collisions) 169 return not bool(new_collisions - old_collisions)
170 170
171 def fix_bounds(self):
172 print "Killing", self, self.rect, self._float_pos
173 self.kill()
174
171 175
172 class World(object): 176 class World(object):
173 177
174 GRAVITY = 0.0, 9.8 * 80.0 # pixels / s^2 178 GRAVITY = 0.0, 9.8 * 80.0 # pixels / s^2
175 179
176 def __init__(self): 180 def __init__(self, bounds):
177 self._all = pygame.sprite.LayeredUpdates() 181 self._all = pygame.sprite.LayeredUpdates()
178 self._mobiles = pygame.sprite.Group() 182 self._mobiles = pygame.sprite.Group()
179 self._gravitators = pygame.sprite.Group() 183 self._gravitators = pygame.sprite.Group()
180 self._updaters = pygame.sprite.Group() 184 self._updaters = pygame.sprite.Group()
181 self._actionables = pygame.sprite.Group() 185 self._actionables = pygame.sprite.Group()
182 self._actors = pygame.sprite.Group() 186 self._actors = pygame.sprite.Group()
183 self._collision_groups = { None: pygame.sprite.Group() } 187 self._collision_groups = { None: pygame.sprite.Group() }
184 self._last_time = None 188 self._last_time = None
189 self._bounds = bounds
185 190
186 def freeze(self): 191 def freeze(self):
187 self._last_time = None 192 self._last_time = None
188 193
189 def thaw(self): 194 def thaw(self):
261 sprite.deltav(dv) 266 sprite.deltav(dv)
262 267
263 # friction 268 # friction
264 for sprite in self._mobiles: 269 for sprite in self._mobiles:
265 sprite.apply_friction() 270 sprite.apply_friction()
271
272 # kill sprites outside the world
273 inbound = self._bounds.colliderect
274 for sprite in self._mobiles:
275 if not inbound(sprite):
276 sprite.fix_bounds()
266 277
267 # position update and collision check (do last) 278 # position update and collision check (do last)
268 for sprite in self._mobiles: 279 for sprite in self._mobiles:
269 sprite.deltap(dt) 280 sprite.deltap(dt)
270 sprite_collides = sprite.collide_rect.colliderect 281 sprite_collides = sprite.collide_rect.colliderect