Added GA gamemode

This commit is contained in:
Marcin Kostrzewski 2020-05-16 15:39:53 +02:00
parent 66ac27a6fe
commit de3a0569a9

View File

@ -5,6 +5,7 @@ from pathlib import Path
import pygame import pygame
from src.AI.Affinities import Affinities from src.AI.Affinities import Affinities
from src.AI.GA import geneticAlgorithm
from src.entities.Player import Player from src.entities.Player import Player
from src.game.EventManager import EventManager from src.game.EventManager import EventManager
from src.game.Map import Map from src.game.Map import Map
@ -14,7 +15,7 @@ from src.game.Timer import Timer
# Main Game class # Main Game class
class Game: class Game:
def __init__(self, filesPath, gamemode="test"): def __init__(self, filesPath, gamemode="ga"):
""" """
Game script initialization. Loads all files, creates screen, map, tiles, entities and a player. Game script initialization. Loads all files, creates screen, map, tiles, entities and a player.
Starts the main game loop at the end. Starts the main game loop at the end.
@ -49,9 +50,11 @@ class Game:
# Runnable selection # Runnable selection
if gamemode == "test": if gamemode == "test":
self.testRun(filesPath) self.testRun(filesPath)
elif gamemode == "ga":
# Start game loop self.gaRun(filesPath)
self.mainLoop() else:
print("Invalid gamemode. \n Possible options: test, ga")
exit(1)
def initializePygame(self): def initializePygame(self):
""" """
@ -130,6 +133,37 @@ class Game:
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)
# Start game loop
self.mainLoop()
def gaRun(self, filesPath):
"""
Runs the game in GA mode - runs genetic algorithm in headless mode.
:param filesPath: Absolute path to game's root directory
"""
self.running = True
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
# Vertical rotation is unsupported due to UI layout
if self.config["window"]["height"] > self.config["window"]["width"]:
print("The screen cannot be in a vertical orientation. Exiting...")
exit(1)
# Initialize timers
# Virtual timer to track in-game time
self.ingameTimer = Timer()
self.ingameTimer.startClock()
# Initialize screen
self.screen = Screen(self, self.config["window"])
print("OK")
self.initializeMap(filesPath)
# Run GA:
geneticAlgorithm(self.map, 200, 8, 0.05)
def mainLoop(self): def mainLoop(self):
""" """
Continuously running loop. Calls events, updates and draws everything. Continuously running loop. Calls events, updates and draws everything.