Skip to content

Commit

Permalink
Merge pull request #698 from KhwajaYousuf/feature/enhanced-tile-match…
Browse files Browse the repository at this point in the history
…ing-game

Feature/enhanced tile matching game
  • Loading branch information
Mrinank-Bhowmick authored Feb 10, 2024
2 parents fe3b482 + 981dac3 commit 1fd285e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
37 changes: 24 additions & 13 deletions projects/Alarm Clock/clock.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
# Importing the necessary modules
import tkinter
from time import strftime

# Creating the main application window
top = tkinter.Tk()
top.title("Clock") # Setting the title of the window
top.title("Dynamic Clock") # Updated title
top.resizable(0, 0) # Making the window non-resizable

# Function to determine the time of day
def get_time_of_day(hour):
if 5 <= hour < 12:
return "Morning"
elif 12 <= hour < 18:
return "Afternoon"
else:
return "Evening"

# Function to update the time display
def time():
# Get the current time in the format HH:MM:SS AM/PM
string = strftime("%H:%M:%S %p")
def update_time():
current_time = strftime("%H:%M:%S %p")
hour = int(strftime("%H"))
time_of_day = get_time_of_day(hour)

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

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

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

# Creating a Label widget to display the time
clockTime = tkinter.Label(
clock_time = tkinter.Label(
top,
font=("courier new", 40),
background="black",
foreground="white",
)

# Position the Label widget in the center of the window
clockTime.pack(anchor="center")
clock_time.pack(anchor="center")

# Call the time function to start updating the time display
time()
# Call the update_time function to start updating the time display
update_time()

# Start the Tkinter main event loop
top.mainloop()
39 changes: 24 additions & 15 deletions projects/Tile Matching/tile_matching.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import tkinter as tk
import random
from tkinter import messagebox # Import the messagebox module

from tkinter import messagebox

class TileMatchingGame:
def __init__(self, root, rows, columns):
Expand All @@ -19,14 +18,14 @@ def __init__(self, root, rows, columns):

def create_board(self):
all_colors = [
"salmon",
"lightblue",
"azure",
"darkblue",
"orange",
"purple",
"pink",
"brown",
"lightcoral",
"lightseagreen",
"lightsteelblue",
"lightgoldenrodyellow",
"lightsalmon",
"lightgreen",
"lightpink",
"lightcyan",
]
colors = random.sample(all_colors, self.rows * self.columns // 2)
colors *= 2 # Duplicate colors to have pairs
Expand Down Expand Up @@ -103,9 +102,13 @@ def check_matching_tiles(self):
self.end_game()

else:
self.tiles[tile1[0]][tile1[1]].config(text="", bg="gray")
self.tiles[tile2[0]][tile2[1]].config(text="", bg="gray")
self.selected_tiles = []
self.root.update_idletasks()
self.root.after(500, self.hide_unmatched_tiles, tile1, tile2)

def hide_unmatched_tiles(self, tile1, tile2):
self.tiles[tile1[0]][tile1[1]].config(text="", bg="gray")
self.tiles[tile2[0]][tile2[1]].config(text="", bg="gray")
self.selected_tiles = []

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

def reset_game(self):
self.root.destroy()
main()

def main():
root = tk.Tk()
Expand All @@ -127,12 +133,15 @@ def main():

game = TileMatchingGame(root, rows, columns)

# Reset Button
reset_button = tk.Button(root, text="Reset Game", command=game.reset_game)
reset_button.grid(row=rows + 3, columnspan=columns)

# Exit Button
exit_button = tk.Button(root, text="Exit", command=root.destroy)
exit_button.grid(row=rows + 3, columnspan=columns)
exit_button.grid(row=rows + 4, columnspan=columns)

root.mainloop()


if __name__ == "__main__":
main()

0 comments on commit 1fd285e

Please sign in to comment.