Python Click-O-Mania – Click Faster, Win Bigger!
Players must match two squares of the same color by clicking on the gray button and matching the same color button. The game is won when the player matches all of the colors.
About Python Click-O-Mania Game
There are 16 gray boxes with a score of 25 in this clickomania project. We only have 25 turns to complete the game. We’ve constructed two major functionalities here: one for developing games and one for activation. By clicking on the gray button, we can activate a specific color box. We must determine whether two square buttons are equivalent. .
Prerequisites for Click-O-Mania Game using Python
- Basic knowledge of the Python programming language and how functions are defined in it.
- How the GUI window is made using Tkinter
- How to work with the Random library
Download Python Click-O-Mania Game Project
Please download the source code of Python Click-O-Mania Game Project from the following link: Python Click-O-Mania Game Project Code
Steps to Create Click-O-Mania Game using Python
Following are the steps for developing the Python Click-O-Mania Game Project:
Step 1: Importing the necessary modules
Step 2: Making a window for our project
Step 3: Functions
Step 4: The main game loop
Step 1: Importing the necessary modules
To use Tkinter, we need to import the Tkinter module to make our GUI window. We are also going to import the random module.
Code
# import library from tkinter import * import random from functools import partial
Step 2: Making a window for our project
This code sets the title of the window as ‘TechVidvan’, and sets its dimensions and background color.
Code
# creating display display = Tk() display.geometry("800x750") display.title('TechVidvan') display.configure(bg='#fff8e9')
Step 3: Functions
- Make a button variable and assign an empty list to it.
- match_it- Function for activating color buttons and determining whether two squares are equivalent.
- .pop() is a built-in Python method that removes and returns the last value from a list or pops the first element from a list.
- .append() adds a single item to an existing list.
- If the match button’s length is equal to 2, it will match with the second background color; otherwise, it will remain gray.
- In the play() function, we make a list of all the colors and save it in the colors variable. Because we want each color twice, multiply by 2, and we make two labels, one for won and one for lost, and assign text and font to both labels. Buttons will be randomly positioned on the game window for each new game color.
- Show_score to update the score board.
Code
# button creation buttons_list = [] score = 25 color_match = [] guess = 0 def match_it(btn: object, color: str): global lost global guess global score global score_L global color_match if btn['background'] == color: btn['background'] = 'white' color_match.pop() else: btn['background'] = color color_match.append(btn) if len(color_match) == 2: if color_match[0]['background'] == color_match[1]['background']: color_match[0].config(command='') color_match[1].config(command='') color_match[0]['text'] = color color_match[1]['text'] = color guess += 1 else: color_match[0]['background'] = 'grey' color_match[1]['background'] = 'grey' score -= 1 show_score(score) if score == 0: for btn in buttons_list: btn.destroy() lost.pack(side=TOP, expand=True, fill=BOTH) if guess == 7: for btn in buttons_list: btn.destroy() won.pack(side=TOP, expand=True, fill=BOTH) color_match.clear() def show_score(game_score: int): score_L['text'] = f"Score: {game_score}" def play(): global score global guess global lost global won global frame1 global score_L global buttons_list global color_match color_match = [] guess = 0 score = 25 colors = ['purple', 'gold', 'red', 'green', 'lightblue', 'orange', 'yellow', 'brown'] * 2 show_score(score) if len(buttons_list): won.destroy() lost.destroy() for btn in buttons_list: btn.destroy() won = Label(master=frame3, text="Congrats, You WON!", font=("calibre", 40)) lost = Label(master=frame3, text="Lost, Better Luck Next Time :)", font=("calibre", 40), ) for j in range(4): for k in range(4): color_random = random.randint(0, len(colors) - 1) color = colors[color_random] button1 = Button(master=frame3, text=f"!!", width=12, height=5, background='grey', activebackground=color) buttons_list.append(button1) button1.grid(row=j, column=k, padx=20, pady=20) buttons_list[-1]['command'] = partial(match_it, button1, color) colors.pop(color_random)
Step 4: The main game loop
We create three frames: one to display the heading to our window Clickomania, a second to display the score and reset button, and a third for color buttons, and we call the play() function.
Code
frame1 = Frame(master=display, pady=5, padx=16, bg="#fff8e9") frame1.pack(expand=True) Label(master=frame1, text="Clickomania", font="calibre 32 bold", fg='#5a9a6d', bg="#fff8e9").pack() frame2 = Frame(master=display, pady=5, padx=20, bg="#fff8e9") frame2.pack(expand=True) score_L = Label(master=frame2, text=f"Score: {score}", font='calibre 25 bold', fg='#78a44e', bg="#fff8e9") score_L.pack(side=LEFT, padx=80) Button(master=frame2, text="Reset", command=play, fg='white', bg="#9eb9bf", font="calibre 20 bold", padx=10, pady=3).pack(side=RIGHT,padx=80) frame3 = Frame(master=display, pady=5, padx=20, bg="#fff8e9") frame3.pack(expand=True) play() display.mainloop()
Full Code
# import library from tkinter import * import random from functools import partial # creating display display = Tk() display.geometry("800x750") display.title('TechVidvan') display.configure(bg='#fff8e9') # button creation buttons_list = [] score = 25 color_match = [] guess = 0 def match_it(btn: object, color: str): global lost global guess global score global score_L global color_match if btn['background'] == color: btn['background'] = 'white' color_match.pop() else: btn['background'] = color color_match.append(btn) if len(color_match) == 2: if color_match[0]['background'] == color_match[1]['background']: color_match[0].config(command='') color_match[1].config(command='') color_match[0]['text'] = color color_match[1]['text'] = color guess += 1 else: color_match[0]['background'] = 'grey' color_match[1]['background'] = 'grey' score -= 1 show_score(score) if score == 0: for btn in buttons_list: btn.destroy() lost.pack(side=TOP, expand=True, fill=BOTH) if guess == 7: for btn in buttons_list: btn.destroy() won.pack(side=TOP, expand=True, fill=BOTH) color_match.clear() def show_score(game_score: int): score_L['text'] = f"Score: {game_score}" def play(): global score global guess global lost global won global frame1 global score_L global buttons_list global color_match color_match = [] guess = 0 score = 25 colors = ['purple', 'gold', 'red', 'green', 'lightblue', 'orange', 'yellow', 'brown'] * 2 show_score(score) if len(buttons_list): won.destroy() lost.destroy() for btn in buttons_list: btn.destroy() won = Label(master=frame3, text="Congrats, You WON!", font=("calibre", 40)) lost = Label(master=frame3, text="Lost, Better Luck Next Time :)", font=("calibre", 40), ) for j in range(4): for k in range(4): color_random = random.randint(0, len(colors) - 1) color = colors[color_random] button1 = Button(master=frame3, text=f"!!", width=12, height=5, background='grey', activebackground=color) buttons_list.append(button1) button1.grid(row=j, column=k, padx=20, pady=20) buttons_list[-1]['command'] = partial(match_it, button1, color) colors.pop(color_random) frame1 = Frame(master=display, pady=5, padx=16, bg="#fff8e9") frame1.pack(expand=True) Label(master=frame1, text="Clickomania", font="calibre 32 bold", fg='#5a9a6d', bg="#fff8e9").pack() frame2 = Frame(master=display, pady=5, padx=20, bg="#fff8e9") frame2.pack(expand=True) score_L = Label(master=frame2, text=f"Score: {score}", font='calibre 25 bold', fg='#78a44e', bg="#fff8e9") score_L.pack(side=LEFT, padx=80) Button(master=frame2, text="Reset", command=play, fg='white', bg="#9eb9bf", font="calibre 20 bold", padx=10, pady=3).pack(side=RIGHT,padx=80) frame3 = Frame(master=display, pady=5, padx=20, bg="#fff8e9") frame3.pack(expand=True) play() display.mainloop()
Python Click-O-Mania Game Output
Summary
We successfully developed a Python Clickomania with a Graphical User Interface. We’ve learnt about the Tkinter module, random, and the functions it offers. I hope you had fun building with us.