Over the past few months, I have been learning the C programming language. After reading a long book on the subject, I had decided it was finally time to apply what I had learned. After searching around the internet for project ideas, I had eventually stumbled upon a workshop from CS50, showcasing the SDL2 multimedia library for C. I had never written a graphical application before, so I took it as a challenge to write a game in C. And thus Pong: Hard Mode! was born.
I knew that I wanted to imitate a simple arcade game, but I didn't want to clone it verbatim. Several twists were therefore added to the game to make it more interesting. The premise is simple: The player must manage two balls instead of one. The player also controls both paddles to add an extra challenge and so I didn't have to write an algorithm to play the game ;).
After the first weekend, I had a working prototype consisting of the main game, in addition to a score counter. At that point, I had not planned on working on it further, hence it was one long main function. It was a valuable experience to do it in C however, as I had to load each asset into memory, define a space for it on the screen, and handle input and collisions. After refactoring, most of these functionalities became their own routines, both to reduce headache, and so I can share the code without being embarrassed with myself.
I knew that I wanted to continue working on the game, but the actual gameplay was more or less complete. I therefore chose to add a leaderboard, which proved to be quite a challenge in C. The first step was reading a username from the user. This involved reading the input, appending it to a string, turning the string into the texture, and rendering it. When the player hits backspace, it had to remove a character and rerender the string. After the player finishes a game, their score and their username are appended to a data file for persistence. Then that data file is read from, where each line is an element in an array of char pointers. These strings are then sorted based on the score field and the first 16 strings are rendered to the leaderboard.
This project was great fun write, and it allowed me to practice many useful skills. I think that the most important skill I utilized was perhaps organizing a project of nontrivial size (okay, I mean more than a single file). I was forced to pick and choose which functionalities I would reuse so I could make functions for them, and then how to pass data around to those functions. My use of dynamic memory allocation for the leaderboard also reinforced the concepts behind pointers, after many painful segfaults! Overall, it was a worthwhile experience, and I hope others will see it and maybe even contribute!