Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 1fd285e

Browse files
Merge pull request #698 from KhwajaYousuf/feature/enhanced-tile-matching-game
Feature/enhanced tile matching game
2 parents fe3b482 + 981dac3 commit 1fd285e

2 files changed

Lines changed: 48 additions & 28 deletions

File tree

projects/Alarm Clock/clock.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1-
# Importing the necessary modules
21
import tkinter
32
from time import strftime
43

54
# Creating the main application window
65
top = tkinter.Tk()
7-
top.title("Clock") # Setting the title of the window
6+
top.title("Dynamic Clock") # Updated title
87
top.resizable(0, 0) # Making the window non-resizable
98

9+
# Function to determine the time of day
10+
def get_time_of_day(hour):
11+
if 5 <= hour < 12:
12+
return "Morning"
13+
elif 12 <= hour < 18:
14+
return "Afternoon"
15+
else:
16+
return "Evening"
1017

1118
# Function to update the time display
12-
def time():
13-
# Get the current time in the format HH:MM:SS AM/PM
14-
string = strftime("%H:%M:%S %p")
19+
def update_time():
20+
current_time = strftime("%H:%M:%S %p")
21+
hour = int(strftime("%H"))
22+
time_of_day = get_time_of_day(hour)
1523

16-
# Update the text of the clockTime Label with the current time
17-
clockTime.config(text=string)
24+
# Update the text of the clockTime Label with the current time and time of day
25+
clock_time.config(text=current_time + f"\nGood {time_of_day}!")
1826

19-
# Schedule the time function to be called again after 1000 milliseconds (1 second)
20-
clockTime.after(1000, time)
27+
# Dynamically change the background color based on time of day
28+
color = "lightblue" if time_of_day == "Morning" else "lightyellow" if time_of_day == "Afternoon" else "lightcoral"
29+
top.configure(background=color)
2130

31+
# Schedule the update_time function to be called again after 1000 milliseconds (1 second)
32+
clock_time.after(1000, update_time)
2233

2334
# Creating a Label widget to display the time
24-
clockTime = tkinter.Label(
35+
clock_time = tkinter.Label(
2536
top,
2637
font=("courier new", 40),
2738
background="black",
2839
foreground="white",
2940
)
3041

3142
# Position the Label widget in the center of the window
32-
clockTime.pack(anchor="center")
43+
clock_time.pack(anchor="center")
3344

34-
# Call the time function to start updating the time display
35-
time()
45+
# Call the update_time function to start updating the time display
46+
update_time()
3647

3748
# Start the Tkinter main event loop
3849
top.mainloop()

projects/Tile Matching/tile_matching.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import tkinter as tk
22
import random
3-
from tkinter import messagebox # Import the messagebox module
4-
3+
from tkinter import messagebox
54

65
class TileMatchingGame:
76
def __init__(self, root, rows, columns):
@@ -19,14 +18,14 @@ def __init__(self, root, rows, columns):
1918

2019
def create_board(self):
2120
all_colors = [
22-
"salmon",
23-
"lightblue",
24-
"azure",
25-
"darkblue",
26-
"orange",
27-
"purple",
28-
"pink",
29-
"brown",
21+
"lightcoral",
22+
"lightseagreen",
23+
"lightsteelblue",
24+
"lightgoldenrodyellow",
25+
"lightsalmon",
26+
"lightgreen",
27+
"lightpink",
28+
"lightcyan",
3029
]
3130
colors = random.sample(all_colors, self.rows * self.columns // 2)
3231
colors *= 2 # Duplicate colors to have pairs
@@ -103,9 +102,13 @@ def check_matching_tiles(self):
103102
self.end_game()
104103

105104
else:
106-
self.tiles[tile1[0]][tile1[1]].config(text="", bg="gray")
107-
self.tiles[tile2[0]][tile2[1]].config(text="", bg="gray")
108-
self.selected_tiles = []
105+
self.root.update_idletasks()
106+
self.root.after(500, self.hide_unmatched_tiles, tile1, tile2)
107+
108+
def hide_unmatched_tiles(self, tile1, tile2):
109+
self.tiles[tile1[0]][tile1[1]].config(text="", bg="gray")
110+
self.tiles[tile2[0]][tile2[1]].config(text="", bg="gray")
111+
self.selected_tiles = []
109112

110113
def end_game(self):
111114
self.timer_label.config(text="Game Over!")
@@ -118,6 +121,9 @@ def get_tile_position(self, tile):
118121
col = row_tiles.index(tile)
119122
return row, col
120123

124+
def reset_game(self):
125+
self.root.destroy()
126+
main()
121127

122128
def main():
123129
root = tk.Tk()
@@ -127,12 +133,15 @@ def main():
127133

128134
game = TileMatchingGame(root, rows, columns)
129135

136+
# Reset Button
137+
reset_button = tk.Button(root, text="Reset Game", command=game.reset_game)
138+
reset_button.grid(row=rows + 3, columnspan=columns)
139+
130140
# Exit Button
131141
exit_button = tk.Button(root, text="Exit", command=root.destroy)
132-
exit_button.grid(row=rows + 3, columnspan=columns)
142+
exit_button.grid(row=rows + 4, columnspan=columns)
133143

134144
root.mainloop()
135145

136-
137146
if __name__ == "__main__":
138147
main()

0 commit comments

Comments
 (0)