How I Let an AI Code a Game For Me! by@lukaszwronski

How I Let an AI Code a Game For Me!

June 29th 2022 11,897 reads
Read on Terminal Reader
react to story with heart
react to story with light
react to story with boat
react to story with money
I was trying to figure out if I can write entire Space Invaders clone using just the code written by an AI. I've used Copilot to try it out. It took 2 hours to build the game with some major issues, but in general the experiment was a success. You can see my struggle in a 2 hour long video attached to this article.
Łukasz Wroński HackerNoon profile picture

Łukasz Wroński

CEO, developer, hacker, father of two, bass player, internet troll and meme enthusiast...

linkedin social icon

Recently I've found that Github Copilot, an AI that helping to write the code by suggesting whole snippets (often based just on natural language comments), is now available as a commercial service. No longer needing to wait for beta access, I've checked myself in for a two-month free trial.

I have to say - my expectations were high. I've played a bit with OpenAI API before. I believe it uses the same model as Copilot, so I knew it could be a mind-blowing experience. I've installed it as a Visual Studio Code extension and started to wonder how should I challenge my new virtual colleague.

Let the Games Begin

Reading some of the community comments and articles, I've found that one of Copilot's advantages is that this AI knows more programming languages, frameworks and APIs than any flash and bone developer living today. After all, it learns using every single public GitHub repository ever existed. It can be more than just a productivity tool. It can also be an interactive way to discover new things.

While looking forward to utilising it this way, I've decided to build a game. A clone of Space Invaders seemed to be an appropriate choice, but to make this more interesting, I've made a few assumptions:

  1. I'll use Python - a language I bearly know and employ it only from time to time to solve CTF challenges
  2. I'll use PyGame - a library I've fiddled with over ten years ago to build a small academic project
  3. I won't prepare in any way - just go with the flow
  4. I won't write a single line of code myself - expecting my Copilot to write it all for me

I've set screen recording, turned on my microphone to do a little commentary, started Visual Studio and just wrote my first request:

"""main function for a pygame"""

Copilot started to give me lines of code, one after another. I've just tab-entered myself through the list of suggestions accepting a ton of imports. Enthusiastic at the start, I've quickly found myself stuck in an infinite loop of adding the same two lines of code over and over again. No good.

Off to a bad start, I've quickly realised that I'm trying to do too much, too quickly. Asking Copilot to add a PyGame import first and then to create a function showing a window did the trick.

At that point, I also realized that by making my request more precise I can improve the results. While writing

"""create a function showing a game window"""

I've got it working except for the fact, that it showed just for a second and then immediately quit. Then I imagined I need to be very straightforward regarding my expectations. Changing it to

"""create a function showing a game window and then left it open"""

Copilot extended the code adding a basic game loop that prevented the window from getting closed.

It seemed that my buddy and I started to communicate with each other. Nice!

Moths in My Stomach

The next step was to create a spaceship class. I've created a class in a separate file, again started to write another request and found another cool thing. Turns out Copilot suggests not only the code but also the content of the comments I'm writing. I've started asking to use arrow keys to move my spaceship, and it suggested an entire sentence.

Copilot not only write the code, but also suggests what you might want it to do

Copilot not only write the code, but also suggests what you might want it to do

I have to say I was a bit sceptic if Copilot will actually understand what is going on project-wise. Analyzing code in a currently edited file is one thing. Figure out how these files are interconnected is another kettle of fish.

Copilot says: "I got you covered". It is aware of classes in other files, and it was not a problem to use spaceship in our game class. Copilot also figured out that we've defined specific game window size and used it while positioning the spaceship at the bottom centre of the screen.

You must think - "this tool is amazing". Right?

It kind of is, but our relationship was not only moonlight and roses. Some of the problems were trivial, like unnecessary logic in functions I've asked Copilot to write for me. Some suggestions were coming delayed, and I've lost a bit of time waiting for them.

Sometimes I was frustrated because of my own mistakes. I've left a comment that the function should return zero at the top of the file, and then writing another comment below I was figuring out why Copilot is trying to return zero at the end of my function. But it was nothing in comparison to the problem I'd spent almost an hour solving.

I was looking to position the ship in the right place and set its size to be 50x50 pixels in size. I was trying to formulate my comment in so many ways. Always getting the same result. Code, that was not working because Copilot decided to use methods that were not existing in PyGame.

Stubborn as a mule it was trying to invoke set_size and set_center on my loaded sprite object, every single time getting:

AttributeError: 'pygame.Surface' object has no attribute 'set_size'

Countless times I've tried to rephrase my request always getting the code that was not working at all. Soooo frustrating.

At some point, I even thought I should stop recording and forget about it. Label Copilot as not suitable for this purpose and end the experiment. Yet, I felt there must be a way to overcome this.

It’s Not You, It’s Me

I thought that working with Copilot I can forget about the docs, but it turned out it was my fault for not understanding the basics of PyGame. I did not understand the concept of Rect helping to move objects on the scene. I did not understand how to use utility classes to scale my sprite.

Finding that information in the documentation broke the spell. I was able to suggest a Copilot different strategy separating the sprite represented as a Surface object from the Rect and passing them separately to the screen.

The rest of this course was peace of cake. The more code I've produced, the smarter Copilot got. It was magical when it read my mind and made bullets and aliens move in right the directions. It was a piece of cake to add a score counter that Copilot automatically figured out should add up bullet collisions with invaders.

Copilot is suggesting entire snippets of code

Copilot is suggesting entire snippets of code

After all, it was a success. The game was working and I've made it in under 2 hours learning it all from scratch. Copilot wrote at least 99% of the code and I've barely needed to look into documentation, although without it I would probably still be stuck with the incorrect code served by my new friend.

  • Is Copilot a good way to learn how to use a new programming language or library? It kind of is, but if you think you'll never read documentation ever again, then you are wrong. Knowing at least the basics of PyGame concepts would make my life a lot easier and I would spend a lot less time.

  • Can we use Copilot and not write a single line of code? Rather not. As our own code shapes the way the AI thinks. It really understands a lot so giving it just a little push makes it a pleasure to work with. It will bring a lot more value in helping you expand existing code than building something from scratch.

  • Is Copilot an amazing tool? Definitely. This is the future of development and I'm not saying it will take our jobs because AI will not replace programmers anytime soon, but it will definitely make us more productive. It is a revolution and it was great fun to discover its capabilities this way.

You can watch all my efforts in this 2-hour-long video on YouTube:


No preparation, no edit, no tricks. Pure improvisation.

Just a quick warning - it is emotional. I'm frustrated half the time just to laugh and be totally excited making every little step in the right direction.

You can also find the source code it generated on my Github. As I’ve mentioned in the video it’s not pretty, but it works:


Hope you enjoyed it. Thanks for reading!

react to story with heart
react to story with light
react to story with boat
react to story with money

Related Stories

. . . comments & more!