Vibe Coding the Snake Game with Python and pygame
There are many great computer games. One of the oldest computer games is the Snake game, which came out on various versions of DOS (MS-DOS, PC-DOS, etc). In other words, the game ran in your terminal. You controlled a “snake” that started out very small. You would control the snake as it moved within your terminal. If the snake ran into a block of food, it would get bigger. On the other hand, if the snake ran into any of the borders of the terminal or if the snake ran into itself, the game would end.
Editors Note: This article is a snippet from my upcoming book, Vibe Coding Video Games with Python
Some versions of Snake also included additional levels with walls. If the snake hit any of the walls, it was game over. You kept score by seeing how many pieces of food you were able to eat without dying.
For the purposes of this chapter, you will be using AI to attempt to create the snake game. In this case, you will be using Microsoft’s M365 Copilot Enterprise, but you could use whatever AI you have access to. In fact, it might even be worth the time to try different AI engines to see which one works the best for creating games in Python. Many of the AI tools have a free tier that you can use with this book.
Without further ado, you should start working on the game!
Getting Started
The first step is to open up Copilot and enter a prompt. You can be as detailed or as terse as you like with your AI prompt. For this example, you will try a small prompt:
Prompt: How do I create the snake game using Python and pygame?
Creating a Snake game using Python and Pygame is a fun project! Here’s a step-by-step guide to help you get started:
Step 1: Install Pygame
First, you’ll need to install Pygame. You can do this using pip:
pip install pygame
If you are new to Python, you may not know what this means. How do you run this command? If you have questions, ask the AI to see what it will tell you. In short though, you open a terminal and you can attempt to run this command. If pip is not found, you can ask the AI how to fix that as well.
Step 2: Set Up the Game Window
Create a new Python file and set up the game window:
Step 3: Define the Snake and Food
Create the snake and food objects:
Step 4: Run the Game
Save your file and run it. You should see a window with a snake that you can control using the arrow keys. The snake will grow longer each time it eats the food.
Feel free to customize the game further by adding features such as score tracking, multiple levels, or obstacles. Have fun coding! 🐍
If you have any questions or need further assistance, just let me know!
There are three problems with this code. The first is that there is a SyntaxError buried in this code. Try and find it yourself. If you don’t see the error, run the code and Python will tell you. The second problem is that after fixing the code, when you run your game, you will find that pressing up makes the snake go down and pressing down does nothing at all. Let’s ask the AI how to fix those other two problems as the SyntaxError is simple for you to fix.
Prompt: Pressing up and down does not work correctly. Pressing up sends the snake down and pressing down doesn’t work at all. How do I fix this?
It looks like there’s a small typo in the code that affects the snake’s movement. Let’s fix it!
In the section where you handle the key presses, the elif statement for the UP key has an incorrect assignment. Here’s the corrected code snippet:
Make sure to replace the existing key press handling code with this corrected version. This should ensure that pressing the UP key moves the snake up and pressing the DOWN key moves the snake down correctly.
Give it a try and let me know if it works! 🐍
When you run this code, you will find that if you make the snake hit a wall or itself, the game over message is not centered on the screen correctly. In fact, it is cut off. See below:
You can try asking the AI for help using the following prompt:
The game over screen is not centered. It is cut off on the right. How do I fix that issue?
Great observation! The issue with the game over message being cut off is likely due to the text being rendered starting from the center of the screen without accounting for the width of the text itself.
To fix this, you should calculate the width and height of the rendered text and then center it properly. Here’s how you can modify the message() function:
This uses get_rect() to get the size of the rendered text and then centers it on the screen using the center argument.
Replace your existing message() function with this one, and the game over message should now appear perfectly centered.
Would you like help adding a score display or improving the UI further?
At this point, you can ask the AI for additional help if you want it. For example, you might ask the AI to add a way to keep score or add sound effects.
Here is an example of the running game:
Wrapping Up
The snake game is an old, but challenging computer game. There have been many variations of the game over the years. You were able to create the snake game using artificial intelligence and Python. The Python code is short and easy to understand as well.
Now you can take what you have learned and make the game more robust. Check out the next section for some ideas.
Game Improvement Suggestions
Try to figure out how to add sound effects for when you eat or die. Feel free to use the AI for help if you get stuck.
Add a way to restart the game.
Create a new level with walls. You could do this by limiting the number of items the snake eats. For example, if the snake eats 25 items and survives, you continue to a new, harder level.
Add a score based on the number of items the snake eats.
Add a leaderboard for the top five or ten scores.








Solid breakdown of AI-assisted game dev. The iterative debugging part really highlights how these tools work best with back-and-forth rather than one-shot prompts. Kinda interesting that even the Copilot still has those subtle bugs (like the inverted controls) becuase it shows we're still in that awkward middle phase where AI accelerates prototyping but not necesarily perfection.