When AI Crafts Games: A Peek into Building a JS Game with ChatGPT

Vladimir Balko
4 min readJul 16, 2023

--

TRY GAME HERE

Motivation

From the moment I first discovered that ChatGPT could code, I was eager to test whether I would be able to create a functioning application — even if it was small. This task isn’t necessarily challenging — you can have ChatGPT generate the basic structure of the application, then describe its functionality, and GPT will supply you with the code, file structure, and best practices.

How it went

However, I aimed to create an application where I acted only as the idea-generator and guided the programmer (ChatGPT) towards what I wanted to accomplish, with the AI doing all the heavy lifting. And I managed to do just that. There were, of course, some hitches along the way — as there always are — but I can confidently say that the AI programmer (ChatGPT) carried out 95% of the coding work. I was responsible for providing ideas, specifying what I wanted to achieve, and, due to ChatGPT’s technical limitations, I did the hands-on work — i.e., copy-pasting what the AI wrote into the actual code, using GitHub’s Codespaces.

My project resides in a GitHub repository at https://github.com/vbalko/simple-fighter-game. I committed after almost every modification, whether it was the addition of a new feature or a bug fix. This allows you to observe the progression of the app’s development. As the code started to become more complex, there were times when additional prompts were necessary, and sometimes, I forgot to make a commit. Nevertheless, the commits should provide an almost complete chronicle of the game’s evolution.

I found myself in a position twice where ChatGPT lost the context, and the code it generated either failed to incorporate previous changes or didn’t sufficiently address my requirements. As a result, I had to re-establish the context by providing the current version of the source code as a prompt. This tactic partially restored the context, but as the code volume increased, it became clear that we were beginning to lose track. By the end of the project, it was evident that we were only focusing on sections of the code and missing the connections to the remaining parts. This required me to step out of my role as the idea provider and analyze the code to identify what was lacking or excessive. Generally, though, ChatGPT shouldered the bulk of the developer’s workload.

And here we run into another problem, which originates from the fact that ChatGPT is a general-purpose language model, not one tailored for programming. There were instances where a new version or iteration resulted in the disappearance of some functionality that was implemented in previous steps. Additionally, the consistency and cleanliness of the code are rather — let’s say — unsatisfactory from a maintainability standpoint. I attempted a refactoring once, during the early versions of the game, and the results were almost satisfactory. The code was substantially tidied up, but following a few more iterations, I realized that some functionalities had been lost. Consequently, I refrained from further refactoring in the later stages, and the code remains as is. Of course, this issue could be addressed by implementing unit tests, but this was meant to be a simple project in vanilla JavaScript. Implementing tests would further saturate the context and would likely necessitate more significant interventions into the application’s structure and breakdown. That was beyond the scope of what I wanted to experiment with — perhaps next time.

AI putting pieces of puzzle together

Conclusion

In conclusion, my overall experience was positive, and I can assert that, with some effort, ChatGPT can indeed develop a complete application. However, there are certain constraints — it must be a simple application that can fit within the model’s context to allow it to make informed modifications. Also, it seems unlikely that a requester (or idea maker) without programming experience and analytical thought could replicate what I’ve attempted here, as they would probably get stuck at the points where I had to “cheat” and lend my expertise. On the other hand, we shouldn’t discredit this extraordinary tool — after all, its primary function isn’t to build standalone applications. What it excels at is translating my thoughts into individual pieces of code and helping with boilerplate tasks. In that regard, it’s an invaluable aide. However, it still falls upon me — the programmer — to piece together these puzzle fragments into a complete picture.

This brings me to a quote that’s become quite prevalent lately:

AI WON’T TAKE YOUR JOB, PEOPLE WHO KNOW HOW TO USE AI WILL TAKE YOUR JOB.

Some additional info

The concept was to create a game without utilising any external assets — as a result, everything present is defined in the code itself.

You can find two ChatGPT conversations that I used during the development of this game. There are two instances because I reached the limit of the number of messages allowed per time frame with ChatGPT 4, which resulted in a switch to ChatGPT 3.5. However, this version did not meet my expectations (subjectively), which led me to start a new conversation in GPT 4.

PLAY GAME HERE

--

--