feat: tractor goes on the tile clicked by user.
This commit is contained in:
parent
b0ac67a105
commit
a6f51420e1
11
src/main.py
11
src/main.py
@ -1,7 +1,7 @@
|
||||
import pygame
|
||||
from field import Field
|
||||
import os
|
||||
from config import TILE_SIZE, TICK_RATE
|
||||
from config import TILE_SIZE, TICK_RATE, FINAL_X, FINAL_Y
|
||||
|
||||
if __name__ == "__main__":
|
||||
pygame.init()
|
||||
@ -13,10 +13,19 @@ if __name__ == "__main__":
|
||||
field = Field()
|
||||
|
||||
running = True
|
||||
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
x, y = pygame.mouse.get_pos()
|
||||
print(f"Mouse clicked at: ({x}, {y})")
|
||||
|
||||
grid_x = x // TILE_SIZE
|
||||
grid_y = y // TILE_SIZE
|
||||
|
||||
field.tractor.set_new_goal((grid_x, grid_y))
|
||||
|
||||
field.tractor.update()
|
||||
screen.fill(WHITE)
|
||||
|
@ -18,30 +18,33 @@ class Tractor(pygame.sprite.Sprite):
|
||||
def __init__(self, field):
|
||||
super().__init__
|
||||
self.field = field
|
||||
|
||||
self.water = 50
|
||||
self.image = pygame.image.load('images/tractor/east.png').convert_alpha()
|
||||
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
|
||||
self.rect = self.image.get_rect()
|
||||
|
||||
self.direction = STARTING_DIRECTION
|
||||
# TODO: enable tractor to start on other tile than (0,0)
|
||||
self.start = (START_X, START_Y)
|
||||
self.final = (FINAL_X, FINAL_Y)
|
||||
self.direction = 'east'
|
||||
self.start = (0, 0)
|
||||
self.final = (0, 0)
|
||||
print('destination @', self.final[0], self.final[1])
|
||||
self.rect.topleft = (self.start[0] * TILE_SIZE, self.start[1] * TILE_SIZE)
|
||||
|
||||
self.water = 50
|
||||
|
||||
#A-STAR
|
||||
came_from, total_cost = self.a_star()
|
||||
path = self.reconstruct_path(came_from)
|
||||
self.actions = self.recreate_actions(path)
|
||||
self.rect.topleft = (self.start[0] * TILE_SIZE, self.start[1] * TILE_SIZE)
|
||||
self.actions = []
|
||||
self.action_index = 0
|
||||
|
||||
# DECISION TREE:
|
||||
self.label_encoders = {}
|
||||
self.load_decision_tree_model()
|
||||
|
||||
def set_new_goal(self, goal):
|
||||
self.start = self.get_coordinates()
|
||||
self.final = goal
|
||||
came_from, total_cost = self.a_star()
|
||||
path = self.reconstruct_path(came_from)
|
||||
self.actions = self.recreate_actions(path)
|
||||
self.action_index = 0
|
||||
print(f"New goal set to: {self.final}")
|
||||
|
||||
def load_decision_tree_model(self):
|
||||
data = pd.read_csv('tree.csv')
|
||||
|
||||
@ -368,13 +371,12 @@ class Tractor(pygame.sprite.Sprite):
|
||||
if current == self.final:
|
||||
break
|
||||
|
||||
# next_node: tuple[int, int]
|
||||
for next_node in self.neighboring_nodes(coordinates=current):
|
||||
enter_cost = self.cost_of_entering_node(coordinates=next_node)
|
||||
new_cost: int = cost_so_far[current] + enter_cost
|
||||
new_cost = cost_so_far[current] + enter_cost
|
||||
if next_node not in cost_so_far or new_cost < cost_so_far[next_node]:
|
||||
cost_so_far[next_node] = new_cost
|
||||
priority = new_cost + self.manhattan_cost(current)
|
||||
priority = new_cost + self.manhattan_cost(next_node)
|
||||
heapq.heappush(fringe, (priority, next_node))
|
||||
came_from[next_node] = current
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user