Create a Memory Puzzle Game in Python
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Memory Puzzle Game is the best brain exercise as it helps in enhancing the memory and concentration of the player. It is a popular game. Let’s start developing a memory puzzle game in Python and learn some concepts.
About Memory Puzzle Game
In this game we will create 3 levels: easy, medium and hard. At the start of the game the shapes are hidden. The player clicks on the tiles to uncover the tile and when two similar tiles are uncovered the score increases. To complete the game the user has to uncover all the pairs of shapes. After completion of the game, the number of moves required to complete the game is also displayed on the screen.
Python Memory Puzzle Game Project
The main objective of the project is to develop a Memory Puzzle Game that has 3 different levels. Installation of tkinter is a must to start developing the project.
Project Prerequisites
Basic concepts of python and tkinter are required to get ahead with the project.
Download Memory Puzzle Game
Please download the source code of python memory puzzle game: Memory Puzzle Game Project
Project File Structure
1. Installing Tkinter
2. Importing Modules and initializing tkinter window
3. Various functions required for creating easy level
4. Various functions required for creating medium level
5. Various functions required for creating hard level
1. Installing Tkinter
Installation of tkinter is a must to start the project. Tkinter is a module that provides the most easy way to develop a graphical user interface. To install tkinter write the command mentioned below on your terminal window or cmd.
pip install tkinter
2. Importing Modules and initializing tkinter window
from tkinter import * import random from tkinter import ttk PuzzleWindow=Tk() PuzzleWindow.title('Memory Puzzle Game By DataFlair') tabs = ttk.Notebook(PuzzleWindow) easy= ttk.Frame(tabs)
Code Explanation:
a. random: random helps in generating random numbers.
b. Tk(): It provides a library of basic elements of gui widgets.
c. title(): It helps in setting the title of the screen.
d. ttk.Notebook(): This widget manages the collection of windows and displays one window at a time.
e. ttk.Frame(): It is basically a rectangular container for other widgets.
3. Various functions required for creating easy level
def draw(a,l,m): global base1 if a=='A': d=base1.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='red') elif a=='B': d=base1.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='yellow') elif a=='C': d=base1.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='blue') elif a=='D': d=base1.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='red') elif a=='E': d=base1.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='yellow') elif a=='F': d=base1.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='blue') elif a=='G': d=base1.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='red') elif a=='H': d=base1.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='green') def quizboard(): global base1,ans1,board1,moves1 count=0 for i in range(4): for j in range(4): rec=base1.create_rectangle(100*i,j*100,100*i+100,100*j+100,fill="white") if(board1[i][j]!='.'): draw(board1[i][j],i,j) count+=1 if count==16: base1.create_text(200,450,text="No. of moves: "+str(moves1),font=('arial',20)) def call(event): global base1,ans1,board1,moves1,prev1 i=event.x//100 j=event.y//100 if board1[i][j]!='.': return moves1+=1 #print(moves) if(prev1[0]>4): prev1[0]=i prev1[1]=j board1[i][j]=ans1[i][j] quizboard() else: board1[i][j]=ans1[i][j] quizboard() if(ans1[i][j]==board1[prev1[0]][prev1[1]]): print("matched") prev1=[100,100] quizboard() return else: board1[prev1[0]][prev1[1]]='.' quizboard() prev1=[i,j] return base1=Canvas(easy,width=500,height=500) base1.pack() ans1 = list('AABBCCDDEEFFGGHH') random.shuffle(ans1) ans1 = [ans1[:4], ans1[4:8], ans1[8:12], ans1[12:]] base1.bind("<Button-1>", call) moves1=IntVar() moves1=0 prev1=[100,100] board1=[list('.'*4) for count in range(4)] quizboard()
Code Explanation:
a. draw(): This function helps in drawing different figures that are hidden under tiles.
b. create_rectangle(): It helps in drawing a rectangle on the screen.
c. create_polygon():It helps in drawing a polygon on the screen.
d. create_oval():It helps in drawing an oval on the screen.
e. quizboard: It helps in developing the board for the easy level which is a 4×4 box.
f. call(): This function is called when the player clicks on the tile. This function displays the figures hidden in the tile if the two figures are the same and hides the figure if the other figure that is selected is not the same.
g. Canvas: It adds structured graphics in the python applications.
h. List: It is used to store a list of numbers, string, characters etc.
i. shuffle(): It takes a list and then reorganizes the elements in the list.
4. Various function required for creating medium level
window2= ttk.Frame(tabs) def draw1(a,l,m): global base2 if a=='A': d=base2.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='red') elif a=='B': d=base2.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='yellow') elif a=='C': d=base2.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='blue') elif a=='D': d=base2.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='red') elif a=='E': d=base2.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='yellow') elif a=='F': d=base2.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='blue') elif a=='G': d=base2.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='red') elif a=='H': d=base2.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='green') elif a=='I': d=base2.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='yellow') elif a=='J': d=base2.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='blue') elif a=='K': d=base2.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='black') elif a=='L': d=base2.create_polygon(100*l+50,m*100+20,100*l+20,100*m+100-20,100*l+100-20,100*m+100-20,fill='orange') elif a=='M': d=base2.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='black') elif a=='N': d=base2.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='orange') elif a=='O': d=base2.create_rectangle(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='green') elif a=='P': d=base2.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='black') elif a=='Q': d=base2.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='orange') elif a=='R': d=base2.create_oval(100*l+20,m*100+20,100*l+100-20,100*m+100-20,fill='green') def puzzleboard2(): global base2,ans2,board2,moves2 count=0 for i in range(6): for j in range(6): rec=base2.create_rectangle(100*i,j*100,100*i+100,100*j+100,fill="white") if(board2[i][j]!='.'): draw1(board2[i][j],i,j) count+=1 if count>=36: base2.create_text(300,650,text="No. of moves: "+str(moves2),font=('arial',20)) def call2(event): global base2,ans2,board2,moves2,prev2 i=event.x//100 j=event.y//100 if board2[i][j]!='.': return moves2+=1 if(prev2[0]>6): prev2[0]=i prev2[1]=j board2[i][j]=ans2[i][j] puzzleboard2() else: board2[i][j]=ans2[i][j] puzzleboard2() if(ans2[i][j]==board2[prev2[0]][prev2[1]]): prev2=[100,100] puzzleboard2() return else: board2[prev2[0]][prev2[1]]='.' puzzleboard2() prev2=[i,j] return base2=Canvas(window2,width=1000,height=1000) base2.pack() ans2 = list('AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRR') random.shuffle(ans2) ans2 = [ans2[:6], ans2[6:12], ans2[12:18], ans2[18:24], ans2[24:30], ans2[30:] ] base2.bind("<Button-1>", call2) moves2=IntVar() moves2=0 prev2=[100,100] board2=[list('.'*6) for count in range(6)] puzzleboard2()
Code Explanation:
a. draw1(): This function helps in drawing different figures that are hidden under tiles.
b. quizboard2(): It helps in developing the board for the medium level which is a 6×6 box.
c. Create_text: It can be applied to a canvas object which helps in writing text on it.
d. call2():This function is called when the player clicks on the tile. This function displays the figures hidden in the tile if the two figures are the same and hides the figure if the other figure that is selected is not the same.
e. IntVar: It returns the value of a variable as an integer.
5. Various function required for creating hard level
window3= ttk.Frame(tabs) tabs.add(easy, text ='Easy') tabs.add(window2, text ='medium') tabs.add(window3, text ='Hard') tabs.pack(expand = 1, fill ="both") def draw2(a,l,m): global base3 if a=='A': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='red') elif a=='B': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='yellow') elif a=='C': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='blue') elif a=='D': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='red') elif a=='E': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='yellow') elif a=='F': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='blue') elif a=='G': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='red') elif a=='H': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='green') elif a=='I': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='yellow') elif a=='J': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='blue') elif a=='K': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='black') elif a=='L': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='orange') elif a=='M': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='black') elif a=='N': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='orange') elif a=='O': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='green') elif a=='P': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='pink') elif a=='Q': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='green') elif a=='R': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='pink') elif a=='S': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='purple') elif a=='T': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='purple') elif a=='U': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='purple') elif a=='V': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='pink') elif a=='W': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='maroon') elif a=='X': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='maroon') elif a=='Y': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='maroon') elif a=='Z': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='brown') elif a=='a': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='brown') elif a=='b': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='brown') elif a=='c': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='aqua') elif a=='d': d=base3.create_rectangle(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='aqua') elif a=='e': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='aqua') elif a=='f': d=base3.create_polygon(80*l+50,m*80+20,80*l+20,80*m+80-20,80*l+80-20,80*m+80-20,fill='magenta') elif a=='g': d=base3.create_oval(80*l+20,m*80+20,80*l+80-20,80*m+80-20,fill='magenta') def quizboard3(): global base3,ans3,board3,moves3 count=0 for i in range(8): for j in range(8): e=base3.create_rectangle(80*i,j*80,80*i+80,80*j+80,fill="white") if(board3[i][j]!='.'): draw2(board3[i][j],i,j) count+=1 if count>=64: base3.create_text(300,650,text="No. of moves: "+str(moves3),font=('arial',20)) def call3(event): global base3,ans3,board3,moves3,prev3 i=event.x//80 j=event.y//80 if board3[i][j]!='.': return moves3+=1 if(prev3[0]>8): prev3[0]=i prev3[1]=j board3[i][j]=ans3[i][j] quizboard3() else: board3[i][j]=ans3[i][j] quizboard3() if(ans3[i][j]==board3[prev3[0]][prev3[1]]): print("matched") prev3=[100,100] quizboard3() return else: board3[prev3[0]][prev3[1]]='.' quizboard3() prev3=[i,j] return base3=Canvas(window3,width=1000,height=1000) base3.pack() ans3 = list('AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUWWXXYYZZaabbccddeeffgg') random.shuffle(ans3) ans3 = [ans3[:8], ans3[8:16], ans3[16:24], ans3[24:32], ans3[32:40], ans3[40:48], ans3[48:56], ans3[56:] ] base3.bind("<Button-1>", call3) moves3=IntVar() moves3=0 prev3=[80,80] board3=[list('.'*8) for count in range(8)] quizboard3() mainloop()
Code Explanation:
a. draw2(): This function helps in drawing different figures that are hidden under tiles.
b. quizboard3(): It helps in developing the board for the medium level which is a 8×8 box.
c. call3():This function is called when the player clicks on the tile. This function displays the figures hidden in the tile if the two figures are the same and hides the figure if the other figure that is selected is not the same.
d. shuffle(): It takes a list and then reorganizes the elements in the list.
Python Memory Puzzle Game Output
Summary:
We have successfully developed a memory puzzle game with the knowledge of tkinter and python.
Your opinion matters
Please write your valuable feedback about DataFlair on Google
How to add keyboard arrow keys for activation in keys
why Mysql is not used to store the records of players score??
Creating a memory puzzle game in python language is easy, and the game itself doesn’t demand significant resources, making it unnecessary to utilize a database like MySQL to store player scores.
DataFlair you are the best online coding institution making learning to code so easy.