Space Game with Python and Pygame
During my third phase at Flatiron School, I reengaged with Python and delved deeper into leveraging libraries like Pygame for game development. During my first exposure to Python, I struggled to grasp the concept of Object Oriented Programming. Even after gaining familiarity with Python syntax, concepts like lists and dictionaries, and how to construct different functions, the more abstract and overarching concept of Classes seemed to elude me. However, as I attempted to dissect a library on my own in order to create a game, the doors of object oriented programming conceptually opened up, allowing me to find a deeper understanding of what OOP means, the purpose of class construction, and how to optimally organize and construct code for games.
At the onset of my Phase 3 project, blending my Python basics with an entire library for game development seemed a formidable task. From not knowing what direction to take, to not knowing how the tools I have acquired with Python directly translate to game development, to feeling overwhelmed with the extensiveness of Pygame, I began the process simply by watching YouTube tutorials and gaining a sense of what kind of game I would like to create. I began my project with the one motivation to create a game with a constantly scrolling screen. Having grown up on games like Flappy Bird, Jetpack Joyride, Temple Run, and more, I was motivated to develop a similar game. Although an infinitely scrolling screen seemed like a simple concept, I was fascinated with the idea that I would be able to use a single image in a loop to create a game that could theoretically continue forever. The implementation of an infinitely scrolling window seemed fairly straightforward. Using the width of my background image and the width of my screen, all that was required was to loop through an “infinite” range that appends the background image onto the window directly where the image last ended so that it seems like the image never ends.
Even after this initial step, the beginning portion of my project development felt a lot like trial and error. One example of this was in gaining familiarity with writing clean code. While I was writing all my code in one file similar to the way I began my first two phases, the tutorials that I watched seemed to break the code apart into a main component, a game logic component, and the individual elements of the game themselves. This concept of identifying the different components of my game like how to load different elements, how to control the game flow, and how to construct and introduce different elements not only helped me understand writing clean code, but also to more holistically conceptualize what Object Oriented Programming means. By removing elements out of my main component and being forced to pass information back and forth between files while constructing classes, I found myself developing a more organized understanding of what I was doing. Additionally, similar to the process of identifying reusable components in React, refactoring my code according to its game purpose whether it be a reusable component or a factor that controls gameflow helped not only make my code more readable, but also easier to work with and expand upon.
Although I’m proud of what I learned in terms of what object oriented programming means, how to construct classes, and how to organize my code, I’m excited to further develop these skills in the future. One part of my project that I struggled with was adding additional enemies to my game. Due to the way I had constructed the logical handling of enemies based on my primary Enemy class, it was extremely difficult to create additional classes that seamlessly fit the same mold without changing the game implementation that I had desired.. In hindsight, had I structured the game logic and flow of the code to handle a universal concept of enemies, it might have been much easier for me to construct similar enemy classes to pass onto the game. Ultimately, through this process of trial and error while creating this game, I feel more comfortable with my ability to better plan the construction of my code to allow for additional features and complexities in the future.
Another skill I hope to develop is my ability to think like a game developer. Having grown up playing video games, this project was definitely the most enjoyable so far despite its difficulties. Yet having only been on the consumer side of gaming, I found that it wasn’t as natural or obvious to figure out how to correctly construct game logic. Aspects of my code that I struggled with included manipulating time to represent phases, controlling the game logic for each phase, and updating variables based on game logic and constructs designed by myself. One specific example of a problem I had was when I wanted to increment the number of enemies on each phase. Although I thought this would be as simple as incrementing a value of enemies to be spawned as each phase number increases, a side effect of the design of my code resulted in that number continuously being incremented not only as each phase began, but during the 20 second duration of each phase as well.
As a whole, I know that I have a lot to learn in terms of the functionality of Python, and the processes of creating dynamic solutions that can continuously be built upon and improved. Still, considering how disconnected these concepts seemed to feel in the beginning of this project, I’m proud of the insight and knowledge I have gained. As I continue learning new languages, I aspire to further develop this, more generalized, skill set of logically deconstructing problems to devise optimal and concise solutions that can be consistently applied throughout my career.
Subscribe to my newsletter
Read articles from Christopher Kim directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by