Reputation: 33
I'm currently creating a game in python/ pydev!
My problem is: I have the user sprite that can move around the screen. When he collides with another sprite, I then want that other sprite to disappear and not to be used again, however as it stands now when I collide with the object i want to pick up nothing happens. What code do I need to have to do it? I think I put it in the def update but cant be sure.. Below is the class for the object I want to pick up!!
class CV(pygame.sprite.Sprite):
def __init__(self, screen, (posX, posY)):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("cv.png")
self.image = self.image.convert()
transColor = self.image.get_at((1,1))
self.image.set_colorkey(transColor)
self.rect = self.image.get_rect()
Upvotes: 2
Views: 192
Reputation: 11
You should look up the pygame documentation for the function spriteCollide. It allows you to check if two designated sprites are colliding, and specify behavior for each upon collision. It also has a handy built in 'doKill' argument, where you can specify True if you want a sprite object deleted upon collision, or False if not.
If you're working with multiple sprites, I would try groupCollide, it works largely the same except it returns a dictionary of collision.
Hope that helps!
Upvotes: 1