Making pygame sprites disappear in python
<p>I am working on a rpg game and I want my playbutton to disappear as soon as it was pressed. Is there a method of which I can do that?
I have different game states they are: GAME, MENU, START
The playbutton will appear on the START game state and I want it to disappear when it is pressed or when the game state changes.
Thank you for your contribution</p>

<p>To remove something from the screen you need to draw something else over it. So the most basic answer would be to just stop rendering the button and start render other stuff over it.</p>

<p>A great way to go about it is making all your visible objects inherit <a href="http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite" rel="nofollow">pygame.sprite.Sprite</a> and put them in <a href="http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group" rel="nofollow">sprite groups</a>. From here you could draw, update and remove sprites easily. </p>

<p>Here's a working example. Press the keys 1, 2 or 3 to make the "Buttons" reappear again:</p>

<pre><code>import pygame
pygame.init()

screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()


class Button(pygame.sprite.Sprite):
    def __init__(self, pos, size=(32, 32), image=None):
        super(Button, self).__init__()
        if image is None:
            self.rect = pygame.Rect(pos, size)
            self.image = pygame.Surface(size)
        else:
            self.image = image
            self.rect = image.get_rect(topleft=pos)
        self.pressed = False

    def update(self):
        mouse_pos = pygame.mouse.get_pos()
        mouse_clicked = pygame.mouse.get_pressed()[0]
        if self.rect.collidepoint(*mouse_pos) and mouse_clicked:
            print("BUTTON PRESSED!")
            self.kill()  # Will remove itself from all pygame groups.

image = pygame.Surface((100, 40))
image.fill((255, 0, 0))
buttons = pygame.sprite.Group()
buttons.add(
    Button(pos=(50, 25), image=image),
    Button(pos=(50, 75), image=image),
    Button(pos=(50, 125), image=image)
)

while True:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
                buttons.add(Button(pos=(50, 25), image=image))
            elif event.key == pygame.K_2:
                buttons.add(Button(pos=(50, 75), image=image))
            elif event.key == pygame.K_3:
                buttons.add(Button(pos=(50, 125), image=image))

    buttons.update()  # Calls the update method on every sprite in the group.

    screen.fill((0, 0, 0))
    buttons.draw(screen)  # Draws all sprites to the given Surface.
    pygame.display.update()
</code></pre>

python pygame