-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
colorgame.py
95 lines (72 loc) · 3.31 KB
/
colorgame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Importing necessary packages
import random
import tkinter as tk
from tkinter import *
from tkinter import messagebox
# Declaring global variables
timeleft = 60
score = 0
#---
# Defining a function to create widgets for the game
def CreateWidgets():
instLabel = Label(root, text = "ENTER COLOR OF THE TEXT",
font = ('Helvetica',30),background = 'WHITE')
instLabel.grid(row = 0, column = 0, columnspan = 3, padx=5, pady=15)
startButton = Button(root, text = "START GAME", width = 20,
font = ('Helvetica',15), command = StartGame, background='WHITE')
startButton.grid(row = 1, column = 0,padx = 5,pady = 15, columnspan = 3 )
root.timeLabel = Label(root, text="TIME LEFT : ", font = ('Helvetica',30),
background='WHITE')
root.timeLabel.grid(row=2, column=0, padx=5, pady=15)
root.scoreLabel = Label(root, text="SCORE : "+str(score),
font = ('Helvetica',30),background = 'WHITE')
root.scoreLabel.grid(row=2, column=1, padx=5, pady=15)
root.gameLabel = Label(root, font = ('Comic Sans MS',60),background = 'WHITE')
root.gameLabel.grid(row = 3, column = 0, padx=5, pady=15, columnspan = 2)
root.answerEntry = Entry(root,width = 50, font =30,background = 'SILVER')
root.answerEntry.grid(row=4, column=0, padx=5, pady=15, columnspan = 2)
root.answerEntry.focus()
#---
# Defining function to start the game
def StartGame():
# Calling the above declared global variables
global timeleft, score
# Checking if the timeleft is greater than 0. If yes do the following
if timeleft > 0:
# Decrementing the timeleft by 1
timeleft -= 1
# Displaying the time left in above created label for time left
root.timeLabel.config(text="TIME LEFT : " + str(timeleft))
# Creating a list of random colors
randomColor = ['RED', 'GREEN', 'BLUE', 'VIOLET', 'PINK', 'BROWN', 'BLACK']
# Shuffling the list
random.shuffle(randomColor)
# Selecting two colors from the shuffled randomColor list and setting
# One Color as Label Text and the Other Color as Font Color of the text
root.gameLabel.config(text=str(randomColor[0]), fg=randomColor[1])
#---
# Check if the user's input is equal to the Font Color of the text
if root.answerEntry.get().lower() == randomColor[1].lower():
#If yes increment the score value by 1
score += 1
# Displaying the score
root.scoreLabel.config(text = "SCORE : "+str(score))
# Clearing the user's entry
root.answerEntry.delete(0, END)
# Calling the StartGame function again after 1 second
root.timeLabel.after(1000, StartGame)
# If timeleft is equal to 0, then stop the game and display the users's score
else:
messagebox.showinfo("TIME UP !","YOUR SCORE IS : "+str(score))
#---
# Creating object of tk class
root = tk.Tk()
# Setting the title and background color
# disabling the resizing property
root.title("PyColor Game")
root.configure(background = 'WHITE')
root.resizable(False, False)
# Calling the CreateWidgets() function
CreateWidgets()
# Defining infinite loop to run application
root.mainloop()