forked from Hansajith98/hacktoberfest2021-Excluded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor_game.py
More file actions
124 lines (64 loc) · 2.93 KB
/
Color_game.py
File metadata and controls
124 lines (64 loc) · 2.93 KB
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
import tkinter
import random
class ColorGame:
colors = ["Red", "Yellow", "Orange", "Green", "Blue", "Purple", "Black", "White", "Pink"]
time_left = 30
score = 0
def __init__(self, window):
window.title("Color Game")
window.geometry("400x250")
self.instruction_label = tkinter.Label(window, text="Type the color of the word, not the text!",
font=("Century", 14))
self.instruction_label.pack(pady=10)
self.start_label = tkinter.Label(window, text="Press Enter to Start.", font=("Copperplate Gothic Light", 12))
self.start_label.pack()
self.score_label = tkinter.Label(window, text="Score: 0", font=("Comic Sans MS", 14))
self.time_label = tkinter.Label(window, text="Time Left: " + str(self.time_left),
font=("Arial", 14))
self.color_text_label = tkinter.Label(window, fg=self.colors[0], text="Color", font=("Times", 40))
self.entry = tkinter.Entry(window, font=("Arial", 14))
self.play_again_button = tkinter.Button(window, text="Play Again", font=("Arial", 14), padx=3,
pady=3, background="Green", fg="White", command=self.restart)
window.bind("<Return>", self.play_game)
def play_game(self, event):
self.start_label.pack_forget()
self.score_label.pack()
self.time_label.pack()
self.color_text_label.pack()
self.entry.pack(fill="x", padx=100)
if self.time_left > 0:
self.entry.focus_set()
if self.time_left == 30:
self.countdown()
self.change_color()
else:
self.color_text_label.pack_forget()
self.entry.pack_forget()
self.play_again_button.pack(pady=10)
def change_color(self):
self.check_answer()
random.shuffle(self.colors)
self.color_text_label.config(fg=self.colors[0], text=self.colors[1])
self.color_text_label.pack(pady=13)
def check_answer(self):
if self.entry.get().lower() == self.colors[0].lower():
self.score += 1
self.score_label.config(text="Score: " + str(self.score))
self.entry.delete(0, tkinter.END)
def countdown(self):
if self.time_left > 0:
self.time_left -= 1
self.time_label.config(text="Time Left: " + str(self.time_left))
self.time_label.after(1000, self.countdown)
def restart(self):
self.score = 0
self.time_left = 30
self.score_label.config(text="Score: 0")
self.time_label.config(text="Time Left: " + str(self.time_left))
self.play_again_button.pack_forget()
self.score_label.pack_forget()
self.time_label.pack_forget()
self.start_label.pack()
new_window = tkinter.Tk()
game = ColorGame(new_window)
new_window.mainloop()