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