summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/sliceitoff/game/__init__.py3
-rw-r--r--src/sliceitoff/game/game.py33
-rw-r--r--src/sliceitoff/game/initials.py (renamed from src/sliceitoff/game/hiscore.py)8
-rw-r--r--src/sliceitoff/game/show.py7
-rw-r--r--src/sliceitoff/hiscores/__init__.py1
-rw-r--r--src/sliceitoff/hiscores/hiscores.py55
-rw-r--r--src/sliceitoff/screens/__init__.py3
-rw-r--r--src/sliceitoff/screens/hiscores.py9
-rw-r--r--src/sliceitoff/screens/initials.py (renamed from src/sliceitoff/screens/hiscore.py)2
9 files changed, 99 insertions, 22 deletions
diff --git a/src/sliceitoff/game/__init__.py b/src/sliceitoff/game/__init__.py
index cf0da1e..a2771db 100644
--- a/src/sliceitoff/game/__init__.py
+++ b/src/sliceitoff/game/__init__.py
@@ -1,4 +1 @@
from .game import Game
-from .gameplay import Gameplay
-from .level import Level
-from .show import Show
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py
index fd796e9..0b67d2f 100644
--- a/src/sliceitoff/game/game.py
+++ b/src/sliceitoff/game/game.py
@@ -9,11 +9,12 @@ import pygame
from display import Display
from images import Images, Fonts
from stats import Stats
-from screens import welcome_screen
+from screens import welcome_screen, hiscores_screen
+from hiscores import HiScores
from .level import Level
from .show import Show
-from .hiscore import HiScore
+from .initials import Initials
class Game:
def __init__(self):
@@ -25,12 +26,21 @@ class Game:
Fonts.load_fonts( Path(__file__).parent.parent.resolve() )
+ self.hiscores = HiScores()
+
def welcome(self):
ws = Show(welcome_screen())
dt = 0
while ws.step(dt):
dt = self.clock.tick()
- self.display.update( [ws.sprites] )
+ self.display.update( [ws] )
+
+ def show_highscores(self):
+ his = Show(hiscores_screen(str(self.hiscores)))
+ dt = 0
+ while his.step(dt):
+ dt = self.clock.tick()
+ self.display.update( [his] )
def newgame(self):
Stats.new_game()
@@ -43,18 +53,21 @@ class Game:
if Stats.lives:
Stats.level_up()
- def hiscore(self):
- hs = HiScore()
- dt = 0
- while hs.step():
+ def initials(self):
+ initials = Initials()
+ dt = 0
+ while initials.step():
dt = self.clock.tick()
- # hs.update(dt = dt)
- self.display.update([hs])
+ self.display.update([initials])
+ return initials.name
def run(self):
self.welcome()
self.newgame()
- self.hiscore()
+ if self.hiscores.high_enough(Stats.score):
+ self.hiscores.add( Stats.score, self.initials())
+ self.show_highscores()
+
def __del__(self):
pygame.quit()
diff --git a/src/sliceitoff/game/hiscore.py b/src/sliceitoff/game/initials.py
index 734ad5e..5b25195 100644
--- a/src/sliceitoff/game/hiscore.py
+++ b/src/sliceitoff/game/initials.py
@@ -1,11 +1,11 @@
import pygame
-from screens import hiscore_screen
+from screens import initials_screen
-class HiScore(pygame.sprite.Group):
+class Initials(pygame.sprite.Group):
def __init__(self):
super().__init__()
- self.add(hiscore_screen(""))
+ self.add(initials_screen(""))
self.name = ""
def update(self, **kwargs):
@@ -40,5 +40,5 @@ class HiScore(pygame.sprite.Group):
self.name += pygame.key.name(event.key)[0].upper()
self.name = self.name[:8]
self.empty()
- self.add(hiscore_screen(self.name))
+ self.add(initials_screen(self.name))
return True
diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py
index 50ed0ba..07146c3 100644
--- a/src/sliceitoff/game/show.py
+++ b/src/sliceitoff/game/show.py
@@ -2,10 +2,11 @@
import pygame
from .anykey import anykey
-class Show:
+class Show(pygame.sprite.Group):
""" To show some sprites and quit on any key """
def __init__(self, sprites = []):
- self.sprites = sprites
+ super().__init__()
+ self.add(sprites)
self.fadeout = 1_000
self.timeout = 5_000
@@ -17,7 +18,7 @@ class Show:
if anykey():
return False
self.fadeout -= dt
- self.sprites.update(explode=dt)
+ self.update(explode=dt)
return True
if anykey():
diff --git a/src/sliceitoff/hiscores/__init__.py b/src/sliceitoff/hiscores/__init__.py
new file mode 100644
index 0000000..f3d7811
--- /dev/null
+++ b/src/sliceitoff/hiscores/__init__.py
@@ -0,0 +1 @@
+from .hiscores import HiScores
diff --git a/src/sliceitoff/hiscores/hiscores.py b/src/sliceitoff/hiscores/hiscores.py
new file mode 100644
index 0000000..098f0e3
--- /dev/null
+++ b/src/sliceitoff/hiscores/hiscores.py
@@ -0,0 +1,55 @@
+import os
+
+MAX_HIGHSCORES = 20
+
+class HiScores:
+ def __init__(self):
+ self.table=[]
+ self.config_filename = os.path.join(
+ os.getenv('HOME'),
+ ".config",
+ "sliceitoffrc")
+ if not os.path.isfile(self.config_filename):
+ self.table=[(0,"") for _ in range(MAX_HIGHSCORES)]
+ return
+ with open(self.config_filename, "r") as config_file:
+ for line in config_file:
+ option, value = line.split('=')
+ if option == 'hiscore':
+ score, name = value.split('!')
+ self.add(int(score.strip()),name.strip())
+ if len(self.table)<MAX_HIGHSCORES:
+ self.table+=[(0,"") for _ in range(MAX_HIGHSCORES-len(self.table))]
+
+ def add(self, score, initials):
+ self.table.append( (score, initials) )
+ self.table.sort(reverse=True)
+ self.table = self.table[:MAX_HIGHSCORES]
+
+ def high_enough(self, score):
+ return self.table[-1][0] < score
+
+ def __del__(self):
+ oldlines=[]
+
+ if os.path.isfile(self.config_filename):
+ with open(self.config_filename, "r") as config_file:
+ for line in config_file:
+ option, _ = line.split('=')
+ if option != 'hiscore':
+ oldlines.append(line)
+
+ with open(self.config_filename, 'w') as config_file:
+ config_file.writelines(oldlines)
+ for score, name in self.table:
+ config_file.write(f"hiscore={score}!{name}\n")
+
+ def __str__(self):
+ text = " HIGH SCORES!!\n\n"
+ half = len(self.table)//2
+ for i in range(half):
+ text += (
+ f"{self.table[i][1]:<4s} {self.table[i][0]:08} "
+ f"{self.table[i+half][1]:<4s} "
+ f"{self.table[i+half][0]:08}\n")
+ return text
diff --git a/src/sliceitoff/screens/__init__.py b/src/sliceitoff/screens/__init__.py
index 1d373f6..d448890 100644
--- a/src/sliceitoff/screens/__init__.py
+++ b/src/sliceitoff/screens/__init__.py
@@ -2,4 +2,5 @@ from .welcome import welcome_screen
from .levelup import levelup_screen
from .gameover import gameover_screen
from .level import level_screen
-from .hiscore import hiscore_screen
+from .initials import initials_screen
+from .hiscores import hiscores_screen
diff --git a/src/sliceitoff/screens/hiscores.py b/src/sliceitoff/screens/hiscores.py
new file mode 100644
index 0000000..98bfd46
--- /dev/null
+++ b/src/sliceitoff/screens/hiscores.py
@@ -0,0 +1,9 @@
+from text import TextPage
+
+def hiscores_screen(score_text):
+ return TextPage(
+ score_text,
+ font = 'lcd',
+ size = (8_000, 16_000),
+ grid = (9_000, 14_000),
+ pos = (8_000, 0_000) )
diff --git a/src/sliceitoff/screens/hiscore.py b/src/sliceitoff/screens/initials.py
index c6f2a21..aadb141 100644
--- a/src/sliceitoff/screens/hiscore.py
+++ b/src/sliceitoff/screens/initials.py
@@ -1,6 +1,6 @@
from text import TextPage
-def hiscore_screen(name):
+def initials_screen(name):
return TextPage(
f" New High Score!\n"
f"\n"