-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (79 loc) · 2.87 KB
/
main.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
import random
import tkinter as tk
from tkinter import messagebox
#Imports necessary modules: random for shuffling colors, tkinter for creating the GUI, and messagebox for showing information dialogs.
colours = ['Red', 'Blue', 'Green', 'Yellow', 'Orange', 'Purple', 'Pink', 'Black', 'White']
score = 0
timeleft = 30
#Initializes a list of colors, sets the initial score to 0, and initializes a timer (timeleft) to 30 seconds.
def next_colour():
global score, timeleft
if timeleft > 0:
user_input = e.get().lower()
correct_color = colours[1].lower()
if user_input == correct_color:
score += 1
e.delete(0, tk.END)
random.shuffle(colours)
label.config(fg=colours[1], text=colours[0])
score_label.config(text=f"Score: {score}")
#Defines a function next_colour that is called when the user enters a color.
#Checks if there's still time left and compares the user's input to the correct color.
#If correct, increments the score, clears the entry (e), shuffles colors, and updates the display.
def countdown():
global timeleft
if timeleft > 0:
timeleft -= 1
time_label.config(text=f"Time left: {timeleft}")
time_label.after(1000, countdown)
else:
# messagebox.showwarning ('Attention', 'Your time is out!!')
scoreshow()
def record_highest_score():
highest_score = load_highest_score()
if score > highest_score:
with open("highest_score.txt", "w") as file:
file.write(str(score))
#Defines a countdown function that decrements the timer and updates the display every second.
#Calls scoreshow function when time runs out
def load_highest_score():
try:
with open("highest_score.txt", "r") as file:
data = file.read()
if data:
return int(data)
else:
return 0
except FileNotFoundError:
return 0
def scoreshow():
record_highest_score()
window2 = tk.Tk()
window2.title("HIGH SCORE")
window2.geometry("300x200")
label = tk.Label(window2, text=f"Highest Score: {load_highest_score()}",font=(font, 12))
label.pack()
window2.mainloop()
def start_game(event):
global timeleft
if timeleft == 30:
countdown()
next_colour()
window = tk.Tk()
font = 'Helvetica'
window.title("Color Game")
window.geometry("375x250")
window.resizable(False, False)
instructions = tk.Label(window, text="Enter the color of the text, not the word!", font=(font, 12))
instructions.pack(pady=10)
score_label = tk.Label(window, text="Press Enter to start", font=(font, 12))
score_label.pack()
time_label = tk.Label(window, text=f"Time left: {timeleft}", font=(font, 12))
time_label.pack()
label = tk.Label(window, font=(font, 60))
label.pack(pady=20)
e = tk.Entry(window)
window.bind('<Return>', start_game)
e.pack()
e.focus_set()
window.mainloop()