summaryrefslogtreecommitdiff
path: root/src/sliceitoff/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/sliceitoff/game')
-rw-r--r--src/sliceitoff/game/__init__.py1
-rw-r--r--src/sliceitoff/game/explodeout.py34
-rw-r--r--src/sliceitoff/game/initials.py25
-rw-r--r--src/sliceitoff/game/show.py29
4 files changed, 50 insertions, 39 deletions
diff --git a/src/sliceitoff/game/__init__.py b/src/sliceitoff/game/__init__.py
index 7785623..221acd3 100644
--- a/src/sliceitoff/game/__init__.py
+++ b/src/sliceitoff/game/__init__.py
@@ -1,2 +1,3 @@
""" game - All the game logic and user inputs """
from .game import Game
+from .explodeout import ExplodeOutGroup
diff --git a/src/sliceitoff/game/explodeout.py b/src/sliceitoff/game/explodeout.py
new file mode 100644
index 0000000..34bd9fe
--- /dev/null
+++ b/src/sliceitoff/game/explodeout.py
@@ -0,0 +1,34 @@
+""" game.explodeout - For showing explogind effect and waiting for a key """
+import pygame
+
+from .anykey import anykey
+
+class ExplodeOutGroup(pygame.sprite.Group):
+ """ Sprite group that just counts down feadeout/explosion or a key """
+ def __init__(self, active = True):
+ super().__init__()
+ self.explode = False
+ self.active = active
+ self.fadeout = 1_000
+
+ def update(self, dt = 0, **kwargs):
+ """ Just does the explosion and marks group inactive """
+ if not self.active:
+ return False
+
+ super().update(dt = dt, explode = self.explode, **kwargs)
+
+ if self.explode:
+ if self.fadeout <= 0:
+ self.active = False
+ else:
+ if anykey():
+ self.fadeout = 0
+ self.active = False
+ self.fadeout -= dt
+ return True
+ return True
+
+ def do_fadeout(self):
+ """ Just kicks off exploding phase """
+ self.explode = True
diff --git a/src/sliceitoff/game/initials.py b/src/sliceitoff/game/initials.py
index 7811afa..9fb5a68 100644
--- a/src/sliceitoff/game/initials.py
+++ b/src/sliceitoff/game/initials.py
@@ -3,45 +3,30 @@ import pygame
from screens import initials_screen
-from .anykey import anykey
+from .explodeout import ExplodeOutGroup
-class Initials(pygame.sprite.Group):
+class Initials(ExplodeOutGroup):
""" Sprite group that asks initials to self.name from user """
def __init__(self):
super().__init__()
self.add(initials_screen(""))
self.name = ""
- self.explode = False
- self.active = True
- self.fadeout = 1_000
def update(self, dt = 0, **kwargs):
""" Does it all. Reads keyboard and updates screen """
- if not self.active:
- return
-
- super().update(dt = dt, explode = self.explode, **kwargs)
-
- if self.explode:
- if self.fadeout <= 0:
- self.active = False
- else:
- if anykey():
- self.fadeout = 0
- self.active = False
- self.fadeout -= dt
+ if not super().update(dt = dt, **kwargs):
return
for event in pygame.event.get():
if event.type == pygame.QUIT:
- self.explode = True
+ self.do_fadeout()
break
if event.type == pygame.KEYDOWN:
if event.key in (
pygame.K_ESCAPE,
pygame.K_KP_ENTER,
pygame.K_RETURN):
- self.explode = True
+ self.do_fadeout()
break
if event.key in (
pygame.K_RSHIFT,
diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py
index 0fb6e05..eaf3658 100644
--- a/src/sliceitoff/game/show.py
+++ b/src/sliceitoff/game/show.py
@@ -1,32 +1,23 @@
""" Sprite group that show sprites and skips if key is pressed """
-import pygame
from .anykey import anykey
+from .explodeout import ExplodeOutGroup
-class Show(pygame.sprite.Group):
+class Show(ExplodeOutGroup):
""" To show some sprites and quit on any key """
def __init__(self, sprites = None, active = True):
- super().__init__()
+ super().__init__(active = active)
self.add(sprites)
- self.active = active
- self.explode = False
- self.fadeout = 1_000
self.timeout = 15_000
def update(self, dt = 0, **kwargs):
""" First timeout then fadeout and then inactivity """
- if self.fadeout <= 0:
- self.active = False
- elif self.timeout <= 0:
- if anykey():
- self.fadeout = 0
- self.fadeout -= dt
- self.explode = True
- else:
- if anykey():
- self.timeout = 0
- self.timeout -= dt
- for sprite in self.sprites():
- sprite.update(dt = dt, explode = self.explode, **kwargs)
+ if not super().update(dt = dt, **kwargs):
+ return
+ if anykey():
+ self.do_fadeout()
+ if self.timeout <= 0:
+ self.do_fadeout()
+ self.timeout -= dt
def sprites(self):
""" Return sprites only when active """