11import math
22import random
3+ import json
4+ from thumbySaves import saveData
35z = __import__ ('/Games/Micro2048/obnlib' )
46
57APP_CODE = "#H01"
68APP_NAME = "MICRO 2048"
79APP_RELEASE = "2022"
810APP_VERSION = "V0.1"
911APP_FPS = 50
10-
12+ saveData . setName ( 'Micro2048' )
1113#------------------------------------------------------------------------------
1214
1315IMAGE_TITLE = z .Image (
@@ -69,6 +71,7 @@ def menu_continue(self):
6971 def menu_new_game (self ):
7072 global continuable
7173 continuable = False
74+ saveData .delItem ('boardState' )
7275 self .start = True
7376
7477 def menu_sound (self ):
@@ -148,7 +151,11 @@ class GameState:
148151
149152 def __init__ (self ):
150153 global continuable
151- continuable = False
154+ if saveData .hasItem ('boardState' ) == False :
155+
156+ continuable = False
157+ else :
158+ continuable = True
152159
153160 def prepare (self ):
154161 global continuable
@@ -161,6 +168,20 @@ def prepare(self):
161168 self .anim = 0
162169 self .gameover = False
163170 continuable = True
171+ else :
172+ self .field = Field ()
173+ self .work = None
174+ self .backup = None
175+ self .anim = 0
176+ self .gameover = False
177+ mapData = saveData .getItem ('boardState' )
178+ jdata = json .loads (mapData )
179+ for i in range (0 , FIELD_SIZE ):
180+ for j in self .field .field_range ():
181+ if jdata [i ][j ] >= 0 :
182+ self .field .restore_panel (j , i , jdata [i ][j ])
183+
184+
164185 self .idle = 0
165186 self .undo_msg = 0
166187 self .shake = 0.0
@@ -185,6 +206,7 @@ def update(self):
185206 self .shake = 0.0
186207 z .play ("O4S4>C<GE" , 15 )
187208 elif z .btn_d (z .BTN_B ):
209+ self .saveGameState ()
188210 global continuable
189211 continuable = not self .gameover
190212 next_state = TitleState .ID
@@ -206,6 +228,7 @@ def update(self):
206228 self .field = self .work
207229 self .work = None
208230 self .field .add_new_panel ()
231+ self .saveGameState ()
209232 value , high = self .field .upgrade_panels ()
210233 if value >= 0 :
211234 z .play (UP_MML [value ], value )
@@ -218,6 +241,17 @@ def update(self):
218241 z .play ("O4S6ED+DS8C+C<BS12A+A" , 20 )
219242 self .anim = 0
220243 return next_state
244+
245+ def saveGameState (self ):
246+ mapData = [[- 1 ]* FIELD_SIZE for i in self .field .field_range ()]
247+ for i in range (0 , FIELD_SIZE ):
248+ for j in self .field .field_range ():
249+ if self .field .panels [i ][j ]:
250+ mapData [i ][j ] = self .field .panels [i ][j ].value
251+ json_data = json .dumps (mapData )
252+ print (json_data )
253+ saveData .setItem ('boardState' , json_data )
254+ saveData .save ()
221255
222256 def draw (self ):
223257 z .cls ()
@@ -247,6 +281,7 @@ def __init__(self, value, up=False, fresh=False):
247281class Field :
248282
249283 def __init__ (self , src = None ):
284+
250285 self .panels = [[None ]* FIELD_SIZE for i in self .field_range ()]
251286 self .vx = 0
252287 self .vy = 0
@@ -323,7 +358,11 @@ def put_slid_panel(self, px, py, vx, vy, value):
323358 if not up :
324359 self .empty -= 1
325360 return move
326-
361+
362+ def restore_panel (self , x , y , value ):
363+ self .panels [y ][x ] = Panel (value , fresh = True )
364+ self .empty -= 1
365+
327366 def add_new_panel (self ):
328367 x = 0
329368 y = 0
0 commit comments