From c2ec677f3f5b5ee05f316b7f747d7dc3c87bae61 Mon Sep 17 00:00:00 2001 From: adrien Date: Tue, 1 Oct 2019 12:36:00 +0200 Subject: [PATCH] move high score pickle to tetrislogic --- TetrArcade.py | 7 +++---- tetrislogic/tetrislogic.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/TetrArcade.py b/TetrArcade.py index 0c8e286..2932d36 100644 --- a/TetrArcade.py +++ b/TetrArcade.py @@ -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): diff --git a/tetrislogic/tetrislogic.py b/tetrislogic/tetrislogic.py index a8d1b9f..18c6782 100644 --- a/tetrislogic/tetrislogic.py +++ b/tetrislogic/tetrislogic.py @@ -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.")