The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a memory game using Python

Here’s a simple implementation of a memory game in Python using the tkinter library for the graphical user interface (GUI). In this game, the user will match pairs of cards that are shuffled and placed face down.

Python Code: Memory Game

python
import tkinter as tk import random import time # Create the main window root = tk.Tk() root.title("Memory Game") # Define the size of the grid grid_size = 4 cards = [] # Define the possible card values card_values = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] # Create a list of pairs of card values cards = card_values * 2 # Two copies of each value random.shuffle(cards) # Shuffle the cards randomly # Create a dictionary to track the state of each card (facedown or faceup) card_state = {i: 'facedown' for i in range(grid_size * grid_size)} card_button = {} # Function to handle when a card is clicked def card_click(index): global first_card, second_card, first_index, second_index if card_state[index] == 'facedown': card_state[index] = 'faceup' button = card_button[index] button.config(text=cards[index], state=tk.DISABLED) # If this is the first card clicked if first_card is None: first_card = cards[index] first_index = index else: second_card = cards[index] second_index = index # Check if the two cards match if first_card == second_card: card_state[first_index] = 'matched' card_state[second_index] = 'matched' else: # If the cards do not match, flip them back after a short delay root.after(1000, flip_back, first_index, second_index) first_card = None second_card = None def flip_back(first_index, second_index): # Flip the cards back over card_state[first_index] = 'facedown' card_state[second_index] = 'facedown' card_button[first_index].config(text='', state=tk.NORMAL) card_button[second_index].config(text='', state=tk.NORMAL) def create_game_board(): for i in range(grid_size): for j in range(grid_size): index = i * grid_size + j button = tk.Button(root, width=10, height=3, command=lambda index=index: card_click(index)) button.grid(row=i, column=j) card_button[index] = button first_card = None second_card = None first_index = None second_index = None create_game_board() root.mainloop()

Explanation:

  1. Grid and Card Setup:

    • A 4×4 grid is created with 16 buttons, each representing a card.

    • The card values are randomly shuffled, and each card value appears twice (to form pairs).

  2. Card Clicking Mechanism:

    • When a card is clicked, its value is revealed and displayed in the corresponding button.

    • If two cards are revealed and match, they stay face-up. If they don’t match, after a short delay, they flip back over.

  3. Game Logic:

    • The game keeps track of which cards are face-up, face-down, or matched.

    • It also ensures that only two cards are face-up at any time.

  4. GUI:

    • The game is created using tkinter, and the buttons dynamically show the card values.

    • The game will continue running in a window until all pairs are matched.

To run this game, make sure you have tkinter installed (it usually comes with Python by default). Just copy the code into a Python file and execute it to play!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About