简体   繁体   中英

Making pygame sprites disappear in python

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

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.

A great way to go about it is making all your visible objects inheritpygame.sprite.Sprite and put them in sprite groups . From here you could draw, update and remove sprites easily.

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

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()

If the button is truly a sprite, you can:

  • Add the sprite to a group called Buttons.
  • Render the Buttons on the screen.
  • Use the kill() method to remove the sprite from the Buttons group.
  • Re-render the screen on the next round.

http://pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite.kill

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM