Skip to content

Fix icon error and add restart + feedback features to Color_Game #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions Color_Game/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import random
import tkinter as tk
from tkinter import messagebox
import os
import sys

colours = ['Red', 'Blue', 'Green', 'Yellow', 'Orange', 'Purple', 'Pink', 'Black', 'White']
score = 0
Expand All @@ -15,31 +17,29 @@ def next_colour():

if user_input == correct_color:
score += 1
feedback_label.config(text="✅ Correct!", fg="green")
elif user_input != "":
feedback_label.config(text=f"❌ Wrong! It was: {colours[1]}", fg="red")

e.delete(0, tk.END)
random.shuffle(colours)
label.config(fg=colours[1], text=colours[0])
score_label.config(text=f"Score: {score}")


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))



def load_highest_score():
try:
Expand All @@ -52,38 +52,48 @@ def load_highest_score():
except FileNotFoundError:
return 0


def scoreshow():
record_highest_score()
window2 = tk.Tk()
window2 = tk.Toplevel()
window2.title("HIGH SCORE")
window2.geometry("300x200")

label = tk.Label(window2, text=f"Highest Score: {load_highest_score()}",font=(font, 12))

label = tk.Label(window2, text=f"Highest Score: {load_highest_score()}", font=(font, 12))
label.pack()

window2.mainloop()
# ✅ Restart button that restarts everything
def restart():
window2.destroy()
window.destroy()
os.execl(sys.executable, sys.executable, *sys.argv)

restart_btn = tk.Button(window2, text="Play Again", command=restart)
restart_btn.pack(pady=10)

def start_game(event):
global timeleft
if timeleft == 30:
countdown()
next_colour()

# --- Main Window ---
window = tk.Tk()
font = 'Helvetica'
window.title("Color Game")
window.iconbitmap("color_game_icon.ico")
window.geometry("375x250")
window.geometry("375x300")
window.resizable(False, False)

# ✅ Fix: icon only if exists
icon_path = "color_game_icon.ico"
if os.path.exists(icon_path):
window.iconbitmap(icon_path)

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()

Expand All @@ -94,6 +104,10 @@ def start_game(event):
window.bind('<Return>', start_game)
e.pack()

# ✅ Inline feedback (replaces popups)
feedback_label = tk.Label(window, text="", font=(font, 10))
feedback_label.pack(pady=5)

e.focus_set()

window.mainloop()
window.mainloop()