-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_manager.py
More file actions
64 lines (49 loc) · 1.8 KB
/
game_manager.py
File metadata and controls
64 lines (49 loc) · 1.8 KB
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
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from scene import Scene
import pygame
import sys
from pygame.locals import DOUBLEBUF, HWSURFACE, QUIT, K_F3, KEYDOWN
from enum import Enum
from constants import SCR_DIM, FPS
from game import Game
class GameManager:
def __init__(self) -> None:
pygame.init()
self.screen = pygame.display.set_mode(SCR_DIM, DOUBLEBUF | HWSURFACE)
self.clock = pygame.time.Clock()
self.dt = self.clock.tick_busy_loop(FPS) / 1000
self.debug = False
self.scene = Game(self)
self.scene.setup()
def run(self):
while self.scene.running:
self.update()
self.scene.draw()
if self.debug:
self.scene.debug()
pygame.display.flip()
self.kill()
def update(self):
self.dt = self.clock.tick_busy_loop(FPS) / 1000 # dt calculated for pixels/second
pygame.display.set_caption(f"Pygame Window | {self.clock.get_fps():.0f}")
self.events = {event.type: event for event in pygame.event.get()}
if QUIT in self.events:
self.kill()
self.scene.update()
def kill(self) -> None:
pygame.quit()
sys.exit()
class Scenes(Enum):
GAME = Game
# "self.game.new_scene(self.game.Scenes.GAME)" anywhere in any sprite code to start a new scene
def new_scene(self, scene_class: Scene, **kwargs) -> None:
self.scene.kill()
self.scene = scene_class.value(self)
self.scene.setup(**kwargs)
# Switch to an already existing scene object (ex. from pause screen back to main game where game data is saved)
def switch_scene(self, scene: Scene) -> None:
self.scene.kill()
self.scene = scene
self.scene.start()