504
Creating a Color Game using tkinter involves displaying a color name on the screen with a different font color and asking the player to enter the font color of the displayed text.
This game tests the player’s focus and ability to identify the displayed font color quickly.
We will use the tkinter library for the GUI, random for generating random colors, and a timer to keep the player engaged.
Code
import tkinter as tk import random # List of colors colors = ["Red", "Blue", "Green", "Pink", "Orange", "Purple", "Yellow", "Brown", "Black", "White"] # Initialize score and time score = 0 time_left = 30 # Game duration in seconds # Function to start the game def start_game(event): if time_left == 30: countdown() display_color() # Function to update the displayed color def display_color(): global score, time_left # Check if the time is running if time_left > 0: # Get the text entered by the player user_input = input_entry.get().strip().lower() input_entry.delete(0, tk.END) # Clear the input field # Check if the player's input matches the font color if user_input == color_label["fg"].lower(): score += 1 # Update score display score_label.config(text="Score: " + str(score)) # Choose a new color name and a new color font random_color_name = random.choice(colors) random_font_color = random.choice(colors) # Ensure that the displayed text and its font color are not the same while random_color_name.lower() == random_font_color.lower(): random_font_color = random.choice(colors) # Set the new color text and font color color_label.config(text=random_color_name, fg=random_font_color) # Function for countdown timer def countdown(): global time_left if time_left > 0: time_left -= 1 time_label.config(text="Time left: " + str(time_left)) time_label.after(1000, countdown) else: # Display game over message color_label.config(text="Game Over!", fg="black") # Setup the main window root = tk.Tk() root.title("Color Game") root.geometry("400x300") # Instructions label instructions = tk.Label(root, text="Type the font color of the word displayed!", font=("Helvetica", 14)) instructions.pack() # Score label score_label = tk.Label(root, text="Score: 0", font=("Helvetica", 12)) score_label.pack() # Time label time_label = tk.Label(root, text="Time left: " + str(time_left), font=("Helvetica", 12)) time_label.pack() # Color label (to display the color name with a different font color) color_label = tk.Label(root, font=("Helvetica", 24)) color_label.pack() # Entry box for user input input_entry = tk.Entry(root) input_entry.pack() input_entry.focus_set() # Set focus to the input box # Bind the Enter key to start the game or submit an answer root.bind("<Return>", start_game) # Run the main loop root.mainloop()
Explanation of the Code
- Import Libraries:
- tkinter is imported as tk to create the GUI window and its elements.
- random is used to select random colors from the list for both the displayed text and its font color.
- Define the List of Colors:
- colors is a list containing color names that will be used as both text and font colors.
- Initialize Score and Time Variables:
- score is set to 0 at the start.
- time_left is set to 30 seconds, which is the duration of the game.
- start_game Function:
- This function starts the game when the Enter key is pressed.
- If the game has just started (time is at 30 seconds), it calls countdown() to start the timer and then calls display_color() to show the first color.
- display_color Function:
- This function handles displaying a new color and updating the score based on the player’s input.
- Check Player Input:
- It retrieves the text entered by the player and converts it to lowercase.
- If the input matches the font color of the displayed text (color_label[“fg”]), the score increases by 1.
- Choose New Color:
- It selects a new color name (random_color_name) and a new font color (random_font_color) from the colors list.
- The font color should be different from the color name to make the game challenging.
- Update the Display:
- The text (random_color_name) and font color (random_font_color) are applied to color_label.
- countdown Function:
- This function manages the countdown timer.
- If time_left is greater than 0, it decreases the time by 1 every second.
- The after() method schedules the countdown function to run again after 1000 milliseconds (1 second).
- When time_left reaches 0, the game ends, and the text changes to “Game Over!”.
- Setting Up the GUI Window:
- root = tk.Tk() creates the main application window.
- root.geometry(“400×300”) sets the window size.
- Instructions Label:
- instructions explains the game rules to the player.
- Score and Time Labels:
- score_label displays the current score, initialized to 0.
- time_label shows the time left, initialized to 30 seconds.
- Color Display Label (color_label):
- color_label is where the color name appears, with a font color set to something different to make the game challenging.
- Input Entry Box:
- input_entry is the Entry widget where the player types their answer.
- input_entry.focus_set() ensures the cursor is active in the entry box, allowing the player to type immediately.
- Binding the Enter Key:
- root.bind(“<Return>”, start_game) binds the Enter key to start the game and process the player’s answer.
- Running the Game Loop:
- root.mainloop() starts the main event loop, keeping the application running and responsive.
How the Game Works
- Game Start:
- The player presses Enter to start.
- The timer begins counting down from 30 seconds, and a color name appears with a different font color.
- Player Interaction:
- The player types the font color of the displayed text in the entry box and presses Enter.
- If the answer is correct, the score increases by 1, and a new color name and font color are displayed.
- The player continues guessing until the timer reaches 0.
- Game End:
- When the timer reaches 0, “Game Over!” is displayed on the screen.
Sample Gameplay
Welcome to the Color Game! Type the font color of the word displayed! Score: 0 Time left: 30 Display: "Red" (in green font color) User types: "green" -> Score increases to 1 Display: "Blue" (in orange font color) User types: "orange" -> Score increases to 2 ... Game ends when time_left reaches 0
Summary
- The Color Game tests the player’s ability to focus on the font color rather than the text itself.
- random.choice() is used to select a random color for text and font.
- The timer and score are displayed and updated in real-time.
- tkinter’s after() method allows for continuous updating, creating an engaging and responsive game experience.