So you are marooned, in the middle of a forest. You have no choice but to accept your ill fate and spend the rest of your life, living all by yourself. After clearing a portion of land with your stock chainsaw, wishing that someone was there to witness all your lumberjackness, you pile up the logs. You are left with a clearing that now has an exposed freshwater spring to quench your thirst, a makeshift grill for cooking your kills and a tent you erected to sleep in.
You’re all set to spend the rest of your miserable life here until you realize you have a neighbor, a grizzly bear. He is very special to you since he seems to want you dead. He visits your clearing once every 30 days. All you need to do to keep him away is to build a log barrier surrounding your expanse. You’re chock full of logs from clearing the forest, but with very limited energy since your diet is very skimpy. Your goal is to build a log barricade around you within 30 days. Failing to do so will get you killed by the bear.
Three bars float around your head. A bar indicating your hunger, thirst and your expendable energy. You have your days counted down to the last minute. You look at the sky and you see a tired programmer looking back at you. Yes! You’re inside a game being made by a beginner programmer who just learned how to read through an online documentation of a 2D game library.
If you have no programming background, chances are, you are advised to start with Python. Python is relatively simple to grasp since you don’t have any intimidating curly braces nor are you in any obligation to add a semicolon at the end of every single line. You don’t need to declare a variable before using it as well and so on. The simple syntax makes it less terrifying, and allows you to focus on directly working with the code.
As beginners, we all have a learning curve to climb. An increase in learning comes from a greater experience and to gain a greater experience, we need to obtain a greater exposure. What other ways do we gain exposure in programming other than by building something functional ourselves? You can start from writing programs that just run from the console, with no user interface. These programs contain a Command Line Interface or CLI, where the user has to type in commands directly onto the program. You can actually make killer CLI programs although it sounds like a boring thing to do.
You can also use a standard python GUI library, Tkinter, to create a more user-friendly program containing a visual flair and added abstraction to make the lives of your users easy. But nothing beats creating your own game and learning to code out of it. That’s where we use the 2D game library called PyGame. PyGame is not a standard Python library, that is you would need to install it using the “python -m pip install pygame” command on your console.
Making the game
The storyline above is that of a 2D strategy game built using the PyGame library called “Bear Attack”. It seems quite complicated to build a survival strategy off of very basic knowledge in programming, and to be honest it is, but it isn’t impossible. All you need to know is how to import the library into your script, how to handle game events using loops and conditions, how to define and call functions and some basic object-oriented programming. In other words, a whole lot of copy-pasting from Stack Overflow and Github, syntax errors, frustration, rage, quitting and ranting to your cat until it finally works right. But hey, you got to start somewhere!
You can have a ton of errors, exceptions and bugs
You run the program and it works just right until you notice the fact that you die every time you place the last log on your log wall. This is not an error or an exception, it’s a bug. Since you happen to single-handedly be the developer and the tester of your game, you get to correct your errors, exceptions, failures and bugs/defects. Your compiler and interpreter will inevitably bring up your errors and exceptions. But a bug might not raise issues when it passes through the parser or the executor. This leaves it entirely in yours or your tester’s hands to spot one.
Use your variables
Variables are your best friends. Let’s say the color of your grizzly neighbourino is a nice burnt sienna brown which has an RGB value (165,42,42). You don’t want to type it every time you need to blit a bear onto your screen. Instead, you create a variable and store the RGB value in it so it can be used elsewhere in the code. Say the grizzly bear decided to dye its fur black due to it going through an emo phase. You just simply have to change the RGB value to (0,0,0) at one location (where it’s assigned to a variable) instead of running through your entire code and changing it at every place you used it.
Naming a variable is also of utmost importance, especially if your program has over 500 lines of codes. Tedious as it seems, it’s better to name a variable “bearWalkingSpeed” than a simple “a” or “z” and wondering which end of the alphabet is about the bear a while later.
Know your canvas
Your canvas is a plane and every single point in the plane has its position stored in an array as x and y coordinates. You can exploit this data in may ways like finding the position of your sprite (player) and passing a condition where if the position of the sprite falls under a certain range, it starts off an event, say resetting the energy bar. Drawing or blitting anything on your canvas might need you knowing exactly where to blit it, in terms of x and y coordinates.
Handle events
In the end, you need to watch out for the player and program your response accordingly. And that’s where you need to know about handling events. In “Bear Attack”, the player does not need to use his/her mouse since every movement of the player sprite can be rendered with arrow keys from the keyboard. Every different key-press is treated as a separate KEYDOWN event and the player moves in the appropriate direction.
PyGame also has a pygame.QUIT event which basically terminates the main event loop and executes the following pygame.quit() method once you click the “X” on the top right corner. It is also an example of an event being handled.
TL;DR
Making games in Python using PyGame library is a good way to gain experience in coding. It is quite frustrating and requires a lot of attention to detail, but it is worth the time. I have mentioned a few tips for you to consider this, using my experience in building “Bear Attack”.
In conclusion, engaging is the most proven way to learn effectively. Your first few times building a game might not be the smoothest experience. Your code is highly prone to clutter. Oftentimes you might be the only one who could “decrypt” the madness. But it is always worth the effort, so don’t give up!
References:
- https://bit.ly/3bNX7M3
- https://www.pygame.org/docs/
- https://bit.ly/3dYeD2e
- https://docs.python.org/3/installing/index.html
- https://bit.ly/3dSgVja
- https://pygame.pfaj.ca/
Image credits: