File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import tkinter as tk
22import random
33import os
4+ from pathlib import Path
45
56GRID_SIZE = 4
67CELL_SIZE = 100
3738}
3839
3940HIGH_SCORE_FILE = "highscore.txt"
41+ HIGH_SCORE_PATH = Path (__file__ ).with_name (HIGH_SCORE_FILE )
4042
4143
4244class Game2048 :
@@ -76,14 +78,15 @@ def __init__(self, root):
7678 self .root .bind ("<Key>" , self .handle_keypress )
7779
7880 def load_high_score (self ):
79- if os .path .exists (HIGH_SCORE_FILE ):
80- with open (HIGH_SCORE_FILE , "r" ) as file :
81- return int (file .read ())
81+ if HIGH_SCORE_PATH .exists ():
82+ try :
83+ return int (HIGH_SCORE_PATH .read_text ().strip () or 0 )
84+ except ValueError :
85+ return 0
8286 return 0
8387
8488 def save_high_score (self ):
85- with open (HIGH_SCORE_FILE , "w" ) as file :
86- file .write (str (self .high_score ))
89+ HIGH_SCORE_PATH .write_text (str (self .high_score ))
8790
8891 def create_grid (self ):
8992 for row in range (GRID_SIZE ):
You can’t perform that action at this time.
0 commit comments