Python Tkinter Color Game

In this Python Tkinter Color Game tutorial, we will learn about How to play the Python Tkinter Color Game in Python. To move further, firstly we set the color which shows on the screen in the form of words. The user sees the word and guess the color which is shown on the screen and type the correct color inside the entry box and get the score of the correct guessing color. On every correct guess, they get the point and these points are added to the score that shows on the screen. The user can play this game before the time’s end if the times are up the user can type anything in the entry box but the not accepted this is the rule of the game.

Python Tkinter Color Game
Python Tkinter Color Game

Python Tkinter Color Game

Block of code:

In this Python Tkinter Color Game block of code, we will import the Tkinter library to create a color game with beautiful colors.

  • colours = [‘Cyan’,’Black’,’Purple’,’Yellow’,’Blue’, ‘Green’,’Light Blue’,’White’,’Orange’,’Brown’,’Red’] is used to select the color.
  • score = 0 is used to show the score how many correct and the user gives.
  • time_left = 40 is used to give the time limit to the user.
from tkinter import *
import tkinter as tk 
import random 
colours = ['Cyan','Black','Purple','Yellow','Blue', 'Green','Light Blue','White','Orange','Brown','Red'] 
score = 0
time_left = 40 

Block of code:

In this Python Tkinter Color Game block of code, we define a function start game from which the game is started and the timer also started.

  • countdown() is used to start the countdown.
  • next_Colour() is used to move to the next color.
def start_Game(event):
    if time_left == 40:
        countdown()
    next_Colour() 

Block of code:

In this Python Tkinter block of code, we define a function next color after giving the and or we can say that choose the correct color the next color will appear and the user guesses the next color accordingly.

  • if entry.get().lower() == colours[1].lower(): is used to get the input from the user in the entry box.
  • entry.delete(0, tk.END) is the user enters the correct entry the first entry will delete automatically and the user can fill the next input.
  • random.shuffle(colours) is used to generate the random colors.
  • scoreLabel.config(text = “Score: ” + str(score)) is used to collect the score on the screen.
def next_Colour():
    global score 
    global timeleft
    if time_left > 0:
        entry.focus_set()
        if entry.get().lower() == colours[1].lower():
            score += 1
        entry.delete(0, tk.END)
        random.shuffle(colours)
        label.config(fg = str(colours[1]), text = str(colours[0]))
        scoreLabel.config(text = "Score: " + str(score)) 

Read: Python Tkinter Instagram User Detail

Block of code:

In this Python Tkinter Color Game block of code, we see after pressing enter key the timer start and the user can play the game.

timeLabel.config(text = “Time Left: “+ str(time_left)) is used to give the label on the screen.

def countdown():
    global time_left
    if time_left > 0:
        time_left -= 1
        timeLabel.config(text = "Time Left: "+ str(time_left))
        timeLabel.after(1000, countdown) 

Block of code:

In this Python Tkinter Color Game block of code, we can create a window on which our color game is played by the user.

  • wd.title(“Pythotpoint”) is used to give the title to the screen.
  • wd.geometry(“500×400”) is used to get width and height to the screen.
  • scoreLabel = tk.Label(wd, text = “Press Enter To Start”, font = (‘Times New Roman’, 15)) is used to give the score label.
  • entry = tk.Entry(wd) is used to create entry box.
wd = tk.Tk() 
wd.title("Pythotpoint") 
wd.geometry("500x400") 
instructions = tk.Label(wd, text = "Type in the colour of the words, not the word text!",font = ('Times New Roman', 15)) 
instructions.pack() 
scoreLabel = tk.Label(wd, text = "Press Enter To Start", font = ('Times New Roman', 15)) 
scoreLabel.pack() 
timeLabel = tk.Label(wd, text = "Time left: " +str(time_left), font = ('Times New Roman', 15)) 				
timeLabel.pack() 
label = tk.Label(wd, font = ('Times New Roman', 60)) 
label.pack() 
entry = tk.Entry(wd) 
wd.bind('<Return>', start_Game) 
entry.pack() 
entry.focus_set() 
wd.mainloop() 

Check this code in Repository from Github and you can also fork this code.

Github User Name: PythonT-Point

Code:

In the following code, we will import the Tkinter library to create a color game in which the user guesses the color of the words.

from tkinter import *
import tkinter as tk 
import random 
colours = ['Cyan','Black','Purple','Yellow','Blue', 'Green','Light Blue','White','Orange','Brown','Red'] 
score = 0
time_left = 40 
def start_Game(event):
    if time_left == 40:
        countdown()
    next_Colour() 
def next_Colour():
    global score 
    global timeleft
    if time_left > 0:
        entry.focus_set()
        if entry.get().lower() == colours[1].lower():
            score += 1
        entry.delete(0, tk.END)
        random.shuffle(colours)
        label.config(fg = str(colours[1]), text = str(colours[0]))
        scoreLabel.config(text = "Score: " + str(score)) 
def countdown():
    global time_left
    if time_left > 0:
        time_left -= 1
        timeLabel.config(text = "Time Left: "+ str(time_left))
        timeLabel.after(1000, countdown) 
wd = tk.Tk() 
wd.title("Pythontpoint") 
wd.geometry("500x400") 
instructions = tk.Label(wd, text = "Type in the colour of the words, not the word text!",font = ('Times New Roman', 15)) 
instructions.pack() 
scoreLabel = tk.Label(wd, text = "Press Enter To Start", font = ('Times New Roman', 15)) 
scoreLabel.pack() 
timeLabel = tk.Label(wd, text = "Time left: " +str(time_left), font = ('Times New Roman', 15)) 				
timeLabel.pack() 
label = tk.Label(wd, font = ('Times New Roman', 60)) 
label.pack() 
entry = tk.Entry(wd) 
wd.bind('<Return>', start_Game) 
entry.pack() 
entry.focus_set() 
wd.mainloop() 

Output:

After running the above code, we get the following output in which we can see that the colored word is shown on the screen and the user guesses the color of the word and written in the entry box f the color is correct then give the point to the user and this point is added to the score.

Python Tkinter color game
Python Tkinter Color Game

3 thoughts on “Python Tkinter Color Game”

  1. A person essentially help to make seriously posts I would state. This is the first time I frequented your web page and thus far? I surprised with the research you made to create this particular publish incredible. Magnificent job!

    Reply

Leave a Comment