import pygame from constant import size, rows, cols from decisiontree import predict from neuralnetwork import predict_image import tempfile class Tractor: def __init__(self, row, col, model, feature_columns, neuralnetwork): self.row = row self.col = col self.images = { "up": pygame.image.load("tractor/up.png"), "down": pygame.image.load("tractor/down.png"), "left": pygame.image.load("tractor/left.png"), "right": pygame.image.load("tractor/right.png") } self.direction = "down" self.model = model self.feature_columns = feature_columns self.neuralnetwork = neuralnetwork def draw(self, win): tractor_image = self.images[self.direction] win.blit(tractor_image, (self.col * size, self.row * size)) def turn_left(self): if self.direction == "up": self.direction = "left" elif self.direction == "left": self.direction = "down" elif self.direction == "down": self.direction = "right" elif self.direction == "right": self.direction = "up" def turn_right(self): if self.direction == "up": self.direction = "right" elif self.direction == "right": self.direction = "down" elif self.direction == "down": self.direction = "left" elif self.direction == "left": self.direction = "up" def move_forward(self, board): if self.direction == "up" and self.row > 0: next_row, next_col = self.row - 1, self.col elif self.direction == "left" and self.col > 0: next_row, next_col = self.row, self.col - 1 elif self.direction == "down" and self.row < rows - 1: next_row, next_col = self.row + 1, self.col elif self.direction == "right" and self.col < cols - 1: next_row, next_col = self.row, self.col + 1 else: return # Nie możemy się ruszyć poza planszę if next_row >= len(board.vegetable_names) or next_col >= len(board.vegetable_names[0]): print(f"Next position ({next_row}, {next_col}) is out of range") return vegetable_name = None if board.vegetables[next_row][next_col]: if board.vegetable_names[next_row][next_col]: print(f"Powinno być: {board.vegetable_names[next_row][next_col]}") vegetable_surface = board.vegetables[next_row][next_col] with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file: temp_file_name = temp_file.name # Zapisz nazwę pliku przed zamknięciem pygame.image.save(vegetable_surface, temp_file_name) vegetable_name = predict_image(temp_file_name, self.neuralnetwork) print(f"TRAKTOR UZNAL ZE TO: {vegetable_name}") if vegetable_name in board.vegetable_types: vegetable_type_number = board.vegetable_types[vegetable_name] else: vegetable_type_number = -1 if board.is_dirt(next_row, next_col) and board.board[next_row][next_col] != 10: soil_features = board.soil_features sample = { "wilgotnosc_gleby": soil_features["wilgotnosc_gleby"], "temperatura_gleby": soil_features["temperatura_gleby"], "opady_deszczu": soil_features["opady_deszczu"], "wiek_rosliny": soil_features["wiek_rosliny"], "proc_ekspo_na_swiatlo": soil_features["proc_ekspo_na_swiatlo"], "pora_dnia": soil_features["pora_dnia"], "pora_roku": soil_features["pora_roku"], "roslina": vegetable_type_number } should_water = predict(self.model, self.feature_columns, sample) print(f"Predykcja dla pola ({next_row}, {next_col}): {should_water}") if should_water[0].strip() == 'tak': board.set_soil(next_row, next_col) else: board.set_fakedirt(next_row, next_col) self.row, self.col = next_row, next_col # Przemieszczamy traktor niezależnie od predykcji