Here at Beebom, we cover a lot of AI related news and resources. You must have seen our articles around ChatGPT plugins, GPT-4, and more. Naturally, then, I spend a lot of time looking at what AI can do, and yes, trying to incorporate AI into my workflow to see if it can make a drastic difference in my productivity at work. Recently, however, I wanted to try using AI for something other than work. I wanted to see if I can use ChatGPT for game development with AI.
Finding a Game to Build
The first step in building a game, is to figure out what exactly to build. Obviously, you can’t go about writing the code for a game without knowing what it is you’re coding for.
Thankfully, that sort of thing is easy for a tool like ChatGPT. You can just ask it to suggest games that you can build, and it can suggest (most of the time) everything from basic, simple to build games, to more complex and complicated games.
Getting ChatGPT to Write Code
At this point, someone who knows how to code can very easily build this game. However, I wanted to get ChatGPT to build the entire game for me, so I had to come up with prompts for it.
It wasn’t very difficult either. You can simply ask ChatGPT to write the code for the game, and since ChatGPT is context aware, it knows what you’re talking about and can write the code for it, as you can see in the screenshot below.
Now, originally, I had decided to be a clean coder and separate the HTML, CSS, and JS into individual files namely index.html, styles.css, and script.js… following the industry standard of file names.
However, I quickly got annoyed with this, because ChatGPT is actually not that good at writing code.
Fixing Bugs… Because Yes, There are Bugs
The first iteration of the Whack-a-Mole game was an absolute disaster. I opened the file in Chrome and the game simply didn’t work. There were the mole holes on my screen, but no mole peeped through for me to whack and earn points.
Since this is basically the original (non working) code, it doesn’t run. And since I couldn’t (and didn’t want to) figure out where exactly the problem was, I tried just telling ChatGPT that the code doesn’t work.
ChatGPT gave me a new, updated code. And it told me, very confidently I might add, that it has fixed the issue that was causing the code to not run.
However, upon testing, the same issue persisted. This time, I decided to tell ChatGPT what problem I was facing with the code. It quickly came up with an updated version of the code, and this time around, it was able to figure out the problem, fortunately.
The new code worked properly, and I had a game of Whack-a-Mole that I could play on my browser. That’s fun, right?
Iterating and Improving the Game
I did notice one weird thing. The game seemed to stop running after a few seconds. The moles simply didn’t peep through. Going through the code, I quickly realised that the game was hardcoded to run for 10 seconds.
That’s fine, but I wanted to let the player choose the duration of the game themselves. Instead of figuring out how to do this, I simply asked ChatGPT to do it for me.
With the new code, the game now has an input field where the player can enter the number of seconds for which the game needs to run, and then start the game.
True, the game doesn’t look like anything to write home about, and the UI is… usable, at best. However, it goes to show that ChatGPT can, with a lot of work, build a game for you from scratch.
Testing and Final Thoughts
After playing a few rounds of Whack-a-Mole, I decided to try and get ChatGPT to build something else as well. This time around, I settled on Hangman. Similar hijinks ensued in my attempts to get ChatGPT to give me a working code for the game, however, this time around, I had to be way more specific in my prompt to get the corrected code.
ChatGPT can, most of the time, identify bugs in code. However, from what I have experienced so far, it’s only good at finding issues with syntax. It couldn’t, for example, figure out before multiple tries that the problem with the code it wrote, was a simple case of converting uppercase characters to lowercase.
From the point of view of syntax, the code was perfectly alright. However, the characters the player is guessing are in uppercase, and since the code was checking them against the pre-defined word — which is in lowercase — it simply never matched them correctly.
The cool thing is that ChatGPT even put in the code for displaying an image representative of the hangman. All I had to do was create the various stages of the hangman’s journey. In fact, I can even ask ChatGPT to tell me how to go about creating the images.
Sure, the quality of these images is quite abhorrent, but that’s because I didn’t want to be bothered with creating proper images in Photoshop, and decided to take screenshots instead.
AI is Decently Good at Coding, but it’s not Perfect
The bottom line of my game development with AI experience is simple: tools like ChatGPT can do a really good job of writing code. However, ChatGPT doesn’t seem to write the correct code for things in the first try. If anything, you will have to prompt it with multiple descriptions of the issues you’re facing before it can give you a piece of code that actually works the way you want.
Still, it’s impressive in what it can do, and you can, at least, get a barebones version of what you’re looking for to build upon and improve. If you were, however, wondering whether you will be creating full-on games for distribution, ChatGPT (and AI models in general) aren’t there; not yet.