Added more comments

This commit is contained in:
Marcin Kostrzewski 2020-05-09 22:03:36 +02:00
parent d537eb089d
commit fc47c697df

View File

@ -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
self.screen = Screen(self, self.config["window"])
print("OK")
# Load map data from file
try:
mapFile = Path(str(filesPath) + "/data/mapdata/")
except IOError:
print("Could not load map data. Exiting...")
exit(1)
# 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()