Home » Color Game Using Tkinter Python

Color Game Using Tkinter Python

color game python tkinter project

In this article, we are going to build a color game in Tkinter python. If you don’t know what this color game is then let me explain. Color game is simple and easy to play. 

In this game, different color names will be displayed with different text colors. Here in this game, the role of a player is to enter the correct color of the word which is displayed on the screen. Each time when the player enters the correct color the score will be incremented by one. And the game duration will be 60 seconds.

This is what we are going to build using python Tkinter GUI. You can see the output of this project in this video.

Implementing Color Game using Tkinter Python

To implement the user interface I wrote the following code.

from tkinter import *
import tkinter.font as font
import random

colors = ["Red", "Orange", "White", "Black", "Green", "Blue", "Brown", "Purple", "Cyan", "Yellow", "Pink", "Magenta"]

timer = 60
score = 0
displayed_word_color = ''

my_window = Tk()
my_window.title("Color Game")
my_window.geometry("500x200")

app_font = font.Font(family='Helvetica', size = 12)

game_desp = "Game Description: Enter the color of the words displayed below. \n And Keep in mind not to enter the word text itself"
myFont = font.Font(family='Helvetica')

game_description = Label(my_window, text = game_desp, font = app_font, fg= "grey")
game_description.pack()

game_score = Label(my_window, text = "Your Score : " + str(score), font = (font.Font(size=16)), fg = "green")
game_score.pack()

display_words = Label(my_window , font = (font.Font(size=28)), pady = 10)
display_words.pack()

time_left = Label(my_window, text = "Game Ends in : -", font = (font.Font(size=14)), fg = "orange")
time_left.pack()

color_entry = Entry(my_window, width = 30)
color_entry.pack(pady = 10)

btn_frame = Frame(my_window, width= 80, height = 40, bg= 'red')
btn_frame.pack(side = BOTTOM)

start_button = Button(btn_frame, text = "Start", width = 20, fg = "black", bg = "pink", bd = 0,padx = 20, pady = 10 , command = startGame)
start_button.grid(row=0, column= 0)

reset_button = Button(btn_frame, text = "Reset", width = 20, fg = "black", bg = "light blue", bd = 0,padx = 20, pady = 10 , command = resetGame)
reset_button.grid(row=0, column= 1)

my_window.geometry('600x300')
my_window.mainloop()

The above code will create a window inside which I have placed some description of the game and how to play the game. Then I have placed some labels which display the current score, words with different colors and count down timer. And entry box to enter the color name. Two buttons are placed at the bottom of the window namely start and reset. The start button is used to start the game and the reset button will reset the game so that players can start the game from the beginning.

The code above will create an interface like this.

tkinter color game ui

Then I defined some functions to handle the button clicks and track the score, display random words with different colors, to start the count down, etc.

Here is the complete code for Color Game in Tkinter.

from tkinter import *
import tkinter.font as font
import random

colors = ["Red", "Orange", "White", "Black", "Green", "Blue", "Brown", "Purple", "Cyan", "Yellow", "Pink", "Magenta"]

timer = 60
score = 0
displayed_word_color = ''

#This fuction will be called when start button is clicked
def startGame():
    global displayed_word_color

    if(timer == 60):
        startCountDown()
        displayed_word_color = random.choice(colors).lower()
        display_words.config(text=random.choice(colors), fg=displayed_word_color)
        color_entry.bind('<Return>', displayNextWord)

#This function is to reset the game
def resetGame():
    global timer, score, displayed_word_color
    timer = 60
    score = 0
    displayed_word_color = ''
    game_score.config(text = "Your Score : " + str(score))
    display_words.config(text = '')
    time_left.config(text="Game Ends in : -")
    color_entry.delete(0, END)

#This function will start count down
def startCountDown():
    global timer
    if(timer >= 0):
        time_left.config(text = "Game Ends in : " + str(timer) + "s")
        timer -= 1
        time_left.after(1000,startCountDown)
        if (timer == -1):
            time_left.config(text="Game Over!!!")

#This function to display random words
def displayNextWord(event):
    global displayed_word_color
    global score
    if(timer > 0):
        if(displayed_word_color == color_entry.get().lower()):
            score += 1
            game_score.config(text = "Your Score : " + str(score))
        color_entry.delete(0, END)
        displayed_word_color = random.choice(colors).lower()
        display_words.config(text = random.choice(colors), fg = displayed_word_color)

my_window = Tk()
my_window.title("Color Game")
my_window.geometry("500x200")

app_font = font.Font(family='Helvetica', size = 12)

game_desp = "Game Description: Enter the color of the words displayed below. \n And Keep in mind not to enter the word text itself"
myFont = font.Font(family='Helvetica')

game_description = Label(my_window, text = game_desp, font = app_font, fg= "grey")
game_description.pack()

game_score = Label(my_window, text = "Your Score : " + str(score), font = (font.Font(size=16)), fg = "green")
game_score.pack()

display_words = Label(my_window , font = (font.Font(size=28)), pady = 10)
display_words.pack()

time_left = Label(my_window, text = "Game Ends in : -", font = (font.Font(size=14)), fg = "orange")
time_left.pack()

color_entry = Entry(my_window, width = 30)
color_entry.pack(pady = 10)

btn_frame = Frame(my_window, width= 80, height = 40, bg= 'red')
btn_frame.pack(side = BOTTOM)

start_button = Button(btn_frame, text = "Start", width = 20, fg = "black", bg = "pink", bd = 0,padx = 20, pady = 10 , command = startGame)
start_button.grid(row=0, column= 0)

reset_button = Button(btn_frame, text = "Reset", width = 20, fg = "black", bg = "light blue", bd = 0,padx = 20, pady = 10 , command = resetGame)
reset_button.grid(row=0, column= 1)

my_window.geometry('600x300')

In the above code, I have imported tkinter for the GUI itself and random to randomly select the color from the list. 

When the player clicks on the start button, the startGame() function will be called. this function first checks whether the timer is equal to 60 or not. Previously I have set timer variable to 60. if the timer variable is equal to 60 then another function startCountDown() will be called to start counting from 60 to 0. 

After calling the startCountDown() function a random color from the colors list will be fetched and shown to the user with a different text color.

startCountDown() function will just count from 60 to 0 and when the counter hits 0 it will display a message “Game Over!!!” to the player.

And have written another function called displayNextWord(). The purpose of this function is to display a new word every time the player submits his answer and increment the score if the player enters the correct answer. And repeat this process until the timer gets to zero.

 we have also defined a reset button in our interface. When the player clicks on this reset button resetGame() function will be called. This function will simply set the timer variable to 60 and sets the score variable to 0. And updates the same in the UI.

Conclusion

Hence we have successfully developed a color game using python Tkinter. Further, we can also try to add some more features to this project such as allowing the player to select the duration of the game (count down timer), or to select the number of words to display, etc.

Leave a Reply

Your email address will not be published.