Implemented screen initialization

This commit is contained in:
Marcin Kostrzewski 2020-04-02 16:49:03 +02:00
parent 5e33e55396
commit 14fac73279
3 changed files with 55 additions and 4 deletions

View File

@ -1,6 +1,7 @@
{ {
"window": { "window": {
"width": 1280, "width": 1280,
"height": 720 "height": 720,
"name": "Adventure Survival"
} }
} }

View File

@ -2,9 +2,12 @@ import pygame
import json import json
from pathlib import Path from pathlib import Path
from game.Screen import Screen
class Game: class Game:
def __init__(self): def __init__(self):
self.running = True
print("Loading configuration...", end=" ") print("Loading configuration...", end=" ")
try: try:
@ -24,5 +27,21 @@ class Game:
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ") 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() game = Game()

View File

@ -1,4 +1,35 @@
import math
import pygame
# minimum UI width
MARGIN = 300
class Screen: class Screen:
def __init__(self): def __init__(self, gameObject, windowConfig):
self.map self.gameObject = gameObject
self.ui 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