Best Python Projects for Kids: Memory Game in Python

In this lesson, we are going to be creating a fun memory game using Turtle and Python. Whether you’re new to Python or looking for a fun project to try, this project is great for anyone.

Table Of Content:

Who is this Project For?

This is an intermediate-level project for those who are new to Python. Before beginning, you should already have experience with Turtle. You should also understand key concepts such as creating functions and variables, using operators and booleans, and creating loops. These will be the core concepts used in this project. Better for students who completed Python Level 2.

What Will We Learn?

This project focuses on using knowledge of Turtle Graphics, python operators, booleans, and functions. By the end of the project, you will be able to use these concepts to create a fun and functional game.

Features to Consider:

  • The game screen should have an 8x8 grid of squares
  • There should be an image of some sort behind the grid, which is revealed when the tiles are matched
  • When a player clicks on one tile, its number will show but will hide when another tile is selected.
  • When the player clicks two tiles in a row that match, the tiles will disappear, showing part of the image.

Main Steps:

This project has 6 main steps:  

  1. General setup
  2. Setup main game components
  3. Define and setup grid squares
  4. Define main game functions
  5. Create draw function
  6. Use functions and state final game commands

Step 1: General Setup

The first thing we’ll need to do is set up all the libraries and modules we’re going to use. For this project, we’ll be using turtle, random, and freegames. To do this we’ll type:

from random import * 
from turtle import * 
from freegames import path

Step 2: Setup Main Game Components

Once we've imported all the libraries and modules we need, we'll set up the game's main components. We will need an image for the background (In this case I’ve used a car). We will also need to set up our tile numbers and states and add the code that will hide certain elements when needed. Let’s begin by coding the car image and the tiles:

car = path('car.gif') 
tiles = list(range(32)) * 2

Hint: We’ve created a list of 31 numbers from 0-31, and then multiplied it by 2 to make two of each tile

Next, we’ll add the code to set the make to None, and set hide to true. By the end of this step, your code will look like this:

state = {'mark': None} 
hide = [True] * 64 print(tiles)

Hint: We’ve set ‘mark’ to none. None is a null value, it is not 0 or false, it is a data type of its own.

Step 3: Define and Setup Grid Squares

Now that we’re done with the basic setup, we can start defining the game’s main functions. To do this, we’ll start by creating the tiles. We will create a function called ‘square’ with both x and y coordinates. This will let us decide where to place the tiles. To do this, we’ll type:

def square(x, y): 
  goto(x, y)
  color('black', 'white')
  for count in range(4):

Hint: We’ve used a for loop to draw our tiles because it makes the code shorter and more efficient.

Now that we’re done with our tiles, we can move on to defining the functions that will make up the main game.

Step 4: Define Main Game Functions

Now that the setup is complete, it’s time to create 3 of our main functions:

  • Index: this function will convert the x and y-coordinates to the tile index
  • XY: this function will convert the tile count to x and y-coordinates
  • Tap: this function will hide or show the tiles based on which ones are clicked

To create our index and XY functions, we’ll type:

def index(x, y):
  return int((x + 200) // 50 + ((y + 200) // 50) * 8)
def xy(count):
  return (count % 8) * 50 - 200, (count // 8) * 50 - 200

Hint: These two functions will allow us to create a grid over the image and keep track of which ones have been clicked.

Next, we’ll create our tap function. This will hide or show the tiles and their marks depending on when has been clicked. In the end, our code will look like this:

def tap(x, y):
 spot = index(x, y)
 mark = state['mark']
 if mark is None or mark == spot or tiles[mark] != tiles[spot]:
   state['mark'] = spot
   hide[spot] = False
   hide[mark] = False
   state['mark'] = None

Step 5: Create Draw Function

Now that we’ve finished with the index, xy, and tap functions, we only have one left. We’ll be creating our Draw function, which will draw the image and tiles, and also has the logic part of the game.

The function is made up of 4 main components:

  • Clear the canvas and draw the car image
  • For loop to draw squares
  • If statement to manage the text lapels
  • Update and timer functions to continually refresh the game

We’ll start out by typing:

def draw():
  goto(0, 0)
  for count in range(64):
    if hide[count]:
      x, y = xy(count)
      square(x, y)
  mark = state['mark']

Hint: Notice how we’ve used a for loop before the if-statement to more efficiently manage the statement and to make sure all the tiles are checked.

Now we only have to make the second if-statement to manage the labels on the tiles and create the code to update the game. We’ll be using the update() and ontimer() functions to ensure that the game is constantly updating. In the end the code will look like this:

if mark is not None and hide[mark]:
  x, y = xy(mark)
  goto(x + 2, y)
  write(tiles[mark], font=('Arial', 30, 'normal'))
ontimer(draw, 100)

Step 6: Create Final Game Commands

Finally, we need to add a few more lines of code to shuffle the tiles, draw the car image, and manage what will happen when the player clicks on a tile. To do this we’ll type:


Project Complete!

And now we’re done! Feel free to test your code and see how it works. If you’re stuck or have any issues with your code, try reviewing it to see if you’ve made any mistakes, or check out the code mentioned above as a reference.

Geek Team

Kelsey is a Grade 10 student in Ontario, Canada. She completed Python, Java, and C++ at Geekedu. She is now focused on preparing for the USACO Silver Competition. She dreams of studying at MIT and is dedicated to turning that dream into reality step-by-step. Kelsey loves to write tutorials as a way to share her learning experience with Geekedu.

Sign up and get a 60-minute free assessment class

Book A FREE Trial