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 from src.game.Timer import Timer
# Main Game class
class Game: class Game:
def __init__(self, filesPath): def __init__(self, filesPath):
self.running = True self.running = True
print("Loading configuration...", end=" ") print("Loading configuration...", end=" ")
# Load config params from file
try: try:
configFolder = Path(str(filesPath) + "/data/config/") configFolder = Path(str(filesPath) + "/data/config/")
configFile = configFolder / "mainConfig.json" configFile = configFolder / "mainConfig.json"
@ -40,8 +42,14 @@ class Game:
print("The screen cannot be in a vertical orientation. Exiting...") print("The screen cannot be in a vertical orientation. Exiting...")
exit(1) exit(1)
# Initialize screen
self.screen = Screen(self, self.config["window"])
print("OK")
# Initialize timers # Initialize timers
# PyGame timer - precise timer, counts milliseconds every frame
self.pgTimer = pygame.time.Clock() self.pgTimer = pygame.time.Clock()
# Virtual timer to track in-game time
self.ingameTimer = Timer() self.ingameTimer = Timer()
self.ingameTimer.startClock() self.ingameTimer.startClock()
@ -51,42 +59,55 @@ class Game:
self.moveTimer = 0 self.moveTimer = 0
self.moveTime = 100 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"]) # Initialize map object
print("OK")
mapFile = Path(str(filesPath) + "/data/mapdata/")
self.map = Map(path.join(mapFile, 'map.txt'), self.screen) self.map = Map(path.join(mapFile, 'map.txt'), self.screen)
# Initialize the player
self.player = Player((6, 2), self.map.tileSize) self.player = Player((6, 2), self.map.tileSize)
self.map.addEntity(self.player, DONTADD=True) self.map.addEntity(self.player, DONTADD=True)
self.eventManager = EventManager(self, self.player) self.eventManager = EventManager(self, self.player)
# A* algorithm
self.movement = AutomaticMovement(self.player, self.map, self.screen.getUiWidth(Locations.LEFT_UI)) self.movement = AutomaticMovement(self.player, self.map, self.screen.getUiWidth(Locations.LEFT_UI))
testTarget = self.map.entities[0] testTarget = self.map.entities[0]
if testTarget is self.player: if testTarget is self.player:
testTarget = self.map.entities[1] testTarget = self.map.entities[1]
self.movement.gotoToTarget(testTarget) self.movement.gotoToTarget(testTarget)
# Start game loop
self.mainLoop() self.mainLoop()
def mainLoop(self): def mainLoop(self):
while self.running: while self.running:
# Update ingame clock # Tick the timers
self.ingameTimer.updateTime(self.pgTimer.tick()) self.ingameTimer.updateTime(self.pgTimer.tick())
self.deltaTime = self.ingameTimer.timePassed - self.lastTimePassed self.deltaTime = self.ingameTimer.timePassed - self.lastTimePassed
self.lastTimePassed = self.ingameTimer.timePassed self.lastTimePassed = self.ingameTimer.timePassed
self.spritesList.update() # Handle all events
self.eventManager.handleEvents() self.eventManager.handleEvents()
# Call update() method for each entity
self.spritesList.update()
if self.moveTimer > 0: if self.moveTimer > 0:
self.moveTimer -= self.deltaTime self.moveTimer -= self.deltaTime
else: else:
self.movement.updatePlayerCoords() self.movement.updatePlayerCoords()
self.moveTimer = self.moveTime self.moveTimer = self.moveTime
# Draw all sprites
self.spritesList.draw(self.screen.pygameScreen) self.spritesList.draw(self.screen.pygameScreen)
# Flip the display
pygame.display.flip() pygame.display.flip()