-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorgame.py
153 lines (85 loc) · 2.75 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# import the modules
import tkinter
import random
# list of possible colour.
colours = ['Red','Blue','Green','Pink','Black',
'Yellow','Orange','White','Purple','Brown']
score = 0
# the game time left, initially 60 seconds.
timeleft = 60
# function that will start the game.
def startGame(event):
if timeleft == 60:
# start the countdown timer.
countdown()
# run the function to
# choose the next colour.
nextColour()
# 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)
# 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))
# 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)
# Driver Code
# -------------create a GUI window-------------------
root = tkinter.Tk()
# set the title
root.title("COLORGAME")
# set the size
root.geometry("475x300")
# add an instructions label
instructions = tkinter.Label(root, text = "Type the colour Name",fg= '#625050',
font = ('Time', 30,"bold",'underline'))
instructions.pack()
# add a score label
scoreLabel = tkinter.Label(root, text = "Press enter to start!", fg= '#c27099',
font = ('Helvetica', 12))
scoreLabel.pack()
# add a time left label
timeLabel = tkinter.Label(root, text = "Time left: " +
str(timeleft),fg='#b30000', font = ('Helvetica', 20))
timeLabel.pack()
# add a label for displaying the colours
label = tkinter.Label(root, font = ('Helvetica', 60))
label.pack()
# add a text entry box for
# typing in colours
e = tkinter.Entry(root)
# 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()
# start the GUI
root.mainloop()