Pygame tutorial 1 – 3.3 – Animation while moving

This is an interesting evolution of our little application to move around a sprite. Now we do not only:

  • move the sprite
  • but we animate it while it’s moving
  • and we also flip it
  • and… I was almost forgetting, we make the animation ‘indipendent’ from the frame rate (almost)

Nothing too complicated, thought, because we want to proceed with caution and trying some experiment.

The Cat class

Our sprite animation is managed by this class. Very simply.

The __init__ method

This method contains the stuff that are executed when we create an istance of the class:

So, we pass also the 100, 100 arguments that are used to set the initial position of the sprite, that is this, by the way,

This is in the cat folder. You find everything in this github repository. Images are not mine and are free.

We were talnking about the __init__ method (there are two _ before and after init):

Here we just take the x and y position of the cat, we create a list with all the surface images (read the previous posts to know what a surface is post 1, 2, 3.1, 3.2) and then we set the self.image = self.list[0] as the starting image and “right” as the starting direction (where the cat is watching).

The update method

This one is the most interesting part, where the images are cycled so that you see an animation… but it’s an illusion, there are simply different images of different poses… but well I think you get it yet.

This method will be called in the while loop that makes everything go on (again watch the past posts if you do not know what is the while loop that makes everything go on: post 1, 2, 3.1, 3.2).

The entire code for the animation

Ok, now we’re done for the moment. Watch my video where I try to explain what I did in the code. See ya in the next post for more complicated and yet fun stuffs.

The video with the code explanation

Pygame map editor devlog

The devlog of the Pygame map editor. A work in progress to create a map editor to create maps with tiles with an example of a game that uses these maps. The current version is 1.5 of the map editor and 0.2 of the game, not yet ready for the 1.5 version of the editor (16.9.2020). Repository of the map editor