Like most millennials, I like video games. Hell, I grew up on them. Smash Bros, Galaga, Goldeneye, you name it. I was the quintessential middle school boy crushing Mountain Dew, playing Call of Duty until 2 A.M.
Naturally, I started tinkering. I pulled up ChatGPT and installed a Python program that would allow me to build the classic video game, Pong, on my laptop. I followed this rabbit hole for an hour or two until my mom leaned over and asked what the heck I was up to.
To my surprise, curiosity struck the cat and within minutes my mother – who has trouble logging into Facebook – was helping me code a HubSpot-themed video game. We spent a good two hours tinkering with code, testing snippets, and prompting ChatGPT in different ways that might yield interesting results.
That’s right, a humble blogger – and his mother – can turn into full-fledged game developers with the help of generative AI.
I’m not saying it’s like flipping a switch and suddenly you read code like it’s your native tongue. It’s more like having a really smart person at the ready to decipher snippets, tweak broken bits of code, and explain why some things are working and why some things aren’t when you’re moments away from slamming your head against your desk.
If you’re like me, by this point you’ve left this post and have started on your StarFox masterpiece. This also means you’re about 20 minutes away from hitting your first roadblock and are about to circle back to this post to see how I pulled it off.
With that in mind, let’s get started as I’m sure the over-eager readers will catch up.
How to Build a Video Game With Python & AI
Step 1: Start with a simple prompt.
Since this is going to be a multi-step process, I decided to stick to simple prompts. Here’s what I started with:
ChatGPT produced an elaborate code snippet that I copied into a Python file. That part was simple enough, but the next step, not so much.
Before we travel too far down this road, it’s important to note that I had Python, PIP, and all of the required Python libraries installed before prompting ChatGPT. If you need help installing these, GPT can provide a really comprehensive walkthrough.
Now, back to our regularly scheduled program.
Step 2: Test your code.
I wasn’t completely sure how to test my new contraption, so I asked GPT for a little help.
It helped me navigate the Terminal app and load up my pong game. It wasn’t a bad start for only investing about 10 minutes of my time.
Not too shabby, right? But, there are still a few flaws. The ball doesn’t reload after you score, the AI opponent is unbeatable, and there’s no scoreboard to keep track of who’s winning. Let’s fix that.
Step 3: Refine your prompt.
First, I wanted to fix the game so you could play continuously after a score was made. So, I asked ChatGPT to provide some additional code:
Now the ball resets at the center of the screen after a point is scored.
But, we’re not finished, yet. I want to add a scoreboard and have the game end after five points. On to our next step and the arrival of our co-developer, Liz Fontanella.
Step 4: Analyze any malfunctioning code.
So far, it’s been like waving a magic wand and seeing every wish get granted – if only relationships were this easy.
But, I did hit a snafu when adding the scoreboard – and this is when my mom came into the picture.
The code provided didn’t seem to do anything. Being the prompt engineer that she is, Liz suggested copying and pasting the code into ChatGPT and asking it to solve the problem.
It took some tinkering, but after a few back and forths, I added a scoreboard, adjusted the ball speed, and created difficulty levels for the AI opponent.
Here are some of the prompts I used:
- “Can you code me a snippet that will add a scoreboard to the game?”
- “Hmm that didn't seem to do anything, can you tell me why the scoreboard isn’t appearing on the game? Here is the code I am using:”
- “Awesome! With that code, the scoreboard is showing, but it is not changing after a score is made. How do I fix that?”
- “The AI opponent is way too good. Is there a way to make the opponent easier to beat?”
- “That didn't seem to do much. I am still seeing the AI respond at the same speeds as before. What else can I try?”
- “How would I add a snippet that makes the ball slowly increase in “x and y” ball speed after it collides with a paddle?“
- “That didn't seem to work, here is my code. How can I fix this?”
- “Thanks! Can you help me add a snippet of code that would reset the ball speed after each score?”
My workflow looked something like this:
- Ask for a feature, test the code provided.
- If the code worked, celebrate and move on.
- If it didn’t, I copied my entire code and pasted it into ChatGPT, and said, “Here, you fix it.” The buck doesn’t stop at me when I have an AI assistant at the ready.
It might take two or three tries before you get the code exactly right, but LLMs like ChatGPT can walk you through where to put the code, what it does, and how to adjust it to achieve the desired result. Not to mention how detailed the notes are. It adds plenty of comments throughout the code to let you and other developers know what the heck each line does and why it’s there. Great for beginners, and ideal for experienced developers troubleshooting your code.
At this point, our pong game looks something like this:
Also at this point, my mother and I feel invincible. We’re the kings of code, pinnacles of Python, the Internet is our oyster. With nothing to stop us besides my computer’s battery life, we unleashed a fury of new features.
Step 5: Go wild.
By the end of this step, we went from this:
First, we asked ChatGPT for help with the color scheme. Coming through as always, it provided a basic code to get started with. But, it wasn’t perfect. In fact, it caused the game to crash, resulting in an error in Terminal.
So, we copied and pasted the error into ChatGPT and asked it what it meant:
Not only did ChatGPT explain the issue, but it also provided a code snippet and steps to correct it. It was like IT was standing over my shoulder, ready to solve the problem the moment it appeared.
After that, we swapped the ball with a HubSpot sprocket and added a feature that made it spin continuously over time.
And, for my final trick, we added a feature that starts the game after the player hits “Enter” on their keyboard. It was the chef’s kiss to cap off a successful afternoon of coding.
Step 6: Share.
If I’ve learned anything about AI in the last few months, it’s that the more you share and discuss how you are using it, the better you get at working with it. So, in the open-source spirit, here is a link to the final code we settled on for the HubSpot-themed pong game.
Feel free to borrow, copy, outright steal, you name it. Or, take a crack at building it yourself using the steps listed above. You’ll probably find ways to improve upon my process and create something even cooler – like Snake, definitely build Snake.