From c6b61e8f6956f7e18085e42f60f70f5bd10ebdc2 Mon Sep 17 00:00:00 2001 From: Wojciech Kubicki Date: Sat, 27 Apr 2024 20:37:12 +0200 Subject: [PATCH] feat: store constants in .env for easier adjustments during presentations --- README.md | 12 +++++++++++- src/config.py | 12 +++++++++++- src/main.py | 4 ++-- src/tile.py | 4 ++-- src/tractor.py | 6 +++--- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6c0b8a43..876bdd77 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,18 @@ pip install pytholog Stwórz plik `.env` w głównym folderze projektu o poniższej treści: ``` -# adjust TILE_SIZE to fit your screen well +# window size TILE_SIZE=64 + +# game speed +TICK_RATE=10 + +# coordinates of destination tile +FINAL_X=15 +FINAL_Y=15 + +# tiles without plants modifier +FREE_TILES=2 ``` Uruchom środowisko używając komend: diff --git a/src/config.py b/src/config.py index ba4bf877..55d7c9d8 100644 --- a/src/config.py +++ b/src/config.py @@ -1,6 +1,16 @@ 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()) + +# window size TILE_SIZE = int(os.getenv("TILE_SIZE")) + +# game speed +TICK_RATE = int(os.getenv("TICK_RATE")) + +# coordinates of destination tile +FINAL_X = int(os.getenv("FINAL_X")) +FINAL_Y = int(os.getenv("FINAL_Y")) + +FREE_TILES = int(os.getenv("FREE_TILES")) diff --git a/src/main.py b/src/main.py index 66dd3ec6..17495ae0 100644 --- a/src/main.py +++ b/src/main.py @@ -2,7 +2,7 @@ import sys import pygame from field import Field import os -from config import TILE_SIZE +from config import TILE_SIZE, TICK_RATE if __name__ == "__main__": pygame.init() @@ -24,4 +24,4 @@ if __name__ == "__main__": field.draw(screen) pygame.display.flip() - pygame.time.Clock().tick(10) + pygame.time.Clock().tick(TICK_RATE) diff --git a/src/tile.py b/src/tile.py index 49753bbf..24d3c2e7 100644 --- a/src/tile.py +++ b/src/tile.py @@ -3,7 +3,7 @@ import pygame from kb import tractor_kb import pytholog as pl import random -from config import TILE_SIZE +from config import TILE_SIZE, FREE_TILES class Tile(pygame.sprite.Sprite): @@ -16,7 +16,7 @@ class Tile(pygame.sprite.Sprite): self.field = field # temporary solution to have vegetables act as obstacles - if random.randint(1, 10) % 3 == 0: + if random.randint(1, 10) % FREE_TILES == 0: vegetables = tractor_kb.query(pl.Expr("warzywo(Nazwa_warzywa)")) random_vegetable = vegetables[random.randint(0, len(vegetables)-1)]['Nazwa_warzywa'] self.set_type(random_vegetable) diff --git a/src/tractor.py b/src/tractor.py index c533f0b0..5291d943 100644 --- a/src/tractor.py +++ b/src/tractor.py @@ -2,7 +2,7 @@ import pygame import os from kb import ile_podlac, multi_sasiedzi from tile import Tile -from config import TILE_SIZE +from config import TILE_SIZE, FINAL_X, FINAL_Y from collections import deque import heapq import random @@ -19,8 +19,8 @@ class Tractor(pygame.sprite.Sprite): self.direction = 'east' # TODO: enable tractor to start on other tile than (0,0) self.start = (0, 0) - self.final = (8, 14) - print('final target @', self.final[0], self.final[1]) + self.final = (FINAL_X, FINAL_Y) + print('destination @', self.final[0], self.final[1]) self.rect.topleft = (0, 0) self.water = 50