Traktor/tractor.py

99 lines
4.1 KiB
Python
Raw Normal View History

2024-03-10 02:46:14 +01:00
import pygame
2024-05-18 17:10:56 +02:00
from constant import size, rows, cols
2024-05-18 22:51:09 +02:00
from decisiontree import predict
2024-05-23 01:57:24 +02:00
from neuralnetwork import predict_image
2024-05-26 05:12:46 +02:00
import tempfile
2024-05-18 17:10:56 +02:00
2024-03-10 02:46:14 +01:00
class Tractor:
2024-05-23 01:57:24 +02:00
def __init__(self, row, col, model, feature_columns, neuralnetwork):
2024-03-10 02:46:14 +01:00
self.row = row
self.col = col
self.images = {
2024-03-24 19:28:14 +01:00
"up": pygame.image.load("tractor/up.png"),
"down": pygame.image.load("tractor/down.png"),
"left": pygame.image.load("tractor/left.png"),
2024-05-18 17:10:56 +02:00
"right": pygame.image.load("tractor/right.png")
2024-03-10 02:46:14 +01:00
}
self.direction = "down"
2024-05-18 22:51:09 +02:00
self.model = model
self.feature_columns = feature_columns
2024-05-23 01:57:24 +02:00
self.neuralnetwork = neuralnetwork
2024-03-24 19:28:14 +01:00
2024-03-10 02:46:14 +01:00
def draw(self, win):
tractor_image = self.images[self.direction]
2024-05-18 17:10:56 +02:00
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"
2024-05-27 13:57:55 +02:00
2024-05-18 17:10:56 +02:00
def move_forward(self, board):
if self.direction == "up" and self.row > 0:
2024-05-18 22:51:09 +02:00
next_row, next_col = self.row - 1, self.col
2024-05-18 17:10:56 +02:00
elif self.direction == "left" and self.col > 0:
2024-05-18 22:51:09 +02:00
next_row, next_col = self.row, self.col - 1
2024-05-18 17:10:56 +02:00
elif self.direction == "down" and self.row < rows - 1:
2024-05-18 22:51:09 +02:00
next_row, next_col = self.row + 1, self.col
2024-05-18 17:10:56 +02:00
elif self.direction == "right" and self.col < cols - 1:
2024-05-18 22:51:09 +02:00
next_row, next_col = self.row, self.col + 1
else:
return # Nie możemy się ruszyć poza planszę
2024-05-27 14:59:44 +02:00
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
2024-05-27 13:57:55 +02:00
vegetable_name = None
2024-05-26 05:12:46 +02:00
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}")
2024-05-27 13:57:55 +02:00
if vegetable_name in board.vegetable_types:
vegetable_type_number = board.vegetable_types[vegetable_name]
else:
vegetable_type_number = -1
2024-06-08 19:46:06 +02:00
if board.is_dirt(next_row, next_col) and board.board[next_row][next_col] != 12:
2024-05-18 22:51:09 +02:00
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"],
2024-05-27 13:57:55 +02:00
"roslina": vegetable_type_number
2024-05-18 22:51:09 +02:00
}
should_water = predict(self.model, self.feature_columns, sample)
2024-05-27 14:59:44 +02:00
print(f"Predykcja dla pola ({next_row}, {next_col}): {should_water}")
2024-05-18 22:51:09 +02:00
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