AndreasSB02
AndreasSB02

Reputation: 125

Delete sprite added from class

This is the code for adding sprites from a class:

placed = pygame.sprite.Group()
placed.add(StraightPipe((-2,0)))

Straight pipe is the name of the class.

How do i remove the same sprite that i just added?

Upvotes: 1

Views: 41

Answers (2)

Rabbid76
Rabbid76

Reputation: 210968

You can get a list of Sprites by pygame.sprite.Group.sprites. Hence you can access the last element of the Group by subscription and kill it by pygame.sprite.Sprite.kill:

placed.sprites()[-1].kill()

Upvotes: 0

Thomas
Thomas

Reputation: 181745

You need to keep a reference to the sprite you just created, so that you can remove it again:

placed = pygame.sprite.Group()
straight_pipe = StraightPipe((-2,0))
placed.add(straight_pipe)

... later ...

placed.remove(straight_pipe)

I'm assuming that the remove call is in the same scope as the add call. If it's not, you might need to make straight_pipe global or add it as a class member, depending on the structure of the rest of your code.

Upvotes: 2

Related Questions