VOOZH about

URL: https://www.geeksforgeeks.org/python/create-a-memory-puzzle-game-using-pygame/

⇱ Create a Memory Puzzle Game Using Pygame - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Create a Memory Puzzle Game Using Pygame

Last Updated : 23 Jul, 2025

Playing memory puzzle games provides entertainment and boosts mental abilities such as focus and memory recall. This article guides you through the steps to create a straightforward memory puzzle game using the Python Pygame Library. After completing the article, you will have a fully functioning game where players try to match pairs of game pieces.

Create a Memory Puzzle Game in Python

Below is the implementation of the memory puzzle game in Python:

Create a Virtual Environment

First, create the virtual environment using the below commands

python -m venv env 
.\env\Scripts\activate.ps1

Install Necessary Library

First, install PyGame, Requests, and Pillow libraries for multimedia, HTTP requests, and image processing respectively using the command below.

pip install pygame
pip install requests
pip install pillow

Writing PyGame Code

Here is the code of main.py that we are using to create our game using PyGame. Below is a step-by-step explanation of this code.

Step 1: Library Imports

below code imports essential modules for building a game with Pygame, incorporating functionalities for graphics, randomization, time management, HTTP requests, binary data handling, and image processing.

Step 2: Initialization and Setup

below code initializes the Pygame library, defines constants for screen dimensions, card size, grid size, colors, flip delay, button dimensions, and timer limit. Additionally, it provides a list of image URLs for the game. Finally, it creates a game window with specified dimensions and sets the window's caption to "Memory Puzzle Game" using Pygame functions.

Step 3: Loading and Preprocessing Images

below, code uses the `requests` library to download an image from a specified URL, convert it to PNG format using the `PIL` (Pillow) library, and then load it as a Pygame image. The process is applied to both the card back image and a list of card images obtained from a set of URLs.

Step 4: Game Setup - Shuffling and Duplicating Cards

In below code duplicates the card images to create pairs, shuffles them randomly, and initializes a list (`card_state`) to keep track of the state of each card in the game. Each element of `card_state` corresponds to a card on the grid, where `True` indicates the card is face-up and `False` indicates it's face-down.

Step 5: Game Loop and Input Handling

below code segment represents the main game loop, which continuously checks for user input events, such as quitting the game or clicking the mouse. If the mouse is clicked, the code determines if the click occurred within the restart button's rectangle and, if so, shuffles the cards and resets relevant game variables. The code also handles flipping cards, updating move counts, and rendering the game grid with card images.

Step 6: Drawing Functions

below, code initializes a memory puzzle game using Pygame, defining constants, loading card images from URLs, and creating a game window. It employs a main game loop that handles user input, updates the game state, and continuously renders the grid of cards, move counts, restart button, and timer.

Step 7: Game Logic and End Conditions

Below, code checks for matched pairs when two cards are flipped, introducing a brief delay for visibility. If a pair is found, the counter increments; otherwise, the card states are reset. It verifies if all pairs are matched or if the time limit is reached, displaying corresponding messages and concluding the game.

Complete Code

This is the complete code that we have explained in the above steps that we have used to create memory puzzle game using Pygame.

main.py

Output


Comment
Article Tags: