11import tkinter as tk
22import random
3- from tkinter import messagebox # Import the messagebox module
4-
3+ from tkinter import messagebox
54
65class 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
122128def 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-
137146if __name__ == "__main__" :
138147 main ()
0 commit comments