TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games.In this game 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. Colors used in this game are Red, Blue, Green, Pink, Black, Yellow, Orange, White, Purple and Brown. Interface will display name of different colors in different colors. Player has to identify the color and enter the correct color name to win the game.
Here the code:-
#Step1:- import the modules
import tkinter
import random
#Step2:-list of possible colour.
colours = ['Red','Blue','Green','Pink','Black',
'Yellow','Orange','White','Purple','Brown']
#Step3:set the initially score as 0 and game time left 30 seconds
score = 0
timeleft = 30
# Step4:-function that will start the game.
def startGame(event):
if timeleft == 30:
# start the countdown timer.
countdown()
# run the function to choose the next colour.
nextColour()
#Step5:- Function to choose and display the next colour.
def nextColour():
#use the globally declared 'score' and 'play' variables above.
global score
global timeleft
# if a game is currently in play
if timeleft > 0:
# make the text entry box active.
e.focus_set()
# if the colour typed is equal to the colour of the text
if e.get().lower() == colours[1].lower():
score += 1
# clear the text entry box.
e.delete(0, tkinter.END)
random.shuffle(colours)
#Step6:-change the colour to type,by changing the text and the colour to a random colour value
label.config(fg = str(colours[1]), text = str(colours[0]))
# update the score.
scoreLabel.config(text = "Score: " + str(score))
# Step7:-Countdown timer function
def countdown():
global timeleft
# if a game is in play
if timeleft > 0:
# decrement the timer.
timeleft -= 1
# update the time left label
timeLabel.config(text = "Time left: "
+ str(timeleft))
# run the function again after 1 second.
timeLabel.after(1000, countdown)
#Step8:- create a GUI window,set the title,size
root = tkinter.Tk()
root.title("COLORGAME")
root.geometry("500x300")
root.configure(bg="aliceblue")
#Step9:- Add all the Labels
#add an instructions label
instructions = tkinter.Label(root, text = "Type in the colour"
"of the words, and not the word text!",
font = ('Helvetica', 12,'bold'),fg="TEAL",bg="aliceblue")
instructions.pack()
#add a score label
scoreLabel = tkinter.Label(root, text = "Press enter to start",
font = ('Helvetica', 12,'bold'),fg="TEAL",bg="aliceblue")
scoreLabel.pack()
# add a time left label
timeLabel = tkinter.Label(root, text = "Time left: " +
str(timeleft), font = ('Helvetica', 12,'bold'),fg="TEAL",bg="aliceblue")
timeLabel.pack()
# add a label for displaying the colours
label = tkinter.Label(root, font = ('Helvetica', 60,'bold'))
label.pack()
# add a text entry box for typing in colours
e = tkinter.Entry(root,font = ('arial', 14, 'bold'),bg = "powderblue", justify = 'right')
#Step10:- run the 'startGame' function when the enter key is pressed
root.bind('<Return>', startGame)
e.pack()
# set focus on the entry box
e.focus_set()
#Step11:-keeps window alive
root.mainloop()
Here the Output:
1 Comments
tuiriWpist_za-1984 Gregory Taharka Here
ReplyDeleteucolcenla