0

I have a spritesheet for a game I'm making and it's for one of the enemies in the game. I loaded the spritesheet correctly (I think) and I got the code make it appear and move to work. When I run the code though, the enemy doesn't appear.

The code for loading the spritesheet:

enemySheet = pygame.image.load("resources/Alien.png").convert_alpha()
transColor = (255,255,255)
cells = []
for n in range(3):
    width, height=(36,32)
    rect = pygame.Rect(n * width, 0, width, height)
    image = pygame.Surface(rect.size, pygame.SRCALPHA).convert_alpha()
    image.blit(enemySheet, (0,0), rect)
    cells.append(image)

enemyImg = cells[0]
enemyImg.set_colorkey(transColor)
enemy = enemyImg.get_rect()
enemy.center = (216,216)

The code for spawning the enemy:

if timeTillEnemy <= 0:
    enemies.append([0,50,0])
    timeTillEnemy = random.randrange(0,800)
timeTillEnemy -= 1

Here's the code for the enemy's movement:

for enemy in enemies:
    index = 0
    enemy[0] += 2
    if enemy[1] >= 270:
        enemy[2] = -2
    else:
        enemy[2] = enemy[2] + 0.01
    enemy[1] = enemy[1] + enemy[2]
    screen.blit(enemyImg, (enemy[0],enemy[1]))

    if enemy[0] > 470:
        enemies.pop(index)
    index += 1

I haven't gotten around to animating it yet so that's why it isn't here. There is no error message and everything runs just fine. The enemy isn't appearing though so that's why I'm here. All help is appreciated.

MrFoxGamer
  • 67
  • 6
  • Please post a [minimal, runnable example](https://stackoverflow.com/help/mcve), otherwise it's difficult or even impossible for us to find the error. – skrx Jan 10 '18 at 03:22
  • you can use pygame.Surface.subsurface to create images from spritesheet. – furas Jan 10 '18 at 08:28
  • you use `pygame.Rect()` to set enemy position - `enemy.center = ...` - but later you use some list with position and you have to use `enemy[0]`, `enemy[1]`. You should keep `Rect()` on list and then you can use `enemy.x`, `enemy.y` and `blit(enemyImg, enemy)`. It will be more readable, and `Rect()` has methods to check collision. – furas Jan 10 '18 at 08:30

0 Answers0