2020-04-02 15:50:47 +02:00
|
|
|
import pygame
|
|
|
|
import json
|
|
|
|
from pathlib import Path
|
2020-04-03 19:36:13 +02:00
|
|
|
from os import path
|
2020-04-02 15:50:47 +02:00
|
|
|
|
2020-04-02 17:30:25 +02:00
|
|
|
from game.EventManager import EventManager
|
2020-04-02 16:49:03 +02:00
|
|
|
from game.Screen import Screen
|
2020-04-03 19:36:13 +02:00
|
|
|
from game.Map import Map
|
2020-04-05 14:53:54 +02:00
|
|
|
from src.entities.Player import Player
|
2020-04-02 16:49:03 +02:00
|
|
|
|
2020-04-02 15:50:47 +02:00
|
|
|
|
2020-03-31 21:47:09 +02:00
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
2020-04-02 16:49:03 +02:00
|
|
|
self.running = True
|
2020-04-02 15:50:47 +02:00
|
|
|
print("Loading configuration...", end=" ")
|
|
|
|
|
|
|
|
try:
|
2020-04-02 16:55:19 +02:00
|
|
|
configFolder = Path("../data/config/")
|
2020-04-02 15:50:47 +02:00
|
|
|
configFile = configFolder / "mainConfig.json"
|
|
|
|
|
|
|
|
self.config = json.loads(configFile.read_text())
|
|
|
|
|
|
|
|
print("OK")
|
|
|
|
except IOError:
|
|
|
|
print("Error reading configuration file. Exiting...")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print("Initializing pygame...", end=" ")
|
|
|
|
pygame.init()
|
2020-04-03 19:36:13 +02:00
|
|
|
self.spritesList = pygame.sprite.Group()
|
2020-04-05 19:08:08 +02:00
|
|
|
|
2020-04-02 15:50:47 +02:00
|
|
|
print("OK")
|
|
|
|
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
|
|
|
|
|
2020-04-02 16:49:03 +02:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
self.screen = Screen(self, self.config["window"])
|
|
|
|
print("OK")
|
|
|
|
|
2020-04-03 19:36:13 +02:00
|
|
|
self.mapDataFolder = path.dirname("../data/mapdata/")
|
2020-04-03 20:23:36 +02:00
|
|
|
self.map = Map(path.join(self.mapDataFolder, 'map.txt'), self.screen)
|
2020-04-05 14:53:54 +02:00
|
|
|
self.player = Player((0, 0), self.map.tileSize)
|
|
|
|
self.map.addEntity(self.player)
|
2020-04-05 15:41:42 +02:00
|
|
|
self.eventManager = EventManager(self, self.player)
|
|
|
|
|
2020-04-02 16:49:03 +02:00
|
|
|
self.mainLoop()
|
|
|
|
|
|
|
|
def mainLoop(self):
|
2020-04-02 17:30:25 +02:00
|
|
|
while self.running:
|
|
|
|
self.eventManager.handleEvents()
|
|
|
|
self.spritesList.draw(self.screen.pygameScreen)
|
|
|
|
pygame.display.flip()
|