pygame sprite group not moving

Viewed 63

i want to make an advanced animation class but pygame's documentation is a bit confusing for me

I wrote a class that dynamically loads and updates images in a subdirectory

class LoadSprite():
def __init__(self, path, rect):
    self.path = path
    self.rect = rect
    self.index = 0

def load_images(self):
    extend =[]
    images = [pygame.image.load(self.path) for self.path in glob.glob(self.path+"\\*.png")]//load images
    for tile in images:
        tile = pygame.transform.scale(tile, (self.rect.w, self.rect.h))//resized images
        tile.set_colorkey((0,0,0))
        rect = tile.get_rect()
        extend.append(MySprite(tile,rect))//add Sprite object
    return extend

def update(self):
    group = self.load_images() **call load_images function**
    if self.index >= len(group):
        self.index = 0
    self.image = group[self.index]
    self.index += 1


class MySprite(pygame.sprite.Sprite):#Sprite class
  def __init__(self,image,rect):
    super(MySprite, self).__init__()
    self.image = image
    self.rect = rect

and finaly

while True:
   sprite = LoadSprite('Tree',pygame.Rect(100,100,200,200))
   group = pygame.sprite.Group(sprite.load_images())
   group.update()
   group.draw(screen)

I didn't get an error but I could only draw the first image on the screen Am I missing something in the update function or is there some other error in my code?

Thank you for your interest.

1 Answers

You recreate the Group and the Sprites in every frame. You must create the objects once before the application loop:

sprite = LoadSprite('Tree',pygame.Rect(100,100,200,200))
group = pygame.sprite.Group(sprite.load_images())

while True:

   group.update()
   group.draw(screen)
Related