
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Color game using Tkinter in Python
For developing GUI applications tkinter is very popular and easy. Using tkinter easily develop GUI games.
Here also we are trying to develop color game. In this game the player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds and colors used in this game are Red, Blue, Green, Pink, Black, Yellow, Orange, White, Purple and Brown. The interface will display name of different colors in different colors. User has to identify the color and enter the correct color name to win the game.
Example code
import tkinter import random # list of colour. my_colours = ['Red','Blue','Green','Pink','Black','Yellow','Orange','White','Purple','Brown'] my_score = 0 my_timeleft = 30 def my_startGame(event): if my_timeleft == 30: # start the countdown timer. my_countdown() my_nextColour() def my_nextColour(): global my_score global my_timeleft # if a game is currently in play if my_timeleft > 0: e.focus_set() if e.get().lower() == my_colours[1].lower(): my_score += 1 # clear the text entry box. e.delete(0, tkinter.END) random.shuffle(my_colours) label.config(fg = str(my_colours[1]), text = str(my_colours[0])) # update the score. my_scoreLabel.config(text = "Score: " + str(my_score)) # Countdown timer function def my_countdown(): global my_timeleft # if a game is in play if my_timeleft > 0: # decrement the timer. my_timeleft -= 1 # update the time left label timeLabel.config(text = "Time left: "+ str(my_timeleft)) # run the function again after 1 second. timeLabel.after(1000, my_countdown) # Driver Code root = tkinter.Tk() root.title("COLORGAME") root.geometry("375x200") my_instructions = tkinter.Label(root, text = "Type in the color" "of the words, and not the word text!", font = ('Helvetica', 12)) my_instructions.pack() my_scoreLabel = tkinter.Label(root, text = "Press enter to start", font = ('Helvetica', 12)) my_scoreLabel.pack() my_timeLabel = tkinter.Label(root, text = "Time left: " + str(my_timeleft), font = ('Helvetica', 12)) my_timeLabel.pack() label = tkinter.Label(root, font = ('Helvetica', 60)) label.pack() e = tkinter.Entry(root) root.bind('<Return>', my_startGame) e.pack() e.focus_set() # start the GUI root.mainloop()
Output


- Related Articles
- Rock Paper and Scissor Game Using Tkinter
- Setting Background color for Tkinter in Python
- Default window color Tkinter and hex color codes in Tkinter
- Conway’s Game Of Life using Python?
- Catching the ball game using Python
- Develop Notepad using Tkinter in python
- Changing the background color of a tkinter window using colorchooser module
- Hangman Game in Python?
- Jump Game in Python
- Baseball Game in Python
- How to reset the background color of a Python Tkinter button?
- Word Dictionary using Python Tkinter
- Simple GUI calculator using Tkinter in Python
- Draw a circle in using Tkinter Python
- How to use rgb color codes in tkinter?

Advertisements