Knowing what composes a thing is just the barebones start; learning how to build the thing from scratch is the ultimate beginning. But for now, I need to cool down my brain from all the machine learning slop…
Pygame, a popular Python library, provides a simple and intuitive way to create 2D games and multimedia applications. With its easy-to-learn syntax and powerful features, it is an excellent choice for both beginners and experienced programmers.
Pygame is an intermediate-level cross-platform Python library designed for creating video games. It provides a set of functions and classes for handling graphics, sound, and input/output, making it a popular choice for game development, simulations, and multimedia applications.
While the library has been around for many years, it remains a valuable tool for game development and other multimedia projects, especially for beginners and hobbyists. This is because:
Ease of Use: Pygame offers a simple and intuitive API, making it easy to learn and use for those new to game development.
Cross-Platform Compatibility: Pygame can be used to create games that run on multiple platforms, including Windows, macOS, and Linux.
Active Community: Pygame has a large and active community that provides support, tutorials, and resources.
Educational Tool: Pygame is often used in educational settings to teach programming concepts and game development principles.
The most barebones Pygame application runs on a library-unique window. You will first need to initialize all Pygame modules with init() at the very start of your file. After that, the game window can be built with display.set_mode(width, height).
For this tutorial, I am building a 2D platformer logic game with four-direction movement controls. Think of Vampire Survivors or Enter the Gungeon.
Frame by frame, the Pygame program will be continuously updating and checking its assets, from its screen to event scripts. The next scene then overlaps the previous scene at a speed invisible to the naked human eye.
Below are controls for each of the four directions. They and many more button variables can be fetched from the key.ger_pressed() function to simulate movement. For extra measure, you can add an extra condition to prevent your controllable character from going out of bounds.
Speaking of controllable characters, we can draw a movable rectangle with draw.rect() to act as one. All updates performed by lines of commands will finally overwrite the current window frame when update() is run.
We now have a basic rectangle that can move in four directions when we press their keys.
Include the quit() command at the end of your file, otherwise, you will crash your kernel and be forced to reset it.
For better organization, for values that will be repeatably referenced in your projects, making variables will save you a lot of time during fine-tuning.
Onto the next topic, on a flat surface with no higher platforms, the quadratic equation can be borrowed to simulate player jumping. Before we get into the mathematical specifics, we can make a jumping state Boolean and duration first.
To change our controls for a platforming game instead of a 2D RPG, we remove the controls for velocity-based vertical movement (up and down).
Using parabolic trajectories to simulate jumping is common practice in believable and visually appealing game design. Here is how the tutorial's formula below works:
jumpDirection flips the parabola, making the object jump upwards or downwards.
As jumpCount increases, the squared value grows, causing the object to move vertically at an accelerating rate.
The 0.5 factor controls the rate of acceleration, determining the height and duration of the jump.
isJumping is merely a state designation varaible for telling the program of the player's state with regards to jumping.
We now have a rectangle that can merrily bounce across the screen like a jumping bean.
The following sprites can be downloaded here.
Pygame has importing capabilities for the artistic developer. For now, we will be looking at image files and the process of animating them by going through them very quickly via lists in a dictionary.
Now that I removed the directions up and down in my game, only two directions – left and right – remain. You COULD flip your sprites manually in non-programming space… or you could just use transform.flip() to flip each sprite in your animation lists.
The files above are too big for the window, so we can easily scale each list sprite with transform.scale().
Variables like jumpCount can be referential indexes for animation list fetching. For that, we can make walkCount and idleCount to animate our player's walking and idle animations.
Global variables are designed to be accessed and modified from anywhere in the code. They should be used sparingly and with caution, as they can make code harder to understand and maintain, so use local variables and functions whenever possible.
Keep in mind that Python is intolerant of index overflow. Ensure the variables above reset to 0 every time they reach their total length.
In the code block below, you can use the same -count variable to keep track of vertically flipped animation groups. However, I recommend building a variable for each other dictionary list for customization's sake, even if some share the same number of frames.
Keep tinkering to your own liking.
Having a redraw function can ensure your game remains responsive and interactive even while performing complex computations. By redrawing your game window only when necessary, you also save precious system resources like CPU and memory for optimizing gameplay performance.
It can also make debugging less of a chore by removing the hassles of building a potentially messier game loop.
If a game's frame rate is too high, it can lead to visual glitches, stuttering, or even crashes. On the other hand, if the frame rate is too low, the game may appear sluggish and unresponsive.
By limiting how many times the game window is redrawn via tick(), you can control the frame rate and ensure that the game runs smoothly on different systems. This helps maintain a consistent and enjoyable experience for players.