77CELL_PADDING = 10
88BACKGROUND_COLOR = "#92877d"
99EMPTY_CELL_COLOR = "#9e948a"
10+ TOTAL_MOVES = 100
1011
1112TILE_COLORS = {
1213 2 : "#eee4da" ,
@@ -45,6 +46,7 @@ def __init__(self, root):
4546 self .root = root
4647 self .root .title ("2048 Game" )
4748 self .root .resizable (False , False )
49+ self .moves_left = TOTAL_MOVES
4850
4951 self .score = 0
5052 self .high_score = self .load_high_score ()
@@ -59,6 +61,12 @@ def __init__(self, root):
5961 )
6062 self .score_label .grid (pady = 10 )
6163
64+ self .total_moves_label = tk .Label (
65+ root ,
66+ text = f"Moves left: { self .moves_left } " ,
67+ font = ("Arial" , 16 , "bold" )
68+ )
69+ self .total_moves_label .grid (pady = 10 )
6270 self .restart_button = tk .Button (
6371 root ,
6472 text = "Restart Game" ,
@@ -137,6 +145,7 @@ def create_grid(self):
137145 def start_new_game (self ):
138146 self .board = [[0 ] * GRID_SIZE for _ in range (GRID_SIZE )]
139147 self .score = 0
148+ self .moves_left = TOTAL_MOVES
140149
141150 self .add_new_tile ()
142151 self .add_new_tile ()
@@ -175,6 +184,8 @@ def update_grid(self):
175184 self .score_label .configure (
176185 text = f"Score: { self .score } High Score: { self .high_score } "
177186 )
187+ self .total_moves_label .configure (
188+ text = f"Moves left: { self .moves_left } " )
178189
179190 self .root .update_idletasks ()
180191
@@ -268,9 +279,12 @@ def handle_keypress(self, event):
268279
269280 else :
270281 return
282+
283+
271284
272285 if moved :
273286 self .add_new_tile ()
287+ self .moves_left -= 1
274288
275289 if self .score > self .high_score :
276290 self .high_score = self .score
@@ -282,6 +296,8 @@ def handle_keypress(self, event):
282296 # Game over is now checked even when no movement happens
283297 if self .check_game_over ():
284298 self .game_over ()
299+ if self .moves_left <= 0 :
300+ self .game_over ()
285301
286302 def game_over (self ):
287303
0 commit comments