1
+ from tkinter import *
2
+ import tkinter .font as font
3
+ import random
4
+
5
+ colors = ["Red" , "Orange" , "White" , "Black" , "Green" , "Blue" , "Brown" , "Purple" , "Cyan" , "Yellow" , "Pink" , "Magenta" ]
6
+ timer = 60
7
+ score = 0
8
+ displayed_word_color = ''
9
+
10
+
11
+ # This function will be called when start button is clicked
12
+ def startGame ():
13
+ global displayed_word_color
14
+
15
+ if timer == 60 :
16
+ startCountDown ()
17
+ displayed_word_color = random .choice (colors ).lower ()
18
+ display_words .config (text = random .choice (colors ), fg = displayed_word_color )
19
+ color_entry .bind ('<Return>' , displayNextWord )
20
+
21
+ # This function is to reset the game
22
+ def resetGame ():
23
+ global timer , score , displayed_word_color
24
+ timer = 60
25
+ score = 0
26
+ displayed_word_color = ''
27
+ game_score .config (text = "Your Score : " + str (score ))
28
+ display_words .config (text = '' )
29
+ time_left .config (text = "Game Ends in : -" )
30
+ color_entry .delete (0 , END )
31
+
32
+ # This function will start count down
33
+ def startCountDown ():
34
+ global timer
35
+ if timer >= 0 :
36
+ time_left .config (text = "Game Ends in : " + str (timer )+ "s" )
37
+ timer -= 1
38
+ time_left .after (1000 ,startCountDown )
39
+ if timer == - 1 :
40
+ time_left .config (text = "Game Over!!!" )
41
+
42
+
43
+ # This function to display random words
44
+ def displayNextWord (event ):
45
+ global displayed_word_color
46
+ global score
47
+ if timer > 0 :
48
+ if displayed_word_color == color_entry .get ().lower ():
49
+ score += 1
50
+ game_score .config (text = "Your Score : " + str (score ))
51
+ color_entry .delete (0 , END )
52
+ displayed_word_color = random .choice (colors ).lower ()
53
+ display_words .config (text = random .choice (colors ), fg = displayed_word_color )
54
+
55
+
56
+ root = Tk ()
57
+ root .title ("Color Game" )
58
+ root .geometry ("500x200" )
59
+ root .iconbitmap ("./img/game-console.ico" )
60
+
61
+ app_font = font .Font (family = 'Helvetica' , size = 12 )
62
+
63
+ game_desp = "Game Description: Enter the color of the words displayed below. \n And Keep in mind not to enter the " \
64
+ "word text itself "
65
+ myFont = font .Font (family = 'Helvetica' )
66
+
67
+ game_description = Label (root , text = game_desp ,font = app_font , fg = "grey" )
68
+ game_description .pack ()
69
+
70
+ game_score = Label (root , text = "Your Score : " + str (score ), font = (font .Font (size = 16 )), fg = "green" )
71
+ game_score .pack ()
72
+
73
+ display_words = Label (root ,font = (font .Font (size = 28 )), pady = 10 )
74
+ display_words .pack ()
75
+
76
+ time_left = Label (root ,text = "Game Ends in : -" , font = (font .Font (size = 14 )), fg = "orange" )
77
+ time_left .pack ()
78
+
79
+ color_entry = Entry (root , width = 30 )
80
+ color_entry .pack (pady = 10 )
81
+
82
+ btn_frame = Frame (root , width = 80 , height = 40 , bg = 'red' )
83
+ btn_frame .pack (side = BOTTOM )
84
+
85
+ start_button = Button (btn_frame , text = "Start" , width = 20 , fg = "black" , bg = "pink" , bd = 0 , padx = 20 , pady = 10 ,command = startGame )
86
+ start_button .grid (row = 0 , column = 0 )
87
+
88
+ reset_button = Button (btn_frame , text = "Reset" ,width = 20 , fg = "black" ,bg = "light blue" , bd = 0 , padx = 20 , pady = 10 , command = resetGame )
89
+ reset_button .grid (row = 0 , column = 1 )
90
+
91
+ root .geometry ('600x300' )
92
+ root .mainloop ()
0 commit comments