@@ -75,9 +75,17 @@ def __init__(self, root):
7575 )
7676 self .restart_button .grid (pady = 5 )
7777
78+ self .reset_high_score_button = tk .Button (
79+ root ,
80+ text = "Reset High Score" ,
81+ font = ("Arial" , 12 , "bold" ),
82+ command = self .reset_high_score
83+ )
84+ self .reset_high_score_button .grid (pady = 5 )
85+
7886 self .instruction_label = tk .Label (
7987 root ,
80- text = "🎮 Controls: ← ↑ → ↓ | Merge same numbers | Goal: 2048 🎯" ,
88+ text = "🎮 Controls: WASD / ← ↑ → ↓ | Merge same numbers | Goal: 2048 🎯" ,
8189 font = ("Arial" , 10 ),
8290 fg = "#6a635b" ,
8391 )
@@ -87,6 +95,8 @@ def __init__(self, root):
8795 self .board = [[0 ] * GRID_SIZE for _ in range (GRID_SIZE )]
8896
8997 self .game_over_label = None
98+ self .win_label = None
99+ self .game_won = False
90100
91101 self .create_grid ()
92102 self .start_new_game ()
@@ -107,6 +117,18 @@ def save_high_score(self):
107117 except OSError as e :
108118 print (f"Warning: Could not save high score: { e } " )
109119
120+ def reset_high_score (self ):
121+ """Reset the high score to 0 and clear the saved high score file."""
122+ self .high_score = 0
123+ try :
124+ if HIGH_SCORE_PATH .exists ():
125+ HIGH_SCORE_PATH .unlink ()
126+ except OSError as e :
127+ print (f"Warning: Could not delete high score file: { e } " )
128+ self .score_label .configure (
129+ text = f"Score: { self .score } High Score: { self .high_score } "
130+ )
131+
110132 def create_grid (self ):
111133 for row in range (GRID_SIZE ):
112134 row_cells = []
@@ -146,6 +168,7 @@ def start_new_game(self):
146168 self .board = [[0 ] * GRID_SIZE for _ in range (GRID_SIZE )]
147169 self .score = 0
148170 self .moves_left = TOTAL_MOVES
171+ self .game_won = False
149172
150173 self .add_new_tile ()
151174 self .add_new_tile ()
@@ -261,44 +284,58 @@ def check_game_over(self):
261284
262285 return True
263286
264- def handle_keypress (self , event ):
265- key = event .keysym
266- moved = False
267-
268- if key == "Left" :
269- moved = self .move_left ()
287+ def check_win (self ):
288+ return any (2048 in row for row in self .board )
270289
271- elif key == "Right" :
272- moved = self .move_right ()
273-
274- elif key == "Up" :
275- moved = self .move_up ()
276-
277- elif key == "Down" :
278- moved = self .move_down ()
279-
280- else :
290+ def handle_keypress (self , event ):
291+ key = event .keysym .lower () if len (event .keysym ) == 1 else event .keysym
292+ move_map = {
293+ "Left" : self .move_left , "a" : self .move_left ,
294+ "Right" : self .move_right , "d" : self .move_right ,
295+ "Up" : self .move_up , "w" : self .move_up ,
296+ "Down" : self .move_down , "s" : self .move_down ,
297+ }
298+ if key not in move_map :
281299 return
282-
283-
300+
301+ moved = move_map [ key ]()
284302
285303 if moved :
286304 self .add_new_tile ()
287- self .moves_left -= 1
305+ self .moves_left -= 1
288306
289307 if self .score > self .high_score :
290308 self .high_score = self .score
291309 self .save_high_score ()
292310
293311 self .update_grid ()
294312
295- # FIXED BUG:
296- # Game over is now checked even when no movement happens
297- if self .check_game_over ():
298- self .game_over ()
299- if self .moves_left <= 0 :
313+ if not self .game_won and self .check_win ():
314+ self .you_win ()
315+ return
316+
317+ # Check for game over even if no movement happens
318+ if self .check_game_over () or self .moves_left <= 0 :
300319 self .game_over ()
301320
321+ def you_win (self ):
322+
323+ if self .game_won :
324+ return
325+
326+ self .game_won = True
327+
328+ self .root .unbind ("<Key>" )
329+
330+ self .win_label = tk .Label (
331+ self .root ,
332+ text = "🎉 YOU WIN! 🎉" ,
333+ font = ("Arial" , 24 , "bold" ),
334+ fg = "green"
335+ )
336+
337+ self .win_label .grid (pady = 10 )
338+
302339 def game_over (self ):
303340
304341 # Prevent duplicate labels
@@ -324,6 +361,11 @@ def restart_game(self):
324361 self .game_over_label .destroy ()
325362 self .game_over_label = None
326363
364+ # Remove old win label
365+ if self .win_label is not None :
366+ self .win_label .destroy ()
367+ self .win_label = None
368+
327369 self .start_new_game ()
328370
329371 # Re-enable keyboard controls
@@ -335,5 +377,6 @@ def main():
335377 game = Game2048 (root )
336378 root .mainloop ()
337379
380+
338381if __name__ == "__main__" :
339382 main ()
0 commit comments