-1

I am creating a game in pygame, and am trying to make a death animation for when the enemy dies. My code for the animation is as following:

if enemy_health <= 0:
  Number += 1
  if Number >= 1 and Number < 2:
    enemy_image = pygame.image.load('image\\1.png').convert_alpha()  
  if Number >= 2 and Number < 3:
    enemy_image = pygame.image.load('image\\2.png').convert_alpha()
  if Number >= 3 and Number < 4:
    enemy_image == pygame.image.load('image\\3.png').convert_alpha()
  if Number >= 4 and Number < 5:
    enemy_image == pygame.image.load('image\\4.png').convert_alpha()
  if Number >= 5 and Number < 6:
    enemy_image == pygame.image.load('image\\5.png').convert_alpha()
  if Number >= 6 and Number < 7:
    enemy_image == pygame.image.load('image\\6.png').convert_alpha()
  if Number >= 7 and Number < 8:
    enemy_image == pygame.image.load('image\\7.png').convert_alpha()
  if Number >= 8 and number < 9:
    enemy_image == pygame.image.load('image\\8.png').convert_alpha()
    Number = 0

What this should be doing is making it so when the enemy health is less than or equal to zero, the animation will play. I don't know why. I tried to do print(Number), and all I got was that Number was equal to 0. I tried to change what Number started as -- I changed it to 5 --, and then, when I used the printing method, it said that Number is equal to 5. In general, Number is equal to what it started as, even though it should be changing.

I don't get an actual error, but what happens is that the UFO (enemy) image just switches to the first frame, but it never goes further than that.

Here is some more code for reference:

Setting enemy_image:

enemy_image = pygame.image.load('file').convert_alpha()

Blitting enemy_image onto the window:

window.blit(enemy_image, (enemy.topleft[0] - 20, enemy.topleft[1]))

Enemy rect:

enemy = pygame.draw.rect(window, black, (shooting_x, shooting_y, 100, 75))
oB1qtx
  • 1
  • 3
  • Does this answer your question? [How to animate drawings in pygame (movement)](https://stackoverflow.com/questions/13527402/how-to-animate-drawings-in-pygame-movement) – import random Oct 04 '22 at 06:33
  • The same way that you animate in any other environment: by repeatedly drawing, with a *change* to what is drawn each time, while *allowing a small amount of time to elapse* between each drawing. There are numerous typos in the code you show (`==` vs `=`), and the images should be loaded ahead of time (because the loading is slow, and because it only needs to be done once per image) and stored in a list (because that makes them easy to work with). Saying any more requires a proper [mre]. – Karl Knechtel Oct 04 '22 at 06:49
  • "In general, Number is equal to what it started as, even though it should be changing." Well, **did you try to check** that this part of the code runs, in the first place? Did you check what `Number` is **immediately before and after** that part of the code? Did you check whether **anything else** in the code changes `Number`? Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – Karl Knechtel Oct 04 '22 at 06:51

1 Answers1

-1

looking at your code, it is the indentation on line 2 'Number += 1'

Then in that case of line 2 not being a problem, I have then realized that using this code wouldn't work as when you pass that first if statement the next one is then also true and so on, so in 1 tick of your game it has gone through all the statements and then set number back to zero.

what I would do is this:

#start of code (outside of main loop)
number = 10 # so the 'if' statement isn't triggered for now

#when character dies do this
number = 0

#In Main Loop
if number =< 8:
    number += 1
    enemy_image = pygame.image.load('image\\'+str(number)+'.png').convert_alpha()