summaryrefslogtreecommitdiff
path: root/src/sliceitoff/game/game.py
blob: 57f2c566bd589bb69aa39237fcf6f80554a783e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
""" game.game - Slice It Off! - Game where you slice the area where smily
    faces reside to the minimum.
"""

from pathlib import Path

import pygame

from sliceitoff.settings import settings
from sliceitoff.display import Display
from sliceitoff.stats import Stats
from sliceitoff.screens import (
    hiscores_screen,
    instructions1_screen,
    instructions2_screen)
from sliceitoff.hiscores import hi_scores
from sliceitoff.text import fonts
from sliceitoff.sfx import sfx

from .mainmenu import Mainmenu, MenuItems
from .level import Level
from .show import Show
from .initials import Initials
from .settings import SettingsMenu

class Game:
    """ This is the whole game. """
    def __init__(self):
        assets_path = Path(__file__).parent.parent.resolve().joinpath('assets')
        pygame.init()
        sfx.init(assets_path)
        fonts.init(assets_path)
        self.clock = pygame.time.Clock()
        self.display = Display()
        self.stats = None
        pygame.mouse.set_visible(False)

    def __del__(self):
        pygame.quit()

    def instructions(self):
        """ displays instruction and waits a key """
        for page in [instructions1_screen, instructions2_screen]:
            screen = Show(page())
            while screen.active:
                screen.update(dt = self.clock.tick())
                self.display.update( [screen] )

    def show_highscores(self):
        """ displays highscores and waits a key """
        sfx.music("pimpelipompeli")
        his = Show(hiscores_screen(str(hi_scores)))
        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(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()
        while initials.active:
            initials.update(dt = self.clock.tick())
            self.display.update([initials])
        return initials.name

    def mainmenu(self):
        """ menu where one select what to do """
        sfx.music("pimpelipompeli")
        menu = Mainmenu()
        while menu.active:
            menu.update(dt = self.clock.tick())
            self.display.update([menu])
        return menu.selection

    def settings(self):
        """ settings menu where one can adjust volume for example """
        menu = SettingsMenu()
        while menu.active:
            menu.update(dt = self.clock.tick())
            self.display.update([menu])

    def run(self):
        """ This is the main loop of the game """
        while True:
            match self.mainmenu():
                case MenuItems.QUIT:
                    hi_scores.save()
                    settings.save()
                    break
                case MenuItems.HISCORES:
                    self.show_highscores()
                case MenuItems.INSTRUCT:
                    self.instructions()
                case MenuItems.SETTINGS:
                    self.settings()
                case MenuItems.NEWGAME:
                    self.newgame()
                    if hi_scores.high_enough(self.stats.score):
                        hi_scores.add( self.stats.score, self.initials())
                    self.show_highscores()