Have you ever wanted to make a memory game in Python and Pygame? Well if you do then you’re in the right place!
Memory games have long been considered exercise for the brain on top of having numerous benefits such as improvements in concentration, attention, focus and critical thinking skills. So what better way to play a memory game than to build one using Python and Pygame! Plus you’ll learn valuable Python programming skills along the way, so it’s a win-win scenario.
Here is the YouTube video for the first part of this tutorial series where we go into much more depth on how to build this game:
Video for Part 1
Here are some timestamps of the video for your convenience:
- 0:00 Intro to Memory Game
- 0:28 Demo + Game Rules
- 3:18 Coding Global Variables
- 7:52 Coding Helper Functions
Along with all the supporting links for the video:
- Here is the source code for the game, in case you get stuck: https://github.com/ShehanAT/PythonMiniProjects/tree/master/memoryGame
- If you need to download the image files for the gems used in this game, here is the GitHub repo link to do so: https://github.com/ShehanAT/PythonMiniProjects/tree/master/memoryGame/assets
Here is a quick recap of the important sections covered in the video above:
- The Memory Game is build using a 4×3 game board, with the main objective being to match all the sprites to their matching sprite in order to reveal all the covered up boxes in the game board, and in doing so, moving on to the next level
- The next level will contain one extra row & column compared to the previous one
- The 4×3 game board contains 12 boxes, that in turn contain an unique sprite inside them
- The sprite represents the object of memorization. There are 5 unique shapes in 7 different colors so there are 35 unique sprites in total
- The sprite contained inside the box is revealed when the user clicks on the box
- The game lets the user select two boxes, one after the other. If the two selected boxes contain identical sprites then the two boxes remain revealed for the remainder of the game and the game score is incremented by 10. If they don’t match, then both boxes are covered up after 1 second of being revealed to the user.
- The game can be paused by pressing the
ESCkey and terminated by clicking the Quit button in the Pause menu
- The second part of the video explains the logic behind the helper functions for the game. Here is a quick summary of what each function does:
levelUp(): Let’s the game level up when a winning condition has been met. It adds an extra row and column to the game board
draw<Shape>Sprite(): This group of functions are responsible for fetching each shape of specified color from the assets folder and returning them to the caller
Gem(pygame.sprite.Sprite): This class creates a Gem instance representing each sprite on the game board. It contain an image property to represent it’s corresponding sprite image and a draw function, in order to draw that sprite on to the game board
Thanks for following along in this tutorial and if you have any questions or concerns please feel free to post a comment in this article and I will get back to you if I find the time.
Well that’s all for today, I hope you found this article helpful. Thanks so much for reading my article! Feel free to follow me on Twitter and GitHub, connect with me on LinkedIn and subscribe to my YouTube channel.