Introduction:
Python offers several options for developing GUIs (Graphical User Interface). Of all the GUI approaches, Tkinter is the one that is most often used. It is the standard Python interface that comes preloaded with Python and is part of the Tk GUI toolkit.
Tkinter is the quickest and easiest method of creating graphical user interfaces (GUIs).
TKinter is a popular tool for creating graphical user interfaces. Tkinter GUI may be used not just to create programs but also to create games.
Tkinter’s module may be used to show, position, and control widgets such as Label, Frame, Entry, Canvas, Text, Button, Checkbutton, Scale, Radiobutton, Scrollbar, Listbox, Spinbox LabelFrame, OptionMenu, and PanedWindow.
We’re going to use Tkinter Python to create a color game in this blog. Let us explain this color game if you are unfamiliar with it. The color game is an easy game to play. In this game, text colors will correspond with distinct color names. The player’s job in this game is to input the word’s right color as it appears on the screen. The score will increase by one each time the player inputs the proper color. Additionally, the game will last for 60 seconds. Colors used in this game are “Red”, “Orange”, “White”, “Black”, “Green”, “Blue”, “Brown”, “Purple”, “Cyan”, “Yellow”, “Pink”, and “Magenta”.
For example, the player must write in the color of the text when it shows the word Yellow in a red font. Playing is made fun by the mismatch of information and timing, which causes a lot of uncertainty.
The Color Game:
The objective of the color game is to locate a collection of colors that the computer chose at random. The color-guessing game that we may play in Python requires us to determine the color of a given sentence at random. You need to type the word “color” in this case, not “text.”
This system’s straightforward design ensures that the user don’t have any problem using it.
We need to set the color of the text that is displayed on the screen before moving forward. When a person correctly predicts the color shown on the screen after seeing the word, they are rewarded with credit for their appropriate guess. For every correct guess, they get one point, which is added to the score on the screen. The user can play this game until the allotted time runs out. The user may enter anything in the entry box before the time runs out, but it won’t be accepted.
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 = ''
#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= "black")
game_description.pack()
game_score = Label(my_window, text = "Your Score : " + str(score), font = (font.Font(size=16)), fg = "red")
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 = "green")
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= 'yellow')
btn_frame.pack(side = BOTTOM)
start_button = Button(btn_frame, text = "Start", width = 20, fg = "black", bg = "purple", 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 = "Orange", bd = 0,padx = 20, pady = 10 , command = resetGame)
reset_button.grid(row=0, column= 1)
my_window.geometry('600x300')
The above-mentioned code opens a window featuring game details and play instructions. The window displays current scores, uniquely colored words, a countdown timer, and a color-entry box. The start and reset buttons are situated at the window’s bottom. The game begins with the start button and restarts with the reset button. Functions manage button clicks, scoring, random text in various colors, countdown initiation, and more. The Tkinter module is imported to craft the GUI, and the random module is used to select colors randomly. The startGame() method activates when the player presses start, assessing if the timer equals 60. The startCountDown() procedure counts down from 60 if the timer is 60, revealing a randomly colored word. “Game Over!!!” displays when the countdown reaches zero. displayNextWord() shows a new word and updates the score upon each correct response until the timer ends. The resetGame() method triggers reset, resetting the score and timer to 0, maintaining the same UI.
Output:
The Color Game application initiates with a window appearing on the screen. Once the player hits Enter to start the countdown, they must promptly input the word’s color. A correct response increases the score by one, and upon completion, the final score is displayed.
Conclusion:
Python proves to be an excellent language for crafting video games. Not only is it easy to learn and use, but it also empowers game developers of all skill levels to create visually stunning and engaging gaming environments. Moreover, developers enjoy a plethora of options to bring their ideas to life, thanks to various game development platforms like PyGame and Tkinter.
In addition, there is room for expanding this project by incorporating additional features. For instance, allowing players to customize the game duration through a countdown timer, deciding the number of displayed words, and more. This flexibility enhances the overall gaming experience, providing users with the ability to tailor the game to their preferences. Learn Python for kids and teens online with Codingal. Whether you are a beginner exploring the basics or an experienced developer aiming to add depth to your projects, learning Python online with Codingal proves to be a transformative journey.