persistent high score improve
This commit is contained in:
parent
7d30478d4d
commit
2b99db6077
@ -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 main():
|
||||
def load_high_score(self):
|
||||
try:
|
||||
with open(HIGH_SCORE_PATH, "r") as f:
|
||||
high_score = int(f.read())
|
||||
self.high_score = int(f.read())
|
||||
except:
|
||||
high_score = 0
|
||||
|
||||
tetrarcade = TetrArcade(high_score)
|
||||
arcade.run()
|
||||
self.high_score = 0
|
||||
|
||||
def save_high_score(self):
|
||||
try:
|
||||
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))
|
||||
f.write(str(self.high_score))
|
||||
except Exception as e:
|
||||
print("High score could not be saved:")
|
||||
print(e)
|
||||
sys.exit(
|
||||
"""High score: {:n}
|
||||
High score could not be saved:
|
||||
""".format(self.high_score)
|
||||
+ str(e)
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
tetrarcade = TetrArcade()
|
||||
arcade.run()
|
||||
tetrarcade.save_high_score()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
@ -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"""
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user