How can i get the first element from a sprite group in pygame

Viewed 345

I create the sprite group land. For that, i use the class Landschaft and the while loop
How can i get from first sprite in the sprite group land rect.x , rect.y rect.w and rect.h ?

class Landschaft(pygame.sprite.Sprite):       
    def __init__(self,image):
        pygame.sprite.Sprite.__init__(self) 
        self.image = image 
        #self.mask = pygame.mask.from_surface(self.image )
        self.rect = self.image.get_rect()        
        #pygame.draw.ellipse(self.image, red, [self.rect.x,self.rect.y,self.rect.width,self.rect.height],3)        
        x=random.randrange(0, breite -60) 
        y=random.randrange(200, hoehe - 200) 
        self.rect.center = (x,y) 

land = pygame.sprite.Group()

while len(land) < anzahl_gegenstaende:
        ii = len(land)
        img = pygame.image.load(f"Bilder/Gegenstaende/geg{ii}.png") 
        if ii == 0:
            img = pygame.transform.scale(img,(breite)) 
        else:    
            zb,zh = img.get_rect().size
            scale_hoehe =100
            scale_breite = int(100 * zb / zh)          
            #img = img.convert_alpha() 
            img = pygame.transform.scale(img,(scale_breite,scale_hoehe))            
        m = Landschaft(img) 
      
        if not pygame.sprite.spritecollide(m, land, False):
            land.add(m)
2 Answers

A list of sprites from a pygame.sprite.Group can be obtained with the sprites() method:. Therefore the first sprite in the Group is:

first_sprite = group.sprites()[0]

Problem? Your land = pygame.sprite.Group() prevents you from getting the first element from a sprite group in pygame. This is a fairly common problem. To get it you have to use sprites() as you did, but in a different way. You approached the solution with your code. You came close.

You have to write, as a solution to your problem, group.sprites() (with [0]) (not pygame.sprite.Group ()) to get from first sprite in the sprite group land rect.x, rect.y rect.w and rect.h

Solution? The solution to your problem is:

class Landschaft(pygame.sprite.Sprite):       
    def __init__(self,image):
        pygame.sprite.Sprite.__init__(self) 
        self.image = image 
        #self.mask = pygame.mask.from_surface(self.image )
        self.rect = self.image.get_rect()        
        #pygame.draw.ellipse(self.image, red, [self.rect.x,self.rect.y,self.rect.width,self.rect.height],3)        
        x=random.randrange(0, breite -60) 
        y=random.randrange(200, hoehe - 200) 
        self.rect.center = (x,y) 

land = group.sprites()[0]

while len(land) < anzahl_gegenstaende:
        ii = len(land)
        img = pygame.image.load(f"Bilder/Gegenstaende/geg{ii}.png") 
        if ii == 0:
            img = pygame.transform.scale(img,(breite)) 
        else:    
            zb,zh = img.get_rect().size
            scale_hoehe =100
            scale_breite = int(100 * zb / zh)          
            #img = img.convert_alpha() 
            img = pygame.transform.scale(img,(scale_breite,scale_hoehe))            
        m = Landschaft(img) 
      
        if not pygame.sprite.spritecollide(m, land, False):
            land.add(m)
Related