Search code examples
pygamesprite

Questions with creating sprites in pygame


So basically what I know how to do is add a player sprite (make a player class that inherits from pygame.sprite, etc...) this works for me.

What I would like to know how to do is iterate the creation of sprites and add them to the sprite group. This is because I have a 2 dimensional array and I have a function that reads this and places the "tiles" accordingly in the 2d space, this is to create levels easier. So what I want this function to do is create these sprites (I guess with a for loop that reads the array ?) and add them to do the group but this doesn't work so I have some questions first:

1)Can you create sprites outside of the init function in a class?

2)What really are sprites, is it a surface coupled to a rect ?

3)And finally do you have an idea of simply how to get this done: If I give you a 2d array, how would you make the function that reads this array and calculates the position (this is okay, I think I have it figured out) and most importantly, make sprites out of the given positions that can then be added to the sprites group.

Thanks in advance


Solution

  • Can you create sprites outside of the init function in a class?

    Sure.

    What really are sprites, is it a surface coupled to a rect ?

    If we talk about pygame's Sprite class: yes.

    Such a sprite is basically a Surface (the image attribute) and a Rect (the rect attribute). They work best together with pygame's Group classes.

    And finally do you have an idea of simply how to get this done ....

    Just create a nested loop to iterate over the array.

    Here's a simple example:

    import pygame
    pygame.init()
    
    TILESIZE = 64
    
    class Actor(pygame.sprite.Sprite):
        def __init__(self, color, pos):
            super().__init__()
            self.image = pygame.Surface((TILESIZE, TILESIZE))
            self.image.fill(pygame.Color(color))
            self.rect = self.image.get_rect(topleft = pos)
    
    def main():
    
        data = [
            '     YUB  ',
            '  G       ',
            '        B ',
            '          ',
            '   Y      ',
            '        U ',
        ]
    
        screen = pygame.display.set_mode((len(data[0]) * TILESIZE, len(data) * TILESIZE))
        sprites = pygame.sprite.Group()
    
        colors = {
            'G': 'green',
            'B': 'black',
            'Y': 'yellow',
            'U': 'dodgerblue'
        }
    
        x, y = 0, 0
        for line in data:
            for char in line:
                if char in colors:
                    sprites.add(Actor(colors[char], (x * TILESIZE, y * TILESIZE)))
                x += 1
            x = 0
            y += 1
    
        while True:
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    pygame.quit()
                    return
    
            sprites.update()
    
            screen.fill((100, 100, 100))
            sprites.draw(screen)
    
            pygame.display.flip()
    
    main()
    

    enter image description here

    You can find another example here.