Skip to content
Advertisement

How add different images for enemies in a python game?

I currently have 6 enemies spawning, but they are all the same image. This is the relevant code:

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load('enemy.png').convert_alpha())
    enemyX.append(random.randint(0, 1179))
    enemyY.append(random.randint(0, 150))
    enemyX_change.append(1.5)
    enemyY_change.append(40)

def enemy(X, Y, i):
    screen.blit(enemyImg[i], (X, Y))

I want to have a different enemy image for each of the 6 enemies but don’t know what to google.

Advertisement

Answer

Just create a list with 6 file names:

# [...]

num_of_enemies = 6
                            # the names are just for example
enemy_files = ['enemy.png', 'a.png', 'b.png', 'c.png', 'd.png', 'e.png'] 

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load(enemy_files[i]).convert_alpha())

    # [...]
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement