[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] spritecollide() right way to check if not player ?



I have a couple sprite / pygame questions:

1) Is there a better way to say ignore player ( who is in the group )
but check for collision on the rest? ) Meaning when I iterate the unit
list, One time the current unit is the player himself.

It might seem easier in this case to remove player from sprites, but
what about when you do enemies on enemies collision? In that case they
cannot be removed.

	class Unit(pygame.sprite.Sprite):
		"""basic Sprite() with sound on .kill"""
		# ...
		def die(self):		
			self.kill()
			play_sound('kaboom')
			
	class Game():
		def __init__(self):
			# group of all drawn sprites.
			self.sprites = pygame.sprite.RenderPlain()		
			
			self.player = Unit()
			self.sprites.add(self.player)

		def main_loop(self)
			for s in pygame.sprite.spritecollide(self.player, self.sprites, False):
				if s == self.player: continue
				s.die()


2) I am looping enemies collision with anything, and I act differently
on items,health, or enemies.
What is the best way to check type? Something like:

	# collide: enemies on anything
	for e in self.enemies:
		for s in pygame.sprite.spritecollide(e, self.sprites, False):
			e.oncollide(s)
			s.oncollide(e)

	# and the function:
	def oncollide(self, other):		
		if isinstance(other, Enemy): self.damage(other.amount)
		elif isinstance(other, Health): self.heal()
		elif isinstance(other, Ammo): self.ammo += 10


3) Is it better to separate my collision checks? Assuming I am doing
the same amount of total collision checks:
[ Or no point in worrying about this, ie: either I need QuadTree or I don't ? ]

	for u in units:
		for s in spritecollide(u, units)
			if isinstance(s, Enemy): #...
			elif isinstance(s, Item): #...
		
VS

	for s in spritecollide(player, enemies):
		# ...
	for s in spritecollide(player, items):
		# ...
-- 
Jake