Home > OS >  How add different images for enemies in a python game?
How add different images for enemies in a python game?

Time:08-21

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.

CodePudding user response:

you should just need to do this and then name the images accordingly like so: enemy0.png, enemy1.png till enemy5.png


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

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load(f'enemy{i}.png').convert_alpha()) # different png number for every enemy
    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))

CodePudding user response:

This is because you are giving them the same image in this line

enemyImg.append(pygame.image.load('enemy.png').convert_alpha())

Just remove the for loop, and add each image to each enemy separately

  • Related