1+ import tkinter as tk
2+ import random
3+ from tkinter import messagebox #Import the messagebox module
4+
5+ class TileMatchingGame :
6+ def __init__ (self , root , rows , columns ):
7+ self .root = root
8+ self .rows = rows
9+ self .columns = columns
10+ self .tiles = []
11+ self .selected_tiles = []
12+ self .is_game_over = False
13+ self .create_board ()
14+ self .score = 0
15+ self .attempts = 0
16+ self .create_scoreboard ()
17+ self .create_timer (60 )
18+
19+ def create_board (self ):
20+ all_colors = ["salmon" ,"lightblue" , "azure" , "darkblue" , "orange" , "purple" , "pink" , "brown" ]
21+ colors = random .sample (all_colors , self .rows * self .columns // 2 )
22+ colors *= 2 # Duplicate colors to have pairs
23+ random .shuffle (colors )
24+
25+ for row in range (self .rows ):
26+ tile_row = []
27+ for col in range (self .columns ):
28+ tile = tk .Label (
29+ self .root ,
30+ text = "" ,
31+ width = 10 ,
32+ height = 4 ,
33+ relief = "raised" ,
34+ borderwidth = 3 ,
35+ bg = "gray" , # Initially, tiles are gray (hidden)
36+ )
37+ tile .grid (row = row , column = col )
38+ tile .bind ("<Button-1>" , self .tile_clicked )
39+ tile_row .append (tile )
40+ self .tiles .append (tile_row )
41+ self .tile_colors = colors
42+
43+ def create_scoreboard (self ):
44+ self .score_label = tk .Label (self .root , text = "Score: 0" )
45+ self .score_label .grid (row = self .rows , columnspan = self .columns )
46+ self .attempts_label = tk .Label (self .root , text = "Attempts: 0" )
47+ self .attempts_label .grid (row = self .rows + 1 , columnspan = self .columns )
48+
49+ def create_timer (self , seconds ):
50+ self .timer_label = tk .Label (self .root , text = f"Time: { seconds } " )
51+ self .timer_label .grid (row = self .rows + 2 , columnspan = self .columns )
52+ self .remaining_time = seconds
53+ self .update_timer ()
54+
55+ def update_timer (self ):
56+ if self .remaining_time > 0 and not self .is_game_over :
57+ self .remaining_time -= 1
58+ self .timer_label .config (text = f"Time: { self .remaining_time } " )
59+ self .root .after (1000 , self .update_timer )
60+ else :
61+ self .timer_label .config (text = "Time's up!" )
62+ self .is_game_over = True
63+
64+ def tile_clicked (self , event ):
65+ if self .is_game_over :
66+ return
67+
68+ tile = event .widget
69+ row , col = self .get_tile_position (tile )
70+ if (row , col ) not in self .selected_tiles and len (self .selected_tiles ) < 2 :
71+ tile .config (text = "X" , bg = self .tile_colors [row * self .columns + col ]) # Reveal the color when clicked
72+ self .selected_tiles .append ((row , col ))
73+
74+ if len (self .selected_tiles ) == 2 :
75+ self .root .update_idletasks ()
76+ self .root .after (500 , self .check_matching_tiles )
77+ self .attempts += 1
78+ self .attempts_label .config (text = f"Attempts: { self .attempts } " )
79+
80+ def check_matching_tiles (self ):
81+ if len (self .selected_tiles ) == 2 :
82+ tile1 = self .selected_tiles [0 ]
83+ tile2 = self .selected_tiles [1 ]
84+ if self .tile_colors [tile1 [0 ] * self .columns + tile1 [1 ]] == self .tile_colors [tile2 [0 ] * self .columns + tile2 [1 ]]:
85+ self .score += 1
86+ self .score_label .config (text = f"Score: { self .score } " )
87+ if self .score == self .rows * self .columns // 2 :
88+ self .end_game ()
89+
90+ else :
91+ self .tiles [tile1 [0 ]][tile1 [1 ]].config (text = "" , bg = "gray" )
92+ self .tiles [tile2 [0 ]][tile2 [1 ]].config (text = "" , bg = "gray" )
93+ self .selected_tiles = []
94+
95+ def end_game (self ):
96+ self .timer_label .config (text = "Game Over!" )
97+ self .is_game_over = True
98+ messagebox .showinfo ("Congratulations!" , "You've won the game!" )
99+
100+ def get_tile_position (self , tile ):
101+ for row , row_tiles in enumerate (self .tiles ):
102+ if tile in row_tiles :
103+ col = row_tiles .index (tile )
104+ return row , col
105+
106+ def main ():
107+ root = tk .Tk ()
108+ root .title ("Tile Matching Game" )
109+
110+ rows , columns = 4 , 4
111+
112+ game = TileMatchingGame (root , rows , columns )
113+
114+ # Exit Button
115+ exit_button = tk .Button (root , text = "Exit" , command = root .destroy )
116+ exit_button .grid (row = rows + 3 , columnspan = columns )
117+
118+ root .mainloop ()
119+
120+ if __name__ == "__main__" :
121+ main ()
0 commit comments