Color Game Using Tkinter in Python

Color game is a iq test game that tests players’ ability to recognize a text’s color based on its name instead of the color it is written in.

The Python Tkinter package, which offers a user-friendly Graphical User Interface (GUI) toolkit, can be used to construct the game.

First, we must construct a window, establish its title, and import the Tkinter library.

from tkinter import *
import random

The next step is to create a label, set its font, size, and position, and then show the color name:

def set_color():
    color_label.config(text=random.choice(colors), fg=random.choice(colors))

We also need to design a label that will show the player’s score:

def check_answer(answer):
    if answer == color_label['fg']:
        score = int(score_label['text'].split()[1]) + 1
        score_label.config(text="Score: " + str(score))
    set_color()

To start the game, we need to create a color names list and a function that will randomly choose a color name and set the text of the color label to that name:

# List of color names
colors = ["red", "blue", "green", "yellow", "orange", "purple", "pink", "brown"]
# Create buttons for each color
button_row = 2
button_column = 0
for color in colors:
    button = Button(game_frame, text=color.capitalize(), font=("Helvetica", 18), width=10, command=lambda c=color: check_answer(c))
    button.grid(row=button_row, column=button_column, padx=10, pady=5)
    button_column += 1
    if button_column > 1:
        button_column = 0
        button_row += 1

Each color’s button uses the ‘Button()’ function. The width property is used to set the width of the button, and the ‘capitalize ()’ function is used to capitalize the initial letter of the color name. The check ‘answer()’ function is connected to the button using a lambda function by the command parameter. The lambda function passes the color name as a parameter to the check answer() method.

In order to begin the game, we must call the set color() function:

# Start the game
set_color()

root.mainloop()

The GUI is operated by, and events are handled by the mainloop() function. Before entering the main loop and waiting for user input, the game first calls the set_color() function to set the color label with a random color name and text color. The corresponding color name is supplied as a parameter to the check_answer() function when a user clicks on a color button, which then verifies the accuracy of the response and modifies the score label.

Complete Python Tkinter code for the Color game is as follows:

Code

from tkinter import *
import random

def set_color():
    color_label.config(text=random.choice(colors), fg=random.choice(colors))

def check_answer(answer):
    if answer == color_label['fg']:
        score = int(score_label['text'].split()[1]) + 1
        score_label.config(text="Score: " + str(score))
    set_color()

root = Tk()
root.title("Color Game")

# Create a frame for the game
game_frame = Frame(root, padx=50, pady=50)
game_frame.pack()

# Create a label to display the color name
color_label = Label(game_frame, text="", font=("Helvetica", 36))
color_label.grid(row=0, column=0, columnspan=2, pady=10)

# Create a label to display the score
score_label = Label(game_frame, text="Score: 0", font=("Helvetica", 24))
score_label.grid(row=1, column=0, columnspan=2, pady=10)

# List of color names
colors = ["red", "blue", "green", "yellow", "orange", "purple", "pink", "brown"]

# Create buttons for each color
button_row = 2
button_column = 0
for color in colors:
    button = Button(game_frame, text=color.capitalize(), font=("Helvetica", 18), width=10, command=lambda c=color: check_answer(c))
    button.grid(row=button_row, column=button_column, padx=10, pady=5)
    button_column += 1
    if button_column > 1:
        button_column = 0
        button_row += 1

# Start the game
set_color()

root.mainloop()

Output

Color game using python pdf
Color game using python github
Color game using python example
project report on color game python
color game using tkinter in python ppt
python tkinter game source code
hangman game in python using tkinter
tkinter games

Download the Color game using Python pdf

In this game, players can test their attention and ability to resist distractions by focusing on the color of the text rather than the meaning of the text. The game is simple to learn, and it can be fun to play and a method to push yourself intellectually and enhance your abilities.