Memory game in Python
The game of memory, also known as the game of concentration or simply memory, is a classic game that tests a person’s ability to recall information. In this article, we will show you how to build a version of the game using Python and the Flet library.
Getting Started
To begin, we will import the necessary libraries, which include Flet, random, and time.
import flet as ft
import random
import timeNext, we will define the main function which will set the properties of the game window such as the title, height, and width. We will also set the number of single cards to be used in the game.
def main(page: ft.Page):
page.title = "Memo Game"
page.window_height = 710
page.window_width = 910
number_of_single_cards: int = 20Creating the Card Deck
In the game of memory, a deck of cards with matching pairs is used. To create the card deck, we will define a function called make_cards, which takes the number of single cards as a parameter. This function will create a deck of cards with duplicates and shuffle them randomly.
def make_cards(number_of_single_cards):
card_deck_with_duplicates = \
[f"{i}_A" for i in range(number_of_single_cards)] + \
[f"{i}_B" for i in range(number_of_single_cards)]
random.shuffle(card_deck_with_duplicates)
return card_deck_with_duplicates
idx_of_cards = make_cards(number_of_single_cards)Get Data Believer’s stories in your inbox
Join Medium for free to get updates from this writer.
Starting the Game
To start the game, we will define a function called start_game which will be triggered by a start button. This function will disable the start button, set the start time, and create the reverse cards.
def start_game(e):
start_button.disabled = True
global start_time
start_time = time.time()
# make reverse cards
for i in range(len(idx_of_cards)):
row_board.controls[i].content = None
row_board.controls[i].bgcolor = ft.colors.AMBER
row_board.controls[i].border_radius = \
ft.border_radius.all(10)
page.update()Matching Pairs
To match pairs of cards, we will define a function called match_pairs. This function will check if the cards selected by the player match and take appropriate action such as flipping the cards back over or disabling them if they match.
def match_pairs(e):
if len(list_of_pairs) == 2:
return
if start_button.disabled:
num = e.control.data
idx = idx_of_cards[num].split('_')[0]
index_a = idx_of_cards.index(str(idx)+'_A')
index_b = idx_of_cards.index(str(idx)+'_B')
index_of_image_ = idx_of_cards[num].split("_")[0]
if len(list_of_pairs) < 2:
row_board.controls[num].content = ft.Image(src=f"{image_page+'1'+index_of_image_}/100",
border_radius=ft.border_radius.all(10))
if idx_of_cards[num] not in list_of_pairs:
list_of_pairs.append(idx_of_cards[num])
page.update()
if len(list_of_pairs) == 2:
a = int(list_of_pairs[0].split('_')[0])
b = int(list_of_pairs[1].split('_')[0])
if a == b:
time.sleep(.4)
for index in (index_a, index_b):
row_board.controls[index].disabled = True
row_board.controls[index].content = None
row_board.controls[index].bgcolor = ft.colors.BLACK
find.append(index_a)
if len(find) == number_of_single_cards:
stop_time = time.time()
start_button.text = "Time: \n{} \nseconds".format(
str(round(stop_time - start_time, 0)))
page.update()
page.update()
list_of_pairs.clear()
else:
time.sleep(1)
flip_a = idx_of_cards.index(list_of_pairs[0])
flip_b = idx_of_cards.index(list_of_pairs[1])
for index in (flip_a, flip_b):
row_board.controls[index].content = None
row_board.controls[index].bgcolor = ft.colors.AMBER
list_of_pairs.clear()
page.update()
page.update()Creating the Game Board
To create the game board, we will define a function called board. This function will take the list of card indexes as a parameter and create a grid of buttons that represent the cards. Each button will have its corresponding index as data, allowing us to match pairs of cards.
def board(idx_of_cards):
card_images = []
for idx, val in enumerate(idx_of_cards):
card = ft.Button(data=idx,
on_click=match_pairs,
width=120,
height=170)
card_images.append(card)
return ft.Grid(children=card_images,
rows=4,
columns=5,
padding=10)
row_board = board(idx_of_cards)
start_button = ft.Button(text="Start", on_click=start_game)
page.append(start_button, row_board)
ft.start(main)Code on GitHub:
Conclusion
With this code, we have created a basic version of the game of memory using Python and the Flet library. The game can be further enhanced by adding a timer, a scoring system, and different levels of difficulty. The possibilities are endless, and we hope this article has provided a good starting point for building your own version of the game.









