From 14fac73279ab7cb1e7d94a3173a8fecff3148c01 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Thu, 2 Apr 2020 16:49:03 +0200 Subject: [PATCH] Implemented screen initialization --- data/config/mainConfig.json | 3 ++- src/game/Game.py | 19 +++++++++++++++++++ src/game/Screen.py | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/data/config/mainConfig.json b/data/config/mainConfig.json index 8fa6c30..48438f5 100644 --- a/data/config/mainConfig.json +++ b/data/config/mainConfig.json @@ -1,6 +1,7 @@ { "window": { "width": 1280, - "height": 720 + "height": 720, + "name": "Adventure Survival" } } \ No newline at end of file diff --git a/src/game/Game.py b/src/game/Game.py index dbd964b..171f77f 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -2,9 +2,12 @@ import pygame import json from pathlib import Path +from game.Screen import Screen + class Game: def __init__(self): + self.running = True print("Loading configuration...", end=" ") try: @@ -24,5 +27,21 @@ class Game: 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) + + self.screen = Screen(self, self.config["window"]) + print("OK") + + self.spritesList = pygame.sprite.Group() + + self.mainLoop() + + def mainLoop(self): + self.spritesList.draw(self.screen.pygameScreen) + pygame.display.flip() + game = Game() diff --git a/src/game/Screen.py b/src/game/Screen.py index a749f7e..7bed8ff 100644 --- a/src/game/Screen.py +++ b/src/game/Screen.py @@ -1,4 +1,35 @@ +import math +import pygame + +# minimum UI width +MARGIN = 300 + + class Screen: - def __init__(self): - self.map - self.ui + def __init__(self, gameObject, windowConfig): + self.gameObject = gameObject + self.winX = windowConfig["width"] + self.winY = windowConfig["height"] + pygame.display.set_caption(windowConfig["name"]) + self.pygameScreen = pygame.display.set_mode((self.winX, self.winY)) + + # map is a square inside the screen + self.mapSize = self.calculateMapDimensions() + + # mapCoord is a top leftmost pixel + self.mapCoord = math.floor((self.winX - self.mapSize) / 2) + + # draw a white rect to resemble map + pygame.draw.rect(self.pygameScreen, (255, 255, 255), [self.mapCoord, 0, self.mapSize, self.mapSize]) + + def calculateMapDimensions(self): + result = 0 + expectedSize = self.winY + + # when there's not enough space for the UI on the sides + if self.winX - expectedSize < MARGIN: + result = expectedSize - (MARGIN - (self.winX - expectedSize)) + else: + result = expectedSize + + return result