From 2b99db607767d85df02c71312f1e1ffcdfa0ec16 Mon Sep 17 00:00:00 2001 From: adrienmalin <41926238+adrienmalin@users.noreply.github.com> Date: Sat, 28 Sep 2019 15:55:04 +0200 Subject: [PATCH] persistent high score improve --- tetrarcade.py | 43 +++++++++++++++++++++++++------------------ tetrislogic.py | 26 ++++++++++++++++++++------ 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/tetrarcade.py b/tetrarcade.py index aae6415..1482692 100644 --- a/tetrarcade.py +++ b/tetrarcade.py @@ -130,8 +130,8 @@ class TetrArcade(TetrisLogic, arcade.Window): scheduler = ArcadeScheduler() - def __init__(self, high_score): - super().__init__(high_score) + def __init__(self): + super().__init__() locale.setlocale(locale.LC_ALL, '') @@ -390,25 +390,32 @@ class TetrArcade(TetrisLogic, arcade.Window): self.matrix_sprite.top = int(self.matrix_sprite.top) self.reload_matrix() + def load_high_score(self): + try: + with open(HIGH_SCORE_PATH, "r") as f: + self.high_score = int(f.read()) + except: + self.high_score = 0 + + def save_high_score(self): + try: + if not os.path.exists(USER_PROFILE_DIR): + os.makedirs(USER_PROFILE_DIR) + with open(HIGH_SCORE_PATH, mode='w') as f: + f.write(str(self.high_score)) + except Exception as e: + sys.exit( + """High score: {:n} +High score could not be saved: +""".format(self.high_score) + + str(e) + ) + def main(): - try: - with open(HIGH_SCORE_PATH, "r") as f: - high_score = int(f.read()) - except: - high_score = 0 - - tetrarcade = TetrArcade(high_score) + tetrarcade = TetrArcade() arcade.run() - - if not os.path.exists(USER_PROFILE_DIR): - os.makedirs(USER_PROFILE_DIR) - try: - with open(HIGH_SCORE_PATH, mode='w') as f: - f.write(str(tetrarcade.high_score)) - except Exception as e: - print("High score could not be saved:") - print(e) + tetrarcade.save_high_score() if __name__ == "__main__": main() diff --git a/tetrislogic.py b/tetrislogic.py index a56478a..f8a90aa 100644 --- a/tetrislogic.py +++ b/tetrislogic.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import random - # Matrix NB_LINES = 20 NB_COLS = 10 @@ -65,10 +64,10 @@ class T_Spin: class AbstractScheduler: def start(task, period): - raise NotImplementedError + raise Warning("AbstractScheduler.start is not implemented.") def stop(self, task): - raise NotImplementedError + raise Warning("AbstractScheduler.stop is not implemented.") def restart(self, task, period): self.stop(task) @@ -195,8 +194,8 @@ class TetrisLogic(): ) scheduler = AbstractScheduler() - def __init__(self, high_score=0): - self.high_score = high_score + def __init__(self): + self.load_high_score() self.state = State.STARTING self.matrix = [] self.next_pieces = [] @@ -476,6 +475,7 @@ class TetrisLogic(): self.scheduler.stop(self.drop) self.scheduler.stop(self.update_time) self.scheduler.stop(self.repeat_action) + self.save_high_score() def update_time(self, delta_time=1): self.time += delta_time @@ -511,5 +511,19 @@ class TetrisLogic(): self.scheduler.stop(self.repeat_action) def show_text(self, text): - raise NotImplementedError + print(text) + raise Warning("TetrisLogic.show_text not implemented.") + + def load_high_score(self): + self.high_score = 0 + raise Warning( + """TetrisLogic.load_high_score not implemented. +High score is set to 0""" + ) + + def save_high_score(self): + raise Warning( + """TetrisLogic.save_high_score not implemented. +High score is not saved""" + )