What line of code do I use to erase all the Sprites on my screen?

Viewed 35

I am programming an Art app and I've came to a block which is How to erase all of the sprites on the screen. now I have searched on videos which directed me to this function: kill(). I have tried kill() in my code and has led me to a crash either to my window or to sensing whether I clicked a button on the screen.
this is my line of code

   import pygame as pg
   all_sprites = pg.sprite.Group()
   pg.init()
   win = pg.display.set_mode((600, 600))
   icon = pg.image.load('paint icon')
   pg.display.set_icon(icon)
   black = (0,0,0)
   grey = (128,128,128)
   white = (255,255,255)
   hpink = (255, 0, 64)
   lpink = (255, 192, 255)
   pink = (255, 128, 255)
   fuchsia = (255, 0, 128)
   red = (255,0,0)
   orange = (255,128,0)
   brown = (192,100,0)
   lbrown = (200,108,0)
   yellow = (255,192,0)
   green = (0,255,0)
   lblue = (0,128,255)
   blue = (0,0,255)
   dblue = (0,0,128)
   purple = (128,0,128)
   click = 0
   win.fill(white)
   w = 10
   h = 10

   class Ink(pg.sprite.Sprite):
       def __init__(self):
           pg.sprite.Sprite.__init__(self)
           super(Ink,self).__init__()
           self.image = pg.Surface([w,h])
           self.rect = self.image.get_rect()
           self.image.fill(black)

    class Drop(pg.sprite.Sprite):
        def __init__(self):
            super(Drop,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([180,300])
            self.rect = self.image.get_rect()
            self.image.fill(brown)
        def update(self):
            if exit.rect.collidepoint(pos):
                if pg.mouse.get_pressed()[0]:


    class Box(pg.sprite.Sprite):
        def __init__(self):
            super(Box,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([50,30])
            self.rect = self.image.get_rect()
            self.image.fill(green)


    class Exit(pg.sprite.Sprite):
        def __init__(self):
            super(Exit,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([40,20])
            self.rect = self.image.get_rect()
            self.image.fill(lbrown)



    class Red(pg.sprite.Sprite):
        def __init__(self):
            super(Red,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([20,20])
            self.rect = self.image.get_rect()
            self.image.fill(red)

    class Orange(pg.sprite.Sprite):
        def __init__(self):
            super(Orange,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([20,20])
            self.rect = self.image.get_rect()
            self.image.fill(orange)

    class Yellow(pg.sprite.Sprite):
        def __init__(self):
            super(Yellow,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([20,20])
            self.rect = self.image.get_rect()
            self.image.fill(yellow)

    class Green(pg.sprite.Sprite):
        def __init__(self):
            super(Green,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([20,20])
            self.rect = self.image.get_rect()
            self.image.fill(green)
    class Blue(pg.sprite.Sprite):
        def __init__(self):
            super(Blue,self).__init__()
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface([20,20])
            self.rect = self.image.get_rect()
            self.image.fill(blue)


    ink = Ink()
    ink.rect.x = 100
    ink.rect.y = 300
    drop = Drop()
    drop.rect.x = 390
    drop.rect.y = 133
    box = Box()
    box.rect.x = 420
    box.rect.y = 528
    exit = Exit()
    exit.rect.x = 530
    exit.rect.y = 138

    redc = Red()
    redc.rect.x = 400
    redc.rect.y = 600//3.5
    orangec = Orange()
    orangec.rect.x = 430
    orangec.rect.y = 600//3.5
    yellowc = Yellow()
    yellowc.rect.x = 460
    yellowc.rect.y = 600//3.5
    greenc = Green()
    greenc.rect.x = 490
    greenc.rect.y = 600//3.5
    bluec = Blue()
    bluec.rect.x = 520
    bluec.rect.y = 600//3.5
    exitc = Exit()

    def redraw():
        pg.display.update()
        all_sprites.draw(win)

    all_sprites.add(drop, box, exit)
    all_sprites.add(redc, orangec, yellowc, greenc, bluec)

    run = True

    while run:
        print(click)
        pos = pg.mouse.get_pos() ---> pos = mouse position
        print(pos) 
        for event in pg.event.get():
            key = event.type
            if key == pg.QUIT:
                run = False

        if redc.rect.collidepoint(pos):
            if pg.mouse.get_pressed()[0]:
                Ink.image.fill(red)

        if orangec.rect.collidepoint(pos):
            if pg.mouse.get_pressed()[0]:
               Ink.image.fill(orange)

        if yellowc.rect.collidepoint(pos):
            if pg.mouse.get_pressed()[0]:
                Ink.image.fill(yellow)

        if greenc.rect.collidepoint(pos):
            if pg.mouse.get_pressed()[0]:
                Ink.image.fill(green)

        if bluec.rect.collidepoint(pos):
            if pg.mouse.get_pressed()[0]:
                Ink.image.fill(blue)

        if box.rect.collidepoint(pos) and click == 0:
            if pg.mouse.get_pressed()[0]:
                all_sprites.add(ink)
                click = 1

        if box.rect.collidepoint(pos) and click == 1:
            if pg.mouse.get_pressed()[0]:
                all_sprites.remove(ink)
                click = 0

        if exit.rect.collidepoint(pos):
            exit.image.fill(red)

        else:
            exit.image.fill(lbrown)

        redraw() 
    pg.quit()

What is wrong with my code?

1 Answers

You have to clear the display before drawing the spits:

def redraw():
    # clear display
    win.fill(white)
        
    # draw all sprites
    all_sprites.draw(win)

    # update display
    pg.display.update()

Everything that is drawn is drawn on win. Therefore win needs to be cleared at the beginning of every frame and before drawing the sprites. win is Surface object that is associated to the PyGame display. The change becomes not immediately visible in the display. The changes become visible, when the display is updated with either pygame.display.update() or pygame.display.flip().

Related