persistent high score
This commit is contained in:
parent
d89a29cce9
commit
7d30478d4d
@ -2,6 +2,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import locale
|
import locale
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import arcade
|
import arcade
|
||||||
@ -79,7 +80,7 @@ PRESS
|
|||||||
TO PLAY
|
TO PLAY
|
||||||
AGAIN"""
|
AGAIN"""
|
||||||
|
|
||||||
# Sprites paths
|
# Paths
|
||||||
WINDOW_BG = "images/bg.jpg"
|
WINDOW_BG = "images/bg.jpg"
|
||||||
MATRIX_SPRITE_PATH = "images/matrix.png"
|
MATRIX_SPRITE_PATH = "images/matrix.png"
|
||||||
MINOES_SPRITES_PATHS = {
|
MINOES_SPRITES_PATHS = {
|
||||||
@ -91,6 +92,12 @@ MINOES_SPRITES_PATHS = {
|
|||||||
"red": "images/red_mino.png",
|
"red": "images/red_mino.png",
|
||||||
"magenta": "images/magenta_mino.png"
|
"magenta": "images/magenta_mino.png"
|
||||||
}
|
}
|
||||||
|
if sys.platform == "win32":
|
||||||
|
USER_PROFILE_DIR = os.environ.get("appdata", os.path.expanduser("~\Appdata\Roaming"))
|
||||||
|
else:
|
||||||
|
USER_PROFILE_DIR = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
|
||||||
|
USER_PROFILE_DIR = os.path.join(USER_PROFILE_DIR, "TetrArcade")
|
||||||
|
HIGH_SCORE_PATH = os.path.join(USER_PROFILE_DIR, ".high_score")
|
||||||
|
|
||||||
# Transparency (0=invisible, 255=opaque)
|
# Transparency (0=invisible, 255=opaque)
|
||||||
NORMAL_ALPHA = 200
|
NORMAL_ALPHA = 200
|
||||||
@ -123,8 +130,8 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
scheduler = ArcadeScheduler()
|
scheduler = ArcadeScheduler()
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, high_score):
|
||||||
super().__init__()
|
super().__init__(high_score)
|
||||||
|
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
|
|
||||||
@ -385,8 +392,23 @@ class TetrArcade(TetrisLogic, arcade.Window):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
TetrArcade()
|
try:
|
||||||
|
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()
|
||||||
|
|
||||||
|
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()
|
||||||
|
@ -195,8 +195,8 @@ class TetrisLogic():
|
|||||||
)
|
)
|
||||||
scheduler = AbstractScheduler()
|
scheduler = AbstractScheduler()
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, high_score=0):
|
||||||
self.high_score = 0
|
self.high_score = high_score
|
||||||
self.state = State.STARTING
|
self.state = State.STARTING
|
||||||
self.matrix = []
|
self.matrix = []
|
||||||
self.next_pieces = []
|
self.next_pieces = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user