feat: create config file and config manager

This commit is contained in:
2021-06-02 22:51:25 -04:00
parent e5ea8988c8
commit 0f1cc442b6
5 changed files with 63 additions and 22 deletions

15
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}

10
config.yaml Normal file
View File

@@ -0,0 +1,10 @@
# window properties
window:
width: 800
height: 600
icon: "img/tetris_icon.png"
title: "Tetris"
background-color: "#6495ED" # cornflower blue
engine:
fps: 60

View File

@@ -4,7 +4,10 @@ import pygame
For information on the Tetris piece Tetromino go here: For information on the Tetris piece Tetromino go here:
https://tetris.fandom.com/wiki/Tetromino https://tetris.fandom.com/wiki/Tetromino
''' '''
class tetromino: class Piece:
def __init__(self): def __init__(self):
pass
def draw(screen):
pass pass

41
main.py
View File

@@ -1,26 +1,21 @@
import pygame import pygame
from entity.tetromino import tetromino
# window properties from entity.Piece import Piece
WIN_WIDTH = 800 from util.ConfigurationManager import ConfigurationManager
WIN_HEIGHT = 600
WIN_ICON = "img/tetris_icon.png" # load configs into game
WIN_TITLE = "Tetris" ConfigurationManager.load()
WIN_BG_COLOR = (100, 149, 237) # cornflower blue, RGB
# potential color palette https://coolors.co/6495ed-ee6352-59cd90-fac05e-f79d84 # potential color palette https://coolors.co/6495ed-ee6352-59cd90-fac05e-f79d84
FIRE_OPAL = (238, 99, 82) FIRE_OPAL = (238, 99, 82) # TODO define colors in config
TILE_SIZE = 20 # TODO define tile size in config
# engine properties
FPS = 60
# other
TILE_SIZE = 20
def draw(screen): def draw(screen):
# draw window bg # draw window bg
screen.fill(WIN_BG_COLOR) bg_color = pygame.Color(ConfigurationManager.configuration["window"]["background-color"])
screen.fill(bg_color)
# TODO move piece logic into class
# draw j tetromino? # draw j tetromino?
j_points = [(0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1)] j_points = [(0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1)]
@@ -42,16 +37,22 @@ def draw(screen):
def main(): def main():
pygame.init() pygame.init()
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) win_width = ConfigurationManager.configuration["window"]["width"]
clock = pygame.time.Clock() win_height = ConfigurationManager.configuration["window"]["height"]
loaded_icon = pygame.image.load(WIN_ICON) win_icon = ConfigurationManager.configuration["window"]["icon"]
win_title = ConfigurationManager.configuration["window"]["title"]
fps = ConfigurationManager.configuration["engine"]["fps"]
pygame.display.set_caption(WIN_TITLE) screen = pygame.display.set_mode((win_width, win_height))
clock = pygame.time.Clock()
loaded_icon = pygame.image.load(win_icon)
pygame.display.set_caption(win_title)
pygame.display.set_icon(loaded_icon) pygame.display.set_icon(loaded_icon)
is_running = True is_running = True
while is_running: while is_running:
clock.tick(FPS) clock.tick(fps)
for event in pygame.event.get(): for event in pygame.event.get():

View File

@@ -0,0 +1,12 @@
import yaml
CONFIG_FILE_LOCATION = "config.yaml"
class ConfigurationManager:
configuration = []
@classmethod
def load(cls):
with open(CONFIG_FILE_LOCATION, "r") as yaml_file:
cls.configuration = yaml.safe_load(yaml_file)