DEV Community

Cover image for Animating Sprites In 2D games
Muhammad Faseeh
Muhammad Faseeh

Posted on

Animating Sprites In 2D games

Basics of Animating Sprites in Game Dev:

**Animating sprites while making a 2D game is a really important part of that game.

The method I am going to tell will work literally for every game framework:
> The Logic Matters, syntax does'nt.

Here are some steps to implement the animation:

  1. Grab a spritesheet and it is better to split it into frames, but if yo dont wanna waster time you can use the spritesheet as well but that's the case for another blog.

  2. Load the each frames using a list or list comprehension i.e
    e.g #python spritesheet = [pygame.image.load(f"Assets/{i}.png") for i in range(1, total_frames)] in this case 4

2.After that you have implemented a basic setup in your code, now you can create a method named ## func/def/void animate_sprites()
or as you wanna name it.

  1. Now create some variables such as the animation_boolean(that tells when to load the animation) and animation_index(that holds the count for each frame).
    Note if you want an endles animation you can skip the animation_boolean or set that to true forever.

  2. Now inside the animation_boolean() method, whenver the anim_bool is true i.e:
    if (animation_bool):
    animation_index += 1

    if (animation_index >= 40):
    animation_index = 0 # higher number means
    smoother and slower animation and then reset it to zero, make sure that your stop limit should be divisible by the number of frames

  3. Now, finally whichever framework you are using display the sprites using the built-in draw method for any library in such a way that the indexing of the list is according to the no. of frames i.e

    python WIN.blit(spritesheet[animation_index // 10], (0,0))

  4. Call the render method and here you go, you've learned how to animate sprites literally in any framework.
    **

Make sure to like and comment if you found it helpful

Top comments (0)