A* już działa, traktor jedzie na pole i tam pracuje. Zostało przeszukiwanie i sprawdzenie czy są jakieś błędy
This commit is contained in:
parent
62319b857e
commit
d3214ddeab
66
functions.py
66
functions.py
@ -1,15 +1,25 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import sys
|
import sys, time
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
import config
|
import config
|
||||||
from itertools import product, starmap
|
from queue import PriorityQueue
|
||||||
|
|
||||||
|
|
||||||
def quit():
|
def quit():
|
||||||
print("Zamykanie...")
|
print("Zamykanie...")
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
class Node():
|
||||||
|
def __init__(self, parent=None, position=None):
|
||||||
|
self.parent = parent
|
||||||
|
self.position = position
|
||||||
|
|
||||||
|
self.g = 0
|
||||||
|
self.h = 0
|
||||||
|
self.f = 0
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.position == other.position
|
||||||
|
|
||||||
def pressed(key, traktor_poz):
|
def pressed(key, traktor_poz):
|
||||||
if key[K_d]:
|
if key[K_d]:
|
||||||
@ -126,8 +136,55 @@ def pathfinding():
|
|||||||
closed = []
|
closed = []
|
||||||
start_position = [int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)]
|
start_position = [int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)]
|
||||||
end_point = search(start_position,avaiable_value)
|
end_point = search(start_position,avaiable_value)
|
||||||
# print(end_point)
|
if start_position == end_point:
|
||||||
|
work([int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)])
|
||||||
|
else:
|
||||||
|
route = a_star(start_position,end_point)
|
||||||
|
for i in route[::-1]:
|
||||||
|
poz = config.TRAKTOR_POZ
|
||||||
|
if i[0]< poz[0]:
|
||||||
|
move_down()
|
||||||
|
elif i[0]> poz[0]:
|
||||||
|
move_up()
|
||||||
|
elif i[1]> poz[1]:
|
||||||
|
move_right()
|
||||||
|
elif i[1]< poz[1]:
|
||||||
|
move_left()
|
||||||
|
time.sleep(0.2)
|
||||||
|
work([int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)])
|
||||||
|
|
||||||
|
def a_star(start, end):
|
||||||
|
a_queue = PriorityQueue()
|
||||||
|
a_queue.put(start,0)
|
||||||
|
cost = {tuple(start): 0}
|
||||||
|
path_from = {tuple(start): None}
|
||||||
|
finall_path = [tuple(end)]
|
||||||
|
found = 0
|
||||||
|
while not a_queue.empty():
|
||||||
|
current = tuple(a_queue.get())
|
||||||
|
|
||||||
|
if current == tuple(end):
|
||||||
|
break
|
||||||
|
|
||||||
|
for next in points(current):
|
||||||
|
new_cost = cost[tuple(current)] + int(config.POLE_STAN[next[0],next[1]])
|
||||||
|
if tuple(next) not in cost or new_cost < cost[tuple(next)]:
|
||||||
|
cost[tuple(next)] = new_cost
|
||||||
|
priority = new_cost + heuristic(end, next)
|
||||||
|
a_queue.put(next,priority)
|
||||||
|
path_from[tuple(next)] = current
|
||||||
|
if next == end:
|
||||||
|
found = 1
|
||||||
|
break
|
||||||
|
if found:
|
||||||
|
break
|
||||||
|
|
||||||
|
pth = path_from[tuple(end)]
|
||||||
|
while not pth==tuple(start):
|
||||||
|
finall_path.append(pth)
|
||||||
|
pth = path_from[pth]
|
||||||
|
|
||||||
|
return finall_path
|
||||||
|
|
||||||
closed = []
|
closed = []
|
||||||
def search(point,values):
|
def search(point,values):
|
||||||
@ -138,7 +195,6 @@ def search(point,values):
|
|||||||
else:
|
else:
|
||||||
closed.append(point)
|
closed.append(point)
|
||||||
childs = points(point)
|
childs = points(point)
|
||||||
# print(closed)
|
|
||||||
while childs:
|
while childs:
|
||||||
child = childs.pop()
|
child = childs.pop()
|
||||||
if child not in closed:
|
if child not in closed:
|
||||||
|
Loading…
Reference in New Issue
Block a user