diff --git a/src/config.py b/src/config.py new file mode 100644 index 00000000..ba4bf877 --- /dev/null +++ b/src/config.py @@ -0,0 +1,6 @@ +import os +from dotenv import find_dotenv, load_dotenv + +# you can set TILE_SIZE in the .env file to adjust the window size +load_dotenv(find_dotenv()) +TILE_SIZE = int(os.getenv("TILE_SIZE")) diff --git a/src/main.py b/src/main.py index 07a82ec6..60c70d6f 100644 --- a/src/main.py +++ b/src/main.py @@ -2,16 +2,17 @@ import sys import pygame from field import Field import os -from dotenv import find_dotenv, load_dotenv +from config import TILE_SIZE if __name__ == "__main__": pygame.init() WHITE = (255, 255, 255) - # loading TILE_SIZE to adjust window size - load_dotenv(find_dotenv()) - WINDOW_SIZE = int(os.getenv("TILE_SIZE")) * 16 + + WINDOW_SIZE = TILE_SIZE * 16 screen = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE)) + field = Field() + running = True while running: for event in pygame.event.get(): diff --git a/src/tile.py b/src/tile.py index 3de958a8..cfbe014a 100644 --- a/src/tile.py +++ b/src/tile.py @@ -1,11 +1,12 @@ import os import pygame -from dotenv import find_dotenv, load_dotenv from kb import tractor_kb import pytholog as pl import random +from config import TILE_SIZE class Tile(pygame.sprite.Sprite): + def __init__(self, id, field, type): super().__init__() self.id = id @@ -20,9 +21,6 @@ class Tile(pygame.sprite.Sprite): self.faza = 'posadzono' - # you can set TILE_SIZE in the .env file to adjust the window size - load_dotenv(find_dotenv()) - TILE_SIZE = int(os.getenv("TILE_SIZE")) self.rect = self.image.get_rect() self.rect.topleft = (x * TILE_SIZE, y * TILE_SIZE) @@ -34,10 +32,6 @@ class Tile(pygame.sprite.Sprite): # if self.type == 'grass': # self.image = pygame.image.load("images/grass.png").convert() self.image = pygame.image.load("images/grass.png").convert() - - # you can set TILE_SIZE in the .env file to adjust the window size - load_dotenv(find_dotenv()) - TILE_SIZE = int(os.getenv("TILE_SIZE")) self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE)) diff --git a/src/tractor.py b/src/tractor.py index 9211d1de..548db369 100644 --- a/src/tractor.py +++ b/src/tractor.py @@ -1,17 +1,14 @@ import pygame import os -from dotenv import find_dotenv, load_dotenv from kb import ile_podlac from tile import Tile +from config import TILE_SIZE class Tractor(pygame.sprite.Sprite): def __init__(self, field): super().__init__ self.field = field self.image = pygame.image.load('images/tractor.png').convert_alpha() - # you can set TILE_SIZE in the .env to adjust window size - load_dotenv(find_dotenv()) - TILE_SIZE = int(os.getenv("TILE_SIZE")) self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE)) self.rect = self.image.get_rect() x, y = 0, 0 @@ -22,11 +19,8 @@ class Tractor(pygame.sprite.Sprite): def draw(self, surface): surface.blit(self.image, self.rect) - def move(self, direction): - # you can set TILE_SIZE in the .env to adjust window size - load_dotenv(find_dotenv()) - TILE_SIZE = int(os.getenv("TILE_SIZE")) + def move(self, direction): if direction == "up" and self.rect.y > 0: self.rect.y -= TILE_SIZE self.print_tile_type() @@ -59,19 +53,15 @@ class Tractor(pygame.sprite.Sprite): if keys[pygame.K_DOWN]: self.move('down') - def print_tile_type(self): - load_dotenv(find_dotenv()) - TILE_SIZE = int(os.getenv("TILE_SIZE")) + def print_tile_type(self): x = self.rect.x // TILE_SIZE y = self.rect.y // TILE_SIZE tile_type = self.field.tiles.sprites()[y * 16 + x].type print(f"The tractor is on a {tile_type} tile.") - def get_current_tile(self): - load_dotenv(find_dotenv()) - TILE_SIZE = int(os.getenv("TILE_SIZE")) + def get_current_tile(self): x = self.rect.x // TILE_SIZE y = self.rect.y // TILE_SIZE current_tile = self.field.tiles.sprites()[y * 16 + x]