We are going to show you how to make a simple Python game called Same. Same consists of different coloured balls arranged on a board.
Balls of the same colour are joined together by connecting squares, and together they form a shape. When any part of this shape is clicked, it disappears and the balls above take the place of the now removed balls.
We are going to be coding all of this from scratch using Pygame. By the end of this tutorial, you should be able to make your own modifications and make this Python game truly your own. How cool is that?
Create a Python game: Same Game code
- Logic.py
- Gui.py
- Download from GitHub
Code structure of Same Game
The code for this project is quite large, so we’ll assume you know how to do simple things in Pygame, such as draw a circle. What we’ll focus on instead is how you should think when you are tackling a program project similar to this. Before we begin, get the code.To start a game:
python Gui.py
The first thing you want to do with a project like this is to think through what you want to do. As your programs begin to get more complex, the more important this step becomes. We will be making use of the model-view-controller (MVC) pattern to structure our program. The model refers to permanent data, the controller is where the main stuff happens, and the view is what the user sees. All our controller code is going to be found in the logic.py file (used for scoring, the internal representation of the board, and other functions for tasks like removing balls and finding shapes).
The Gui.py file contains all the code that is related to the view. It has code to draw balls and connecting squares on the screen, based on the internal representation of the board found in the logic.py file. It also has code to draw the scoreboard, and deals with events as they happen. For the model, we have included it in the logic.py file. The only thing permanently saved between different games is the high score. This is stored in the TopScores.txt file. We access this data with getHighScore() and update the high score if we need to with updateHighScore(). These functions are found in the logic.py file, but they belong to the model.
Create a Python game: PGame logic in Same
Now we have an idea of how our code is going to be structured, let’s discuss some implementation details. We represent the board as a two-dimensional list of balls called, surprisingly, balls. This makes it easy to access the ball at position x,y with balls[x][y].
Another critical task we need to be able to do is to group similarly coloured balls into a shape. This is done by checking up, down, left, and right to see whether the balls at those positions are of the same colour (the _adjacent() function does this ). _findAdjacent() is then used to find all the balls that belong to a shape. The _markBalls() function is used to mark a shape for deletion, while _clearBalls() deletes the shape. The _findAdjacent() function is also used to calculate the number of balls that were removed and hence is used to calculate the score in getScore().
Displaying the board in Same Game
Now we have created logic.py, we can now develop the code that draws things on the screen and responds to clicks. This is where Pygame does its magic. All the code that displays stuff can be found in _display(). It makes use of a function to draw the square of different colours. It draws the board and also draws the connecting squares.
Create a game in Python: Reacting to events
Besides displaying objects on the screen, our game needs to react to events. We need to know which ball was clicked, so we use _getPosition(); this returns the x,y position if a ball was clicked, and None if the click wasn’t on a ball. After we have identified which ball was clicked, we remove all the balls belonging to that ball’s shape. This is done with the _removeBalls() function in logic.py. The event-handling loop is in the run() function. This updates the board frequently after any event has occurred.
Permanent game data
The only thing that is being permanently remembered by our game is the high score. This has been implemented using a file. Another robust approach would be to use an SQLite database to store several high scores.
Create a Python game: Releasing to the world
Our game is working now, but there are still some finishing touches we are going to add. We want to be able to share our game with our friends as easily as possible. One way to do that would be to create an executable that contains our game, and then any player just needs to click on the executable to play the game. No extra downloads! To do this, we need to install pyinstaller. This is achieved by entering the following command:
pip install pyinstaller
To create our package in the Terminal and in the same directory as our game, we run.
pyinstaller Gui.spec --windowed –onefile
Voila! We have an executable we can share with all our friends. Pyinstaller can create executables for other operating systems, too, so nobody misses out!
Same Game was developed by Opemipo Ogunkola. He is interested in hardware and software, Ope codes in Python, C++, C, and knows a bit of web development.