From 505926461e84e8b3b970d43833551febb8989aa8 Mon Sep 17 00:00:00 2001 From: Wojciech Kubicki Date: Mon, 25 Mar 2024 13:04:21 +0100 Subject: [PATCH] feat(logs): print out growth modifier based on tile neighbours --- src/tractor.py | 56 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/src/tractor.py b/src/tractor.py index 1de6f5e4..8924125f 100644 --- a/src/tractor.py +++ b/src/tractor.py @@ -1,6 +1,6 @@ import pygame import os -from kb import ile_podlac +from kb import ile_podlac, multi_sasiedzi from tile import Tile from config import TILE_SIZE @@ -26,24 +26,16 @@ class Tractor(pygame.sprite.Sprite): def move(self, direction): if direction == "up" and self.rect.y > 0: self.rect.y -= TILE_SIZE - self.print_tile_type() + self.log_info() elif direction == "down" and self.rect.y < 15 * TILE_SIZE: self.rect.y += TILE_SIZE - self.print_tile_type() + self.log_info() elif direction == "left" and self.rect.x > 0: self.rect.x -= TILE_SIZE - self.print_tile_type() + self.log_info() elif direction == "right" and self.rect.x < 15 * TILE_SIZE: self.rect.x += TILE_SIZE - self.print_tile_type() - - curent_tile = self.get_current_tile() - water_needed = ile_podlac(curent_tile.type, curent_tile.faza)[0]['Woda'] - if self.water >= water_needed: - print(f"💦 watered {curent_tile.type} with {water_needed} liters\n") - self.water -= water_needed - else: - print(f"❗ {water_needed - self.water} more litres of water needed to water {curent_tile.type}\n") + self.log_info() def update(self): @@ -61,11 +53,28 @@ class Tractor(pygame.sprite.Sprite): print(f"💧 replenished water level: {self.water} litres\n") - def print_tile_type(self): + def log_info(self): + # print on what tile type the tractor is on 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") + + # print if the tile can be watered + current_tile = self.get_current_tile() + water_needed = ile_podlac(current_tile.type, current_tile.faza)[0]['Woda'] + if self.water >= water_needed: + print(f"💦 watered {current_tile.type} with {water_needed} liters") + self.water -= water_needed + else: + print(f"❗ {water_needed - self.water} more litres of water needed to water {current_tile.type}") + + # print out what are the neighbors of the current tile and their effect on growth + neighbors = self.get_neighbors_list() + modifier = multi_sasiedzi(current_tile.type, neighbors)[0]['Mul'] + print(f"🌱 the growth modifier for {current_tile.type} on this tile is ~{modifier:.2f} based on its neighbors: {', '.join(neighbors)}") + + print() # empty line at end of log statement def get_current_tile(self): @@ -73,3 +82,22 @@ class Tractor(pygame.sprite.Sprite): y = self.rect.y // TILE_SIZE current_tile = self.field.tiles.sprites()[y * 16 + x] return current_tile + + + def get_neighbors_list(self): + x = self.rect.x // TILE_SIZE + y = self.rect.y // TILE_SIZE + neighbors = [] + self.field.tiles.sprites()[y * 16 + x].type + + if x > 0: + neighbors.append(self.field.tiles.sprites()[y * 16 + x - 1].type) + if x < 15: + neighbors.append(self.field.tiles.sprites()[y * 16 + x + 1].type) + if y > 0: + neighbors.append(self.field.tiles.sprites()[(y - 1) * 16 + x].type) + if y < 15: + neighbors.append(self.field.tiles.sprites()[(y + 1) * 16 + x].type) + + return neighbors +