DSZI_Survival/src/game/Game.py

93 lines
2.7 KiB
Python
Raw Normal View History

2020-04-02 15:50:47 +02:00
import json
from os import path
2020-05-10 14:25:30 +02:00
from pathlib import Path
import pygame
2020-04-02 15:50:47 +02:00
2020-05-10 14:25:30 +02:00
from src.entities.Player import Player
2020-04-06 09:54:51 +02:00
from src.game.EventManager import EventManager
from src.game.Map import Map
2020-05-10 14:25:30 +02:00
from src.game.Screen import Screen, Locations
2020-04-05 23:51:22 +02:00
from src.game.Timer import Timer
2020-04-02 16:49:03 +02:00
2020-04-02 15:50:47 +02:00
2020-05-09 22:03:36 +02:00
# Main Game class
2020-03-31 21:47:09 +02:00
class Game:
def __init__(self, filesPath):
2020-04-02 16:49:03 +02:00
self.running = True
2020-04-02 15:50:47 +02:00
print("Loading configuration...", end=" ")
2020-05-09 22:03:36 +02:00
# Load config params from file
2020-04-02 15:50:47 +02:00
try:
configFolder = Path(str(filesPath) + "/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()
self.spritesList = pygame.sprite.Group()
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)
2020-04-05 23:51:22 +02:00
# Initialize timers
2020-05-09 22:03:36 +02:00
# PyGame timer - precise timer, counts milliseconds every frame
2020-04-05 23:51:22 +02:00
self.pgTimer = pygame.time.Clock()
2020-05-09 22:03:36 +02:00
# Virtual timer to track in-game time
2020-04-05 23:51:22 +02:00
self.ingameTimer = Timer()
self.ingameTimer.startClock()
2020-05-09 22:30:21 +02:00
# Initialize screen
self.screen = Screen(self, self.config["window"])
print("OK")
2020-04-26 14:33:04 +02:00
self.moveTimer = 0
2020-04-26 15:20:17 +02:00
self.moveTime = 100
2020-04-26 14:33:04 +02:00
2020-05-09 22:03:36 +02:00
# Load map data from file
2020-05-09 22:30:21 +02:00
mapFile = None
2020-05-09 22:03:36 +02:00
try:
mapFile = Path(str(filesPath) + "/data/mapdata/")
except IOError:
print("Could not load map data. Exiting...")
exit(1)
2020-04-26 14:33:04 +02:00
2020-05-09 22:03:36 +02:00
# Initialize map object
self.map = Map(path.join(mapFile, 'map.txt'), self.screen)
2020-05-09 22:03:36 +02:00
# Initialize the player
2020-04-05 21:53:10 +02:00
self.player = Player((6, 2), self.map.tileSize)
2020-04-26 03:59:09 +02:00
self.map.addEntity(self.player, DONTADD=True)
self.eventManager = EventManager(self, self.player)
2020-05-09 22:03:36 +02:00
# Start game loop
2020-04-02 16:49:03 +02:00
self.mainLoop()
def mainLoop(self):
2020-04-02 17:30:25 +02:00
while self.running:
2020-05-09 22:03:36 +02:00
# Tick the timers
2020-04-05 23:51:22 +02:00
self.ingameTimer.updateTime(self.pgTimer.tick())
2020-04-26 14:33:04 +02:00
2020-05-09 22:03:36 +02:00
# Handle all events
2020-04-02 17:30:25 +02:00
self.eventManager.handleEvents()
2020-04-26 14:33:04 +02:00
2020-05-09 22:03:36 +02:00
# Call update() method for each entity
self.spritesList.update()
# Draw all sprites
2020-04-02 17:30:25 +02:00
self.spritesList.draw(self.screen.pygameScreen)
2020-05-09 22:03:36 +02:00
# Flip the display
2020-04-02 17:30:25 +02:00
pygame.display.flip()