Site icon DataFlair

Create Click-o-Mania Game in Python

python game project click-o-mania

Python course with 57 real-time projects - Learn Python

In this project, we will create Clickomania Game using various modules of Python. Let’s start!!!

What is the Clickomania Game?

This is a cool game for young ones. Players have to click on the gray button and match the same color button and select two squares of the same color. After matching all colors, the player will win the game.

Python Clickomania Game Project Details

In this clickomania project, there are 16 gray boxes set on the gaming window with a score of 25. We have to complete the game in 25 turns only. Here we have built the two main functions one for creating games and one for activation. We can activate the gray button by clicking on it and a particular color box gets activated. We have to check if two square buttons are equal or not. Also, we can reset the game by clicking on the reset button.

Python Clickomania Game Project Prerequisite

The Clickomania Project requires good knowledge of python and the Tkinter library as well as a random module. The Tkinter module provides GUI functionality to our program and binds to the Tk toolkit.

Download Clickomania Game Python Code-

Please download the source code of Python Clickomania Game: Click-o-Mania Game Project

Steps to Build Clickomania Game using Python

  1. Import modules
  2. Initializing the window
  3. Make score and reset buttons
  4. Activate function
  5. Function for creation of game
  6. Function for score updation

Step 1- Importing Modules

#DataFlair-  Import library
import tkinter as tk
import random
from functools import partial

Code Explanation-

Step 2- Initializing the window

#DataFlair -  Initializing the window
window = tk.Tk()
window.geometry("500x500")
window.title('DataFlair - Clickomania game')
 
Nav = tk.Frame(master=window, pady=5, padx=16)
Main = tk.Frame(master=window, pady=16, padx=16)
Footer = tk.Frame(master=window, pady=16, padx=16)
Nav.pack(expand=True)
Main.pack(expand=True)
Footer.pack(expand=True)

Code Explanation-

Step 3- Make score and reset buttons

#button creation
Buttons = []
Score = 25
Match_btn = []
Guesses = 0
 
Label_Score = tk.Label( master=Nav, text=f"Score: {Score}", font=("Times new roman", 30))
Label_Score.pack()
Reset_btn = tk.Button(master=Footer, text="Reset", font=("Times new roman", 30),padx=10, pady=3)
Reset_btn.pack()

Code Explanation-

Step 4- Activate function

def Activate(btn: object, color: str):
    global Match_btn
    global Score
    global Match_btn
    global Label_Score
    global Label_Lost
    global Guesses
 
    if btn['background'] == color:
        btn['background'] = 'grey'
        Match_btn.pop()
 
    else:
        btn['background'] = color
        Match_btn.append(btn)
 
    if len(Match_btn) == 2:
        if Match_btn[0]['background'] == Match_btn[1]['background']:
            Match_btn[0].config(command='')
            Match_btn[1].config(command='')
            Match_btn[0]['text'] = color
            Match_btn[1]['text'] = color
            Guesses += 1
        else:
            Match_btn[0]['background'] = 'grey'
            Match_btn[1]['background'] = 'grey'
            Score -= 1
            Score_Update(Score)
 
            if Score == 0:
                for btn in Buttons:
                    btn.destroy()
 
                Label_Lost.pack(side=tk.TOP, expand=True, fill=tk.BOTH)
 
        if Guesses == 8:
            for btn in Buttons:
                btn.destroy()
 
            Label_Won.pack(side=tk.TOP, expand=True, fill=tk.BOTH)
 
        Match_btn.clear()

Code Explanation-

Step 5- Function for creation of game

def Game():
    global Score
    global Guesses
    global Label_Score
    global Buttons
    global Match_btn
    global Label_Lost
    global Label_Won
    global Nav
 
    Match_btn = []
    Guesses = 0
    Score = 25
    Colors = ['white', 'black', 'red', 'green','blue', 'cyan', 'yellow', 'magenta'] * 2
 
    Score_Update(Score)
 
    if len(Buttons):
        Label_Won.destroy()
        Label_Lost.destroy()
        for btn in Buttons:
            btn.destroy()
 
    Label_Won = tk.Label(master=Main, text="You WON!", font=("Times new roman", 35))
    Label_Lost = tk.Label(master=Main, text="You Lost, try again!",font=("Times new roman", 35),)
 
    for i in range(4):
        for j in range(4):
            Random_Color = random.randint(0, len(Colors)-1)
            color = Colors[Random_Color]
 
            Square_btn = tk.Button(master=Main, text=f"????", width=10, height=3, background='grey', activebackground=color)
            Buttons.append(Square_btn)
            Square_btn.grid(row=i, column=j, padx=10, pady=10)
            Buttons[-1]['command'] = partial(Activate, Square_btn, color)
            Colors.pop(Random_Color)

Code Explanation-

Step 6- Function for score updation and mainloop

def Score_Update(Score: int):
    Label_Score['text'] = f"Score: {Score}"
 
Reset_btn['command'] = Game
Game()
window.mainloop()

Code Explanation-

Python Clickomania Game Output

Summary-

We have successfully created a python Clickomania using a Graphical user Interface (GUI). We have learned about the Tkinter module, random, and the function that module provides. It is really great fun to develop a Clickomania in python using GUI rather than playing, now we will play the clickomania game which we have developed.

Exit mobile version