refactor(TILE_SIZE): load environment variables in config.py file

This commit is contained in:
Wojciech Kubicki 2024-03-25 12:30:06 +01:00
parent 7c30177bf1
commit c99e738034
4 changed files with 17 additions and 26 deletions

6
src/config.py Normal file
View File

@ -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"))

View File

@ -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():

View File

@ -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))

View File

@ -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]