0

I am currently trying to make a game based on the 8-Bit Sonic the Hedgehog games, and I need to spawn multiple of one kind of sprite. This would be fine if it weren't for the size of the levels, meaning that if I want a decent amount of anything I would need to create hundreds of variables to store the sprite. Is there any way around this? Here is one of the sprites:

class ring:
    def __init__(self, x, y, frame):
        self.image = [pygame.image.load('assets/ring/ring1'), pygame.image.load('assets/ring/ring2.png'),
                      pygame.image.load('assets/ring/ring3'), pygame.image.load('assets/ring/ring4.png'),
                      pygame.image.load('assets/ring/ring5'), pygame.image.load('assets/ring/ring6.png')]
        self.x = x
        self.y = y
        self.rect = pygame.Rect(x, y, 14, 16)
        self.frame = frame

    def render(self):
        screen.blit(self.image[self.frame])
        pygame.draw.rect(screen, red, self.rect)

Game engines and websites like Scratch allow you to "clone" a sprite, is there something similar here?

Rabbid76
  • 177,135
  • 25
  • 101
  • 146
Aqualuzara
  • 47
  • 4
  • Use a list. `enemies = []`. But this is not a pygame quedtion. This is just a question about how to manage objects in python – Rabbid76 Jan 24 '21 at 11:56
  • In `pygame` you have `sprite.Group`, which is an efficient container, read the [docs](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group) for usage. – Thymen Jan 24 '21 at 12:37
  • @Thymen Yes indeed, but he has no `pygame.sprite.Sprite`. – Rabbid76 Jan 24 '21 at 12:49
  • Why do you want to "clone" a sprite. Just create multiple [Instance Objects](https://docs.python.org/3/tutorial/classes.html#instance-objects). Most likely, you will need to learn Python before you can proceed. – Rabbid76 Jan 24 '21 at 12:51
  • I think I am misunderstanding the question, but if you are asking for a way to manage a large group of sprite like objects. Generally you would make a (real) `pygame.sprite.Sprite` object, such that `pygame` can handle the object groups for you. If you want to do it manually, you will have to build something similar, for example using a `list` (as suggested by Rabbid76). Then you can iterate over the list and do whatever checks you want to do manually. Do notice that `pygame` has already built in a lot, such as sprite collision checks, and efficiently. Reinventing the wheel is not always good. – Thymen Jan 24 '21 at 13:14

0 Answers0