Skip to content

Commit 1208e06

Browse files
authored
Merge pull request #263 from kateract/master
Add Saving to Micro2048
2 parents 38364f4 + c12ee52 commit 1208e06

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

Micro2048/Micro2048.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import math
22
import random
3+
import json
4+
from thumbySaves import saveData
35
z = __import__('/Games/Micro2048/obnlib')
46

57
APP_CODE = "#H01"
68
APP_NAME = "MICRO 2048"
79
APP_RELEASE = "2022"
810
APP_VERSION = "V0.1"
911
APP_FPS = 50
10-
12+
saveData.setName('Micro2048')
1113
#------------------------------------------------------------------------------
1214

1315
IMAGE_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,10 @@ class GameState:
148151

149152
def __init__(self):
150153
global continuable
151-
continuable = False
154+
if saveData.hasItem('boardState') == False:
155+
continuable = False
156+
else:
157+
continuable = True
152158

153159
def prepare(self):
154160
global continuable
@@ -161,6 +167,22 @@ def prepare(self):
161167
self.anim = 0
162168
self.gameover = False
163169
continuable = True
170+
else:
171+
self.field = Field()
172+
self.work = None
173+
self.backup = None
174+
self.anim = 0
175+
self.gameover = False
176+
mapData = saveData.getItem('boardState')
177+
jdata = json.loads(mapData)
178+
for i in range(0, FIELD_SIZE):
179+
for j in self.field.field_range():
180+
if jdata[i][j] >= 0:
181+
self.field.restore_panel(j, i, jdata[i][j])
182+
if self.field.is_playable() == False:
183+
self.gameover = True;
184+
185+
164186
self.idle = 0
165187
self.undo_msg = 0
166188
self.shake = 0.0
@@ -185,6 +207,7 @@ def update(self):
185207
self.shake = 0.0
186208
z.play("O4S4>C<GE", 15)
187209
elif z.btn_d(z.BTN_B):
210+
self.saveGameState()
188211
global continuable
189212
continuable = not self.gameover
190213
next_state = TitleState.ID
@@ -206,6 +229,7 @@ def update(self):
206229
self.field = self.work
207230
self.work = None
208231
self.field.add_new_panel()
232+
self.saveGameState()
209233
value, high = self.field.upgrade_panels()
210234
if value >= 0:
211235
z.play(UP_MML[value], value)
@@ -218,6 +242,16 @@ def update(self):
218242
z.play("O4S6ED+DS8C+C<BS12A+A", 20)
219243
self.anim = 0
220244
return next_state
245+
246+
def saveGameState(self):
247+
mapData = [[-1]*FIELD_SIZE for i in self.field.field_range()]
248+
for i in range(0, FIELD_SIZE):
249+
for j in self.field.field_range():
250+
if self.field.panels[i][j]:
251+
mapData[i][j] = self.field.panels[i][j].value
252+
json_data = json.dumps(mapData)
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):
247281
class 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

Comments
 (0)