Introduction of the Project
Do you like to play games? Today, we are going to code a wonderful, confusing color game using python language. For this, we are going to use the Tkinter module. Tkinter provides classes that allow the display, positioning, and control of widgets like Frame, Label, Entry, Text, Canvas, Button, Radiobutton, Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox LabelFrame, and PanedWindow. Now let us have a look at the requirements and source code for this color game in the python using Tkinter module.
1. VSCode or any Python IDE
2. Tkinter and Random module must be preinstalled on your pc
Steps to Create Color Game In Python Using Tkinter Module
Step 1: Windows: Open Command Prompt and type
macOS: Open Terminal and type
pip3 install tk and
pip3 install random
Step 2: Paste the below piece of code in your Editor/IDE; you can also alter it as per your wish!
# Import the modules import tkinter import random # List of colours colours = ['Pink', 'Blue', 'Green', 'Orange', 'Black', 'Yellow', 'Purple', 'White', 'Brown'] # Initialising score to 0 score = 0 # Initialy the time will be 25 seconds remainingtime = 25 # This function will begin the game def beginGame(event): if remainingtime == 25: # If the above statement is satisfied, we are beginning the countdown countdown() # Here, we are calling function to choose the next colour nextColour() # This function will choose the next color def nextColour(): global score global remainingtime # If statement if remainingtime > 0: # If the above statement is satisfied, we are making the text entry box active i.focus_set() # If the input color name and colou of the text are matching, if i.get().lower() == colours.lower(): # we are increasing the score by 5 score += 5 # Using, delete function, we are deleting the text of the box i.delete(0, tkinter.END) # The random generates a random colour using shuffle random.shuffle(colours) # Here, we are changing the text colour to a random colour value label.config(fg = str(colours), text = str(colours)) # Here, we are updating the score scoreLabel.config(text = "Score : " + str(score)) # This is countdown function def countdown(): global remainingtime if remainingtime > 0: # If the above statement is satisfied, we will decrement the time by 1 second remainingtime -= 1 # Here, we are updating the remaining time label timeLabel.config(text = "Remaining time : " + str(remainingtime)) # after function, calls function once after given time in milliseconds. timeLabel.after(1000, countdown) # To create GUI window root = tkinter.Tk() # To set the title root.title("COLOR GAME") # To set the size root.geometry("900x600") # Here, we are adding an instruction label instruction = tkinter.Label(root, text = "\n Type the colour of the words, and not the word text! \n", font = ('TimesNewRoman', 20)) instruction.pack() scoreLabel = tkinter.Label(root, text = "Press Enter to Play \n", font = ('TimesNewRoman', 20)) scoreLabel.pack() # Remaining time label timeLabel = tkinter.Label(root, text = "Remaining time: " + str(remainingtime), font = ('TimesNewRoman', 20)) timeLabel.pack() # Color text font and size specification label = tkinter.Label(root, font = ('TimesNewRoman', 80)) label.pack() # Input text box i = tkinter.Entry(root) # To run the 'beginGame' function when the Enter key is pressed root.bind('<Return>', beginGame) i.pack() # To set focus on the input box i.focus_set() # To start the GUI root.mainloop()
Explanation Of The Code
In the beginning, we imported two modules to generate random colors and widgets.
1. We have created a list of colors, which we will be using as input into the input box.
2. After that, we have initialized the score to 0 and the remaining time to 25 seconds.
3. Now, we are using begin game function, in which we are using the if statement, in which if the remaining time is equal to 25 seconds, the countdown function will start, and the next color function is also called.
4. Next, we are using the next color function to produce the next color with its text. In this function, we convert the input text to lower case and then check whether the color matches it.
5. The shuffle function is used to shuffle the list of colors, and the delete function is used to clear the input box.
6. Now, if the color name matches the color of the text, the score will be increased by 5; else not.
7. In the countdown function, we are using the after function to call the countdown function after each second to decrease the remaining time by one second.
8. At last, we are creating GUI, in which we are giving the title, setting the geometry, score label, timelable, instruction, and input box with color text fonts.
The GUI of the color game in python using the Tkinter module will look like the below screenshot of the output. Time is limited, be fast in answering!
Things to Remember
- Don’t put space in between height and width in the geometry function.
- Write the name of the modules in lowercase only.
Cisco Ramon is an American software engineer who has experience in several popular and commercially successful programming languages and development tools. He has been writing content since last 5 years. He is a Senior Manager at Rude Labs Pvt. Ltd.