[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] spritecollide() right way to check if not player ?
- To: pygame-users@xxxxxxxx
- Subject: [pygame] spritecollide() right way to check if not player ?
- From: Jake b <ninmonkeys@xxxxxxxxx>
- Date: Fri, 10 Jul 2009 02:32:05 -0500
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Fri, 10 Jul 2009 03:32:27 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:from:date:message-id :subject:to:content-type:content-transfer-encoding; bh=jmRRS7Hqi54CO75cVcDWKmgbTI6SSZ4eVVTezJ9vqzE=; b=CsmQldoN4VK0cPXrVKuaPShgNjBVAnxvv1cZj6aZ+sK+0n73DlN5Hms3M4Us1wGB9H 5SglCTy2VT7j/0mG717hE0c1UIO94SJO5EB3vb/KPzuO19P9slZC9+MKvTPSx4rX4ut8 /UTzqNjG/Hp2W7u4j31jWXXeHNEcsnLFnFe2o=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type :content-transfer-encoding; b=MgU5ViIFipFs4kwRt2sumA5zmNZuz1q6ockAG+bWNMourquKPIEygVbY+ko6+31wJb FCsnfrc+NEHntckAQcTRz3SHAt9eS5IufcKtHBZ9Am/mpE9Dqrujzb0dmclDlbut9jop OjjHlLBAQa+NkRr1ecyV0dKy3iTt9yYpekWWc=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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