persistent high score improve
This commit is contained in:
parent
7d30478d4d
commit
2b99db6077
@ -130,8 +130,8 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
scheduler = ArcadeScheduler()
|
scheduler = ArcadeScheduler()
|
||||||
|
|
||||||
def __init__(self, high_score):
|
def __init__(self):
|
||||||
super().__init__(high_score)
|
super().__init__()
|
||||||
|
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
|
|
||||||
@ -390,25 +390,32 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
self.matrix_sprite.top = int(self.matrix_sprite.top)
|
self.matrix_sprite.top = int(self.matrix_sprite.top)
|
||||||
self.reload_matrix()
|
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():
|
def main():
|
||||||
try:
|
tetrarcade = TetrArcade()
|
||||||
with open(HIGH_SCORE_PATH, "r") as f:
|
|
||||||
high_score = int(f.read())
|
|
||||||
except:
|
|
||||||
high_score = 0
|
|
||||||
|
|
||||||
tetrarcade = TetrArcade(high_score)
|
|
||||||
arcade.run()
|
arcade.run()
|
||||||
|
tetrarcade.save_high_score()
|
||||||
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)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
# Matrix
|
# Matrix
|
||||||
NB_LINES = 20
|
NB_LINES = 20
|
||||||
NB_COLS = 10
|
NB_COLS = 10
|
||||||
@ -65,10 +64,10 @@ class T_Spin:
|
|||||||
class AbstractScheduler:
|
class AbstractScheduler:
|
||||||
|
|
||||||
def start(task, period):
|
def start(task, period):
|
||||||
raise NotImplementedError
|
raise Warning("AbstractScheduler.start is not implemented.")
|
||||||
|
|
||||||
def stop(self, task):
|
def stop(self, task):
|
||||||
raise NotImplementedError
|
raise Warning("AbstractScheduler.stop is not implemented.")
|
||||||
|
|
||||||
def restart(self, task, period):
|
def restart(self, task, period):
|
||||||
self.stop(task)
|
self.stop(task)
|
||||||
@ -195,8 +194,8 @@ class TetrisLogic():
|
|||||||
)
|
)
|
||||||
scheduler = AbstractScheduler()
|
scheduler = AbstractScheduler()
|
||||||
|
|
||||||
def __init__(self, high_score=0):
|
def __init__(self):
|
||||||
self.high_score = high_score
|
self.load_high_score()
|
||||||
self.state = State.STARTING
|
self.state = State.STARTING
|
||||||
self.matrix = []
|
self.matrix = []
|
||||||
self.next_pieces = []
|
self.next_pieces = []
|
||||||
@ -476,6 +475,7 @@ class TetrisLogic():
|
|||||||
self.scheduler.stop(self.drop)
|
self.scheduler.stop(self.drop)
|
||||||
self.scheduler.stop(self.update_time)
|
self.scheduler.stop(self.update_time)
|
||||||
self.scheduler.stop(self.repeat_action)
|
self.scheduler.stop(self.repeat_action)
|
||||||
|
self.save_high_score()
|
||||||
|
|
||||||
def update_time(self, delta_time=1):
|
def update_time(self, delta_time=1):
|
||||||
self.time += delta_time
|
self.time += delta_time
|
||||||
@ -511,5 +511,19 @@ class TetrisLogic():
|
|||||||
self.scheduler.stop(self.repeat_action)
|
self.scheduler.stop(self.repeat_action)
|
||||||
|
|
||||||
def show_text(self, text):
|
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