Skip to content

Commit 84b0e55

Browse files
committed
Update main.py
1 parent 030e159 commit 84b0e55

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

Color_Game/main.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import random
22
import tkinter as tk
3+
from tkinter import messagebox
34

45
colours = ['Red', 'Blue', 'Green', 'Yellow', 'Orange', 'Purple', 'Pink', 'Black', 'White']
56
score = 0
67
timeleft = 30
7-
8-
def start_game(event):
9-
global timeleft
10-
if timeleft == 30:
11-
countdown()
12-
next_colour()
8+
highest_score = 0
139

1410
def next_colour():
1511
global score, timeleft
@@ -26,31 +22,56 @@ def next_colour():
2622
label.config(fg=colours[1], text=colours[0])
2723
score_label.config(text=f"Score: {score}")
2824

25+
2926
def countdown():
3027
global timeleft
3128
if timeleft > 0:
3229
timeleft -= 1
3330
time_label.config(text=f"Time left: {timeleft}")
3431
time_label.after(1000, countdown)
3532
else:
36-
record_highest_score()
33+
# messagebox.showwarning ('Attention', 'Your time is out!!')
34+
scoreshow()
35+
3736

3837
def record_highest_score():
3938
highest_score = load_highest_score()
4039
if score > highest_score:
41-
save_highest_score(score)
40+
with open("highest_score.txt", "w") as file:
41+
file.write(str(score))
42+
4243

43-
def save_highest_score(score):
44-
with open("highest_score.txt", "w") as file:
45-
file.write(f"The highest score is: {score}")
4644

4745
def load_highest_score():
4846
try:
4947
with open("highest_score.txt", "r") as file:
50-
return int(file.read())
48+
data = file.read()
49+
if data:
50+
return int(data)
51+
else:
52+
return 0
5153
except FileNotFoundError:
5254
return 0
5355

56+
57+
def scoreshow():
58+
record_highest_score()
59+
window2 = tk.Tk()
60+
window2.title("HIGH SCORE")
61+
window2.geometry("300x200")
62+
63+
label = tk.Label(window2, text=f"Highest Score: {load_highest_score()}",font=(font, 12))
64+
65+
label.pack()
66+
67+
window2.mainloop()
68+
69+
def start_game(event):
70+
global timeleft
71+
if timeleft == 30:
72+
countdown()
73+
next_colour()
74+
5475
window = tk.Tk()
5576
font = 'Helvetica'
5677
window.title("Color Game")
@@ -62,7 +83,7 @@ def load_highest_score():
6283

6384
score_label = tk.Label(window, text="Press Enter to start", font=(font, 12))
6485
score_label.pack()
65-
86+
6687
time_label = tk.Label(window, text=f"Time left: {timeleft}", font=(font, 12))
6788
time_label.pack()
6889

@@ -75,4 +96,4 @@ def load_highest_score():
7596

7697
e.focus_set()
7798

78-
window.mainloop()
99+
window.mainloop()

0 commit comments

Comments
 (0)