Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pygame. Sprite is still drawing after killing itself

I want to remove the sprite and not display it on screen after click. The screenshot show that the sprite is successfully removed from the group, but it is still drawn on the screen. I would be happy to get help on this matter.

import pygame as pg


class Figure1(pg.sprite.Sprite):
    def __init__(self, width: int, height: int):
        super().__init__()

        self.image = pg.Surface((width, height))
        self.image.fill((0,0,0))
        self.rect = self.image.get_rect()


class Game:
    def __init__(self, main_surface: pg.Surface):
        self.main_surface = main_surface
        self.group = pg.sprite.Group()
        self.main_sprite = Figure1(40,40)
        self.group.add(self.main_sprite)
        self.group.draw(self.main_surface)
        self.selected = None

    def btn_down(self, pos, btn):
        if btn == 1:
            if self.main_sprite.rect.collidepoint(pos):

                print(self.group.sprites())
                print(self.main_sprite.alive())
                self.main_sprite.kill()

                print(self.group.sprites())
                print(self.main_sprite.alive())
                self.group.draw(self.main_surface)



pg.init()
clock = pg.time.Clock()
screen = pg.display.set_mode((200,200))
screen.fill((100,100,100))
pg.display.update()

g = Game(screen)
run = True

while run:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            run = False
        if event.type == pg.MOUSEBUTTONDOWN:
            g.btn_down(event.pos, event.button)

    clock.tick(60)

    pg.display.update()

1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The sprite doesn’t disappear just because you stop drawing it. Of course, you need to clear the screen. You have to clear the screen in every frame. The typical PyGame application loop has to:

import pygame as pg

class Figure1(pg.sprite.Sprite):
    def __init__(self, width: int, height: int):
        super().__init__()
        self.image = pg.Surface((width, height))
        self.image.fill((0,0,0))
        self.rect = self.image.get_rect()

class Game:
    def __init__(self, main_surface: pg.Surface):
        self.main_surface = main_surface
        self.group = pg.sprite.Group()
        self.main_sprite = Figure1(40,40)
        self.group.add(self.main_sprite)
        self.selected = None

    def btn_down(self, pos, btn):
        if btn == 1:
            if self.main_sprite.rect.collidepoint(pos):
                self.main_sprite.kill()

    def draw(self):
        self.group.draw(self.main_surface)

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

g = Game(screen)
run = True
while run:

    # limit the frames per second
    clock.tick(60)

    # handle the events and update objects
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False
        if event.type == pg.MOUSEBUTTONDOWN:
            g.btn_down(event.pos, event.button)

    # clear the screen
    screen.fill((100,100,100))

    # draw the objects
    g.draw()

    # update the display 
    pg.display.update()

pg.quit()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading