Wykrywanie paczki, półki i samodzielne poruszanie wózka

This commit is contained in:
tonywesoly 2022-05-11 13:07:26 +02:00
parent 0618b40afa
commit 56314605ac
3 changed files with 57 additions and 27 deletions

View File

@ -2,6 +2,7 @@ import math
import pygame import pygame
from Global_variables import Global_variables as G_var from Global_variables import Global_variables as G_var
from Package import Package
from Shelf import Shelf from Shelf import Shelf
@ -91,6 +92,10 @@ class Pathfinding:
fringe = [] fringe = []
explored = [] explored = []
is_target_node_walkable = True
if not target_node.walkable:
target_node.walkable = True
is_target_node_walkable = False
fringe.append(start_node) fringe.append(start_node)
while len(fringe) > 0: while len(fringe) > 0:
@ -118,6 +123,8 @@ class Pathfinding:
if not neighbour in fringe: if not neighbour in fringe:
fringe.append(neighbour) fringe.append(neighbour)
target_node.walkable = is_target_node_walkable
def get_distance(self, node_a, node_b): # funckja liczy dystans dla odległości między dwoma nodami def get_distance(self, node_a, node_b): # funckja liczy dystans dla odległości między dwoma nodami
dist_x = abs(node_a.state.x - node_b.state.x) dist_x = abs(node_a.state.x - node_b.state.x)
dist_y = abs(node_a.state.y - node_b.state.y) dist_y = abs(node_a.state.y - node_b.state.y)

View File

@ -1,4 +1,6 @@
import time
from Empty import Empty from Empty import Empty
from Finding_fields import Finding_fields
from Moving_truck import Moving_truck from Moving_truck import Moving_truck
from Package import Package from Package import Package
from Package_types import Package_types from Package_types import Package_types
@ -23,14 +25,13 @@ class Environment:
# TEST CREATE PACKAGE # TEST CREATE PACKAGE
self.package_spawner = Packages_spawner(window,self.enviroment_2d) self.package_spawner = Packages_spawner(window,self.enviroment_2d)
self.package_spawner.spawn_package() self.package_spawner.spawn_package()
# new_package = Package(self.window, 26, 7)
# self.enviroment_2d[26][7] = new_package
new_truck = Truck(window, 14, 7) new_truck = Truck(window, 14, 7)
self.enviroment_2d[14][7] = new_truck self.enviroment_2d[14][7] = new_truck
self.truck = new_truck self.truck = new_truck
self.moving_truck = Moving_truck( self.moving_truck = Moving_truck(
self.window, self.enviroment_2d, self.truck, self.package_spawner) self.window, self.enviroment_2d, self.truck, self.package_spawner)
self.astar = Pathfinding(self.enviroment_2d) self.astar = Pathfinding(self.enviroment_2d)
self.finding_fields = Finding_fields(self.enviroment_2d)
def draw_all_elements(self): def draw_all_elements(self):
for row in self.enviroment_2d: for row in self.enviroment_2d:
@ -41,34 +42,25 @@ class Environment:
pygame.display.flip() pygame.display.flip()
def update_all_elements(self,event): def update_all_elements(self,event):
# self.use_astar() self.use_astar() # wywyoływanie za każdym razem astar jest bardzo zasobożerne. Lepiej raz na przejście
self.update_truck(event) self.update_truck()
time.sleep(0.5)
def use_astar(self): def use_astar(self):
start_state = State(1,self.truck.x,self.truck.y) start_state = State(1,self.truck.x,self.truck.y) # sprawić aby paczka i shelf były wyszukiwane raz
package = self.find_packate() if self.truck.has_package:
end_state = State(1,package.x, package.y) end_position = self.finding_fields.find_closest_shelf(self.truck,self.truck.package_type)
else:
end_position = self.finding_fields.find_package()
end_state = State(1,end_position.x, end_position.y)
self.astar.find_path(start_state,end_state) self.astar.find_path(start_state,end_state)
def find_packate(self): #ta funkcja została zrobiona na szybko, może nie być potrzebna w przyszłości kiedy def update_truck(self):
#ktoś wpadnie na lepsze rozwiązanie next_field_to_move = self.astar.path[0].state
for row in self.enviroment_2d: next_field_x = next_field_to_move.x - self.truck.x
for field in row: next_field_y = next_field_to_move.y - self.truck.y
if isinstance(field,Package): self.moving_truck.move(next_field_x,next_field_y)
return field
return None
def update_truck(self, event):
if event.type == KEYDOWN:
if event.key == K_LEFT:
self.moving_truck.move(-1, 0)
if event.key == K_RIGHT:
self.moving_truck.move(1, 0)
if event.key == K_UP:
self.moving_truck.move(0, -1)
if event.key == K_DOWN:
self.moving_truck.move(0, 1)
def gen_shelf_type(self): def gen_shelf_type(self):
shelve_types = list(Package_types) shelve_types = list(Package_types)

31
Finding_fields.py Normal file
View File

@ -0,0 +1,31 @@
import math
from Empty import Empty
from Package import Package
from Shelf import Shelf
class Finding_fields:
def __init__(self, enviroment_2d):
self.enviroment_2d = enviroment_2d
def find_closest_shelf(self, start_field, type):
shelves = []
for row in self.enviroment_2d:
for field in row:
if isinstance(field,Shelf):
shelves.append(field)
min_distance = math.inf
closest_shelf = None
for shelf in shelves:
if shelf.type == type:
distance = abs(start_field.x - shelf.x) + abs(start_field.y - shelf.y)
if distance < min_distance:
min_distance = distance
closest_shelf = shelf
return closest_shelf
def find_package(self):
for row in self.enviroment_2d:
for field in row:
if isinstance(field,Package) and not field.is_placed:
return field