khieminator
khieminator

Reputation: 13

pygame.sprite.Group.draw doesn't draw image

I'm working on a game with pygame and I'm handling the sprites for the game character with the pygame classes: pygame.sprite.Sprite() and pygame.sprite.Group(). I got my Sprite Class and I want to draw the Sprites with pygame.sprite.Group.draw . But the image doesn't show up. If I blit it normally, it works but if I want to draw it with Group.draw it doesn't.

Here is my Main:

def main():
    pygame.init()
    WIN = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    pygame.display.set_caption("Ultra Super Street Pyther")

    clock = pygame.time.Clock()
    run = True
    index = 0

    # set up Sprite Group
    p1_sprites = NormalGuy("Idle")
    p1 = pygame.sprite.Group(p1_sprites)

    while run:

        # FPS of gameloop
        clock.tick(FPS)

        # check events during game loop
        for event in pygame.event.get():
            # check for quit pygame
            if event.type == pygame.QUIT:
                run = False

        # draw sprites
        p1.update()
        p1.draw(WIN)

        # draw window
        WIN.fill(WHITE)
        pygame.display.update()

    pygame.quit()

Here is my Sprite Class:

class NormalGuy(pygame.sprite.Sprite):
    def __init__(self,action="Air", lo=(0,0)):
            super(NormalGuy, self).__init__()

            # parameters for getting sprites from spritesheet
            self.spriteConfigs = get_config("Super_Strong_Normal_Guy.json")
            self.spriteConfigs = self.spriteConfigs["NormalGuy"][action]
            self.path = self.spriteConfigs["path"]
            self.width = self.spriteConfigs["width"]
            self.height = self.spriteConfigs["height"]
            self.sheet_specs = self.spriteConfigs["sheet_specs"]
            self.scale = self.spriteConfigs["scale"]

            # getting sprites from spritesheet
            self.spriteSheet = Spritesheet(self.path)
            self.images = self.spriteSheet.get_sprites(self.width, self.height, self.sheet_specs, self.scale)

            # essentials for Sprite class
            self.image = self.images[0]
            self.rect = self.image.get_rect()
            self.rect.topleft = lo
            self.index = 0

    def update(self):
        if self.index >= len(self.images):
            self.index = 0
        self.image = self.images[self.index]
        self.index += 1

Upvotes: 1

Views: 630

Answers (1)

Rabbid76
Rabbid76

Reputation: 211135

Yo have to change the order of the instruction. Clear the background before drwing the image:

def main():
    # [...]

    while run:

        # FPS of gameloop
        clock.tick(FPS)

        # check events during game loop
        for event in pygame.event.get():
            # check for quit pygame
            if event.type == pygame.QUIT:
                run = False

        p1.update()
       
        WIN.fill(WHITE) <-- INSERT

        p1.draw(WIN)

        # WIN.fill(WHITE) <-- DELETE

        pygame.display.update()

Upvotes: 1

Related Questions