0

I want to add at least 8 obstacles in my game (2d like flappy bird but with different obstacles), should I load them manually:

sprite1 = pygame.image.load('assets/sprites/obstacle1.tif').convert_alpha()
sprite2 = pygame.image.load('assets/sprites/obstacle2.tif').convert_alpha()
sprite3 = pygame.image.load('assets/sprites/obstacle3.tif').convert_alpha()
...

Or perhaps something like:

type = "Obstacle 1" //example
public class Obstacle {
    public Obstacle(String type,, int SizeX, int SizeY) {
    ObstacleX = 1920;//spawn outside of screen
    ObstacleType = type;
    ObstacleSizeX = SizeX;
    ObstacleSizeY = SizeY;
    ObstacleImg = loadImage(ObstacleType +  ".tif");
}

(not python but the idea is loading automatically sprites if they're on the database, checking every new frame the DB to see if there is an obstacle ready to spawn with the current map X )

PS: should I load them all in the begining of the game or load them weather they are currently passing the screen or not

IBeFrogs
  • 1
  • 2

1 Answers1

0

I load them as a list to easily loop over them when needed. Use of list comprehension and f-strings come in handy to shorten the code.

sprites = [pygame.image.load(f'assets/sprites/obstacle{i}.tif').convert_alpha() for i in range(1, 9)]
Jobo Fernandez
  • 905
  • 4
  • 13