Making Games with AI is Fun, and Slightly Annoying

AI has a long way to go before it can be good at coding

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.

chatgpt game suggestions

So, I asked ChatGPT to suggest some simple games I can build using HTML, CSS, and JavaScript. From that list, I chose to build “Whack-a-Mole”. Sounds like an easy game to build, with a simple enough logic.

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.

At this point, I could try and fix the script, but being an absolute inept coder, I couldn’t, for the life of me, be certain that the issue was in the script, or whether the problem was that the HTML code assigned an id to the HTML tags that was different from what the JavaScript was attempting to use for the game’s logic.

Deciding to give up on manual labor entirely, I asked ChatGPT to put the entire HTML, CSS, and JavaScript code in one single file. Happily, it obliged. But obviously, the code wouldn’t work yet because there was some issue, somewhere.

ai game development combine code snippets

For starters, and this isn’t exactly a bug, ChatGPT put the entire code in one file, but left out space for the JavaScript code. Fortunately, you can simply ask it to give you the rest of the code, and it helpfully spits out the entire HTML file, complete with the code.

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.

fixing bugs with ai for developing a game
Say what you will about ChatGPT, it’s very confident in its appalling bug-fixing capabilities.

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.

ai game development fixed code

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.

game duration hardcoded

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.

game development with ai changing duration

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.

whack a mole game running in browser

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.

game development with ai getting help with creating hangman 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.

#Tags
Comments 0
Leave a Reply

Loading comments...