Draw sprite from image in pygame, internal error

Viewed 51

I'm trying to draw a sprite from a png, the png is in the doomman folder. The error I'm getting is:

TypeError: add_internal() missing 1 required positional argument: 'group'

I'm new to drawing sprites in pygame. What's going on here?

Here's the code

import pygame
pygame.init()

screen = pygame.display.set_mode((640, 480))

BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255,0,0)

class Sprite(pygame.sprite.Sprite):
    def __init__(self, pos):
        # Don't forget to call the __init__ method of the parent class.
        super(Sprite, self).__init__()
        
        self.image = pygame.image.load(demon)
        

all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(Sprite)

demon = pygame.image.load("C:/programming/doomman/cacodemon.png").convert_alpha()

clock = pygame.time.Clock()
pygame.display.set_caption("Demon Dance")
carryOn = True
        
while carryOn == True:
    
    for event in pygame.event.get():
            if event.type==pygame.QUIT:
                carryOn=False
                
    all_sprites_list.update()
    
    pygame.display.flip()
    clock.tick(60)
    all_sprites_list.draw(screen)
 
1 Answers

pygame.sprite.Group manages Sptrite objects. You need to create a create an Instance Objects object from the Sprite class. Additionally the pygame.sprite.Sprite object needs to have a rect attribute. This attribute is requested from pygame.sprite.Group.draw when drawing the _Sprites in the Group:

import pygame
pygame.init()

screen = pygame.display.set_mode((640, 480))

BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255,0,0)

class Sprite(pygame.sprite.Sprite):
    def __init__(self, pos):
        super(Sprite, self).__init__()
        self.image = demon
        self.rect = self.image.get_rect(center = pos)
        
all_sprites_list = pygame.sprite.Group()

demon = pygame.image.load("C:/programming/doomman/cacodemon.png").convert_alpha()
my_sprite = Sprite((50, 50))
all_sprites_list.add(my_sprite)

clock = pygame.time.Clock()
pygame.display.set_caption("Demon Dance")
carryOn = True
        
while carryOn == True:
    
    for event in pygame.event.get():
            if event.type==pygame.QUIT:
                carryOn=False
                
    all_sprites_list.update()
    
    pygame.display.flip()
    clock.tick(60)
    all_sprites_list.draw(screen)
Related