Listen to this story
Dev, hacker, father of two, wannabe rockstar, internet troll and meme enthusiast...
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.
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:
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!
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.
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.
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.
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.
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:
👉 https://github.com/lukasz-wronski/copilot-invaders
Hope you enjoyed it. Thanks for reading!