I am following the video The ultimate introduction to Pygame from YouTube. This video will hopefully help me learn more about Pygame, so I can use the knowledge for future projects.
Despite the run time being below 4 hours, the time I spend to understand the material will make it much longer of a project.
How Pygame works
First, we start with explaining how pygame really works. Graphics, just like in a movie, is a bunch of images played really fast to give the illusion of proper motion. These images are dynamic, however! Meaning the inputs we use (WASD, space, etc) will affect how that image turns out, and what directions it will take. We use loops to check for these events and update accordingly, and this loop should always be active, checking for changes in user input. Yes, holding down a button and/or releasing it are both inputs, along with the normal expected tap input.
When things update accordingly, they can be collecting a coin when it is touched, losing health when touching any enemy, picking up a new item, or even opening your inventory. All of this information will be used to update the screen, or image, and alter the following screens.
What Pygame does for us
Making an image can be very difficult, especially when it can be so dynamic and change little aspects on the screen. Pygame, however, helps us incredibly with this process. It generates, or draws, based on the information we have for it and give it, making it much easier to properly tie everything together when coding the actual game mechanics. Pygame can also play sounds and tie those in much easier than it would ever be for us to code it without pygame.
Pygame can take many inputs and is much better at managing them. Tying them to certain actions is made much easier by the generation based on dynamic inputs that Pygame can do.
Pygame can do many things by default, like text, detecting collision, timers, and more! While these things are possible to code, pygame makes it much easier on us. As coders, we want to spend as little time on one part as possible. Normally, shortcuts are very much encouraged in such a field, or even on your own time. They make the process simpler and easier for everyone involved.
Pygame, however, is not for complex games you would see on the average Steam page or something. While it has many modules to help making a game, it is not built for something so complex. More so, it is helpful to get your foot in the door, or possibly make practice, simpler 2D games for your journey into game development. Practicing with Pygame can help you learn code better and understand and have more practice with systems in code. Hopefully, it is also good for your portfolio! The more experience with code, the better. Doesn’t hurt to add 🙂
Into the code
Making a window in pygame
At the start, as with all additional modules in python, we import it.
Import pygame
This is to let the program know that we are using the module, as often the system will skip over it as to not use up unnecessary space or ram. Now, we will need to activate it properly to use all it’s modules.
Import pygame
pygame.init()
This initializes pygame, and makes it and its modules usable. Finally, we make the window. It is relatively simple, thanks to pygame.
Import pygame
pygame.init()
screen = pygame.display.set_mode((800,400))
With this, we technically have created our first screen. It only shows up for a moment, as it is only one frame. Once the code ends, after our singular frame, it closes, as that is all that it was told to do. Once it is done with a process, it shuts it down. This is what loops are for. With loops, we can continuously call a frame, making it not close, as there is no end yet.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,400))
while True:
Since there is nothing to set to false, it will always be true. This makes an infinite loop, that can only be broken from the inside.
Leave a Reply