-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_guessing_game.py
58 lines (51 loc) · 1.59 KB
/
color_guessing_game.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
'''
Requirements
# pip install tk
'''
# import necessary modules
import tkinter
import random
# predefinced variables
colours = ['Red','Blue','Green','Pink','Black', 'Yellow','Orange','White','Purple','Brown']
score = 0
timeleft = 30
# function to start game
def startGame(event):
if timeleft == 30:
countdown()
nextColour()
# function to display next random color
def nextColour():
global score
global timeleft
if timeleft > 0:
e.focus_set()
if e.get().lower() == colours[1].lower():
score += 1
e.delete(0, tkinter.END)
random.shuffle(colours)
label.config(fg = str(colours[1]), text = str(colours[0]))
scoreLabel.config(text = "Score: " + str(score))
# function for countdown timer
def countdown():
global timeleft
if timeleft > 0:
timeleft -= 1
timeLabel.config(text = "Time left: "+ str(timeleft))
timeLabel.after(1000, countdown)
windows = tkinter.Tk()
windows.title("COLOR GAME")
windows.geometry("375x200")
instructions = tkinter.Label(windows, text = "Type in the colour of the words, and not the word text!",font = ('Helvetica', 12))
instructions.pack()
scoreLabel = tkinter.Label(windows, text = "Press enter to start", font = ('Helvetica', 12))
scoreLabel.pack()
timeLabel = tkinter.Label(windows, text = "Time left: " +str(timeleft), font = ('Helvetica', 12))
timeLabel.pack()
label = tkinter.Label(windows, font = ('Helvetica', 60))
label.pack()
e = tkinter.Entry(windows)
windows.bind('<Return>', startGame)
e.pack()
e.focus_set()
windows.mainloop()