I will begin by describing some of the basic concepts of Gameloop Free Fire development. The first thing you need to know is that games are generally created using “frames.” A-frame can be described as an image or screenshot of what is happening in a video game, with each image being essentially a different moment in time. For example, if there were 10 seconds between each frame, and your game is running at 60 FPS (frames per second), there would be 600 frames.
Gameloop consists of a python script that loads a windowed Pygame project in each frame of a sprite sheet. In this article, I will use the image below as an example:
As you can see, each frame is essentially a different animation. The python script loops through the shelves and draws them to a surface on top of one another to appear as an animation. You can also have more than one animation, if necessary, by creating a separate sprite sheet with each set of animations on its own set of frames.
Package Called Pygame
The next thing you need to know is that I use a package called Pygame to make the game. Pygame is basically like an API (application programming interface) that gives you access to many of the essential functions you would find in other languages, but with the added convenience of dealing with them all together instead of using different APIs for different things.
Pygame is not the only package that can make games, but it’s an excellent place to start. Pygame also provides a convenient sprite-sheet parser that you might want to use if you don’t want to code your own from scratch, as I have done.
A sprite sheet is simply an image containing all the sprites of your game, with each sprite being stored on different frames. For example, if you had an idea with four shelves and three spirits total on there (each sprite takes up one edge), each sprite would take up 1/3rd of the entire image. I used a separate sheet for each animation because sometimes the different spirits use the same frames, and it’s convenient to keep those separate.
The article now continues as if I had used a single sprite sheet with all the animations on there:
“In my case, I have five different animations, for a total of 20 different frames. Each frame is essentially an image from the animation that will slowly play through each frame in sequence at a rate of 60 FPS. To do this, we need to create a Pygame window and draw each frame to it. To keep things simple, I will use the basic template with Pygame in a while loop:
I am leaving out some lines of code so that you don’t have to copy and paste the entire script. But if you would like to download it or view the complete source code, you can do so here:
“The while loop will run indefinitely. Inside the Gameloop Free Fire, we create a new surface and begin drawing some things:
“The first thing we do is set the background colour to white. We do this by creating a surface with that colour, then blitting it onto our main character. Blitting means copying an image from one place to another.
Next, we get the sprite sheet for each of our animations using Pygame’s built-in parser:
“The parser takes an image and returns each frame as a different sprite. Each sprite is essentially like an element in an array, but the difference is that spirits are only used for images (surfaces) rather than numbers or other things. We need to put all our animations onto one sheet because Pygame requires them to be on the same sheet when you blit them, so if they were on separate sheets, Pygame would be unable to recognize them.
“So this is what we did:
“We assigned a variable named animations_list for the sprites that would hold each animation. Why did we use an underscore? It’s called a “mutable” variable because its value can be changed. In other words, the sprite list is a list of mutable sprites instead of a list of static sprites. If we wanted to have a separate sprite sheet for each animation, we could have used an “immutable” variable instead.
Immediately after assigning the animations_list variable, we added a for loop to iterate through all the sprites (or animations) one by one, like so: for energy in animations_list:
“Inside this for loop, we are going to place the code that blits each frame onto our main surface. This can be done using another built-in function named blit. It usually takes two arguments: the character we want to blit and where we want it to be placed. Since we made animations_list a list of mutable sprites, each can be passed in to this function in sequence.