move high score pickle to tetrislogic
This commit is contained in:
parent
11c6321f17
commit
c2ec677f3f
@ -392,7 +392,7 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
||||
def load_high_score(self):
|
||||
try:
|
||||
with open(HIGH_SCORE_PATH, "rb") as f:
|
||||
crypted_high_score = pickle.load(f)
|
||||
crypted_high_score = f.read()
|
||||
super().load_high_score(crypted_high_score)
|
||||
except:
|
||||
self.high_score = 0
|
||||
@ -403,13 +403,12 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
|
||||
os.makedirs(USER_PROFILE_DIR)
|
||||
with open(HIGH_SCORE_PATH, mode='wb') as f:
|
||||
crypted_high_score = super().save_high_score()
|
||||
pickle.dump(crypted_high_score, f, pickle.HIGHEST_PROTOCOL)
|
||||
f.write(crypted_high_score)
|
||||
except Exception as e:
|
||||
sys.exit(
|
||||
"""High score: {:n}
|
||||
High score could not be saved:
|
||||
""".format(self.high_score)
|
||||
+ str(e)
|
||||
""".format(self.high_score) + str(e)
|
||||
)
|
||||
|
||||
def start(self, task, period):
|
||||
|
@ -1,5 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import random
|
||||
import pickle
|
||||
|
||||
from .utils import Coord, Movement, Rotation, T_Spin, Line
|
||||
from .tetromino import Tetromino, T, I
|
||||
@ -398,17 +399,20 @@ class TetrisLogic():
|
||||
raise Warning("TetrisLogic.show_text not implemented.")
|
||||
|
||||
def load_high_score(self, crypted_high_score=None):
|
||||
if crypted_high_score is None:
|
||||
self.high_score = 0
|
||||
if crypted_high_score:
|
||||
crypted_high_score = int(pickle.loads(crypted_high_score))
|
||||
self.high_score = crypted_high_score ^ CRYPT_KEY
|
||||
else:
|
||||
raise Warning(
|
||||
"""TetrisLogic.load_high_score not implemented.
|
||||
High score is set to 0"""
|
||||
)
|
||||
else:
|
||||
self.high_score = crypted_high_score ^ CRYPT_KEY
|
||||
self.high_score = 0
|
||||
|
||||
def save_high_score(self):
|
||||
return self.high_score ^ CRYPT_KEY
|
||||
crypted_high_score = self.high_score ^ CRYPT_KEY
|
||||
crypted_high_score = pickle.dumps(crypted_high_score)
|
||||
return crypted_high_score
|
||||
|
||||
def start(task, period):
|
||||
raise Warning("TetrisLogic.start is not implemented.")
|
||||
|
Loading…
x
Reference in New Issue
Block a user