Traktor/tractor.py

76 lines
3.0 KiB
Python

import pygame
from constant import size, rows, cols
from decisiontree import predict
class Tractor:
def __init__(self, row, col, model, feature_columns):
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
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 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": board.vegetable_types[board.vegetables[next_row][next_col]]
}
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