Traktor/tractor.py

87 lines
3.5 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 02:12:52 +02:00
from neuralnetwork import predict_image
2024-05-18 22:51:09 +02:00
2024-05-18 17:10:56 +02:00
2024-03-10 02:46:14 +01:00
class Tractor:
2024-05-23 02:12:52 +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 02:12:52 +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-23 02:12:52 +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-23 02:12:52 +02:00
image_path = board.vegetables[next_row][next_col] # Ścieżka do obrazu warzywa
# Predykcja warzywa po obrazie
if image_path is not None:
print(image_path, "mialo byc")
vegetable_name = predict_image(image_path, self.neuralnetwork)
print(vegetable_name, "jest")
2024-05-18 22:51:09 +02:00
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"],
2024-05-23 02:12:52 +02:00
"roslina": "warzywa/" + str(vegetable_name) + ".png"
2024-05-18 22:51:09 +02:00
}
should_water = predict(self.model, self.feature_columns, sample)
print(f"Predykcja dla pola ({next_row}, {next_col}) z cechami {sample}: {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