From fc47c697dfde48fec64784ade5220fffdb69fa09 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sat, 9 May 2020 22:03:36 +0200 Subject: [PATCH] Added more comments --- src/game/Game.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/game/Game.py b/src/game/Game.py index e714727..b0efc04 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -12,11 +12,13 @@ from src.entities.Player import Player from src.game.Timer import Timer +# Main Game class class Game: def __init__(self, filesPath): self.running = True print("Loading configuration...", end=" ") + # Load config params from file try: configFolder = Path(str(filesPath) + "/data/config/") configFile = configFolder / "mainConfig.json" @@ -40,8 +42,14 @@ class Game: print("The screen cannot be in a vertical orientation. Exiting...") exit(1) + # Initialize screen + self.screen = Screen(self, self.config["window"]) + print("OK") + # Initialize timers + # PyGame timer - precise timer, counts milliseconds every frame self.pgTimer = pygame.time.Clock() + # Virtual timer to track in-game time self.ingameTimer = Timer() self.ingameTimer.startClock() @@ -51,42 +59,55 @@ class Game: self.moveTimer = 0 self.moveTime = 100 + # Load map data from file + try: + mapFile = Path(str(filesPath) + "/data/mapdata/") + except IOError: + print("Could not load map data. Exiting...") + exit(1) - self.screen = Screen(self, self.config["window"]) - print("OK") - - mapFile = Path(str(filesPath) + "/data/mapdata/") + # Initialize map object self.map = Map(path.join(mapFile, 'map.txt'), self.screen) + + # Initialize the player self.player = Player((6, 2), self.map.tileSize) self.map.addEntity(self.player, DONTADD=True) self.eventManager = EventManager(self, self.player) + # A* algorithm self.movement = AutomaticMovement(self.player, self.map, self.screen.getUiWidth(Locations.LEFT_UI)) - testTarget = self.map.entities[0] if testTarget is self.player: testTarget = self.map.entities[1] self.movement.gotoToTarget(testTarget) + # Start game loop self.mainLoop() def mainLoop(self): while self.running: - # Update ingame clock + # Tick the timers self.ingameTimer.updateTime(self.pgTimer.tick()) self.deltaTime = self.ingameTimer.timePassed - self.lastTimePassed self.lastTimePassed = self.ingameTimer.timePassed - self.spritesList.update() + # Handle all events self.eventManager.handleEvents() + # Call update() method for each entity + self.spritesList.update() + if self.moveTimer > 0: self.moveTimer -= self.deltaTime else: self.movement.updatePlayerCoords() self.moveTimer = self.moveTime + + # Draw all sprites self.spritesList.draw(self.screen.pygameScreen) + + # Flip the display pygame.display.flip()