summaryrefslogtreecommitdiff
path: root/src/sliceitoff/game
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-30 13:58:02 +0200
committerViljami Ilola <+@hix.fi>2024-03-30 13:58:02 +0200
commit5874005e58d0f26b4393845b3b18160658ece160 (patch)
tree428f315dbd99e3f77677107586cac493d000cac7 /src/sliceitoff/game
parentacfac65f17a274d6d7f73cb44ed9476032241134 (diff)
refactor game.level
Diffstat (limited to 'src/sliceitoff/game')
-rw-r--r--src/sliceitoff/game/__init__.py1
-rw-r--r--src/sliceitoff/game/game.py25
-rw-r--r--src/sliceitoff/game/level.py38
-rw-r--r--src/sliceitoff/game/show.py3
4 files changed, 31 insertions, 36 deletions
diff --git a/src/sliceitoff/game/__init__.py b/src/sliceitoff/game/__init__.py
index a2771db..7785623 100644
--- a/src/sliceitoff/game/__init__.py
+++ b/src/sliceitoff/game/__init__.py
@@ -1 +1,2 @@
+""" game - All the game logic and user inputs """
from .game import Game
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py
index bcb04e7..b7d5212 100644
--- a/src/sliceitoff/game/game.py
+++ b/src/sliceitoff/game/game.py
@@ -17,6 +17,7 @@ from .show import Show
from .initials import Initials
class Game:
+ """ This is the whole game. """
def __init__(self):
pygame.init()
self.clock = pygame.time.Clock()
@@ -26,46 +27,46 @@ class Game:
Fonts.load_fonts( Path(__file__).parent.parent.resolve() )
pygame.mouse.set_visible(False)
+ def __del__(self):
+ pygame.quit()
+
def welcome(self):
+ """ displays instruction and waits a key """
ws = Show(welcome_screen())
- dt = 0
while ws.active:
ws.update(dt = self.clock.tick())
self.display.update( [ws] )
def show_highscores(self):
+ """ displays highscores and waits a key """
his = Show(hiscores_screen(str(self.hiscores)))
- dt = 0
while his.active:
his.update(dt = self.clock.tick())
self.display.update( [his] )
def newgame(self):
+ """ new game, new score, runs through levels till game over """
self.stats = Stats()
while self.stats.lives:
- level = Level(display = self.display, stats = self.stats)
- dt = 0
- while level.step(dt):
- dt = self.clock.tick()
+ level = Level(stats = self.stats)
+ while level.active:
+ level.update(dt = self.clock.tick())
+ self.display.update( [level] )
if self.stats.lives:
self.stats.level_up()
def initials(self):
+ """ asks for initials in case of high enough score """
initials = Initials()
- dt = 0
while initials.active:
initials.update(dt = self.clock.tick())
self.display.update([initials])
return initials.name
def run(self):
+ """ This is the main loop of the game (not loop at the moment) """
self.welcome()
self.newgame()
if self.hiscores.high_enough(self.stats.score):
self.hiscores.add( self.stats.score, self.initials())
self.show_highscores()
-
-
- def __del__(self):
- pygame.quit()
-
diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py
index 0742213..085cbc9 100644
--- a/src/sliceitoff/game/level.py
+++ b/src/sliceitoff/game/level.py
@@ -1,28 +1,26 @@
+""" game.level - This is what runs invidual levels """
import pygame
-from display import Display
from status import Status
from player import Player
from field import Field
from enemies import Enemies
from screens import levelup_screen, gameover_screen, level_screen
from .gameplay import Gameplay
-from .anykey import anykey
from .show import Show
-
-class Level:
+class Level(pygame.sprite.Group):
""" One level that can be played """
- def __init__(self, display = None, stats = None):
- self.display = display
+ def __init__(self, stats = None):
+ super().__init__()
self.stats = stats
self.status = Status(stats = self.stats)
self.field = Field(stats = self.stats)
self.enemies = Enemies(count = self.stats.enemies)
self.player = Player()
self.level_info = Show(level_screen(stats.level))
-
self.ended = False
+ self.active = True
self.endscreen = None
self.gameplay = Gameplay(
@@ -30,28 +28,27 @@ class Level:
field = self.field,
enemies = self.enemies,
stats = self.stats)
- self.obj_classes = [
- self.status,
- self.field,
- self.enemies,
- self.level_info,
- self.player]
-
- def step(self, dt):
+
+ def update(self, dt = 0):
+ """ Updates groups, calls gameplay and adds sprites for drawing """
+ self.empty()
self.status.update(dt = dt)
self.field.update(dt = dt)
- self.enemies.update(dt = dt, field_rects = self.field.active_rects() )
+ self.enemies.update(dt = dt, field_rects = self.field.active_rects())
+ self.add(self.status, self.field, self.enemies)
if self.level_info.active:
self.level_info.update(dt = dt)
+ self.add(self.level_info)
elif self.ended:
if self.endscreen.active:
self.endscreen.update(dt = dt)
+ self.add(self.endscreen)
else:
- return False
-
+ self.active = False
else:
+ self.add(self.player)
self.stats.update_bonus(dt)
if self.gameplay.step():
self.ended = True
@@ -59,8 +56,3 @@ class Level:
self.endscreen = Show(levelup_screen(self.stats))
else:
self.endscreen = Show(gameover_screen())
- self.obj_classes.append(self.endscreen)
-
- self.display.update(self.obj_classes)
-
- return True
diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py
index 94e2540..0fb6e05 100644
--- a/src/sliceitoff/game/show.py
+++ b/src/sliceitoff/game/show.py
@@ -14,7 +14,6 @@ class Show(pygame.sprite.Group):
def update(self, dt = 0, **kwargs):
""" First timeout then fadeout and then inactivity """
- super().update(dt = dt, explode = self.explode, **kwargs)
if self.fadeout <= 0:
self.active = False
elif self.timeout <= 0:
@@ -26,6 +25,8 @@ class Show(pygame.sprite.Group):
if anykey():
self.timeout = 0
self.timeout -= dt
+ for sprite in self.sprites():
+ sprite.update(dt = dt, explode = self.explode, **kwargs)
def sprites(self):
""" Return sprites only when active """