bfs działający
This commit is contained in:
parent
b7cf4c914f
commit
61d7ab2fe3
Binary file not shown.
BIN
__pycache__/bfs.cpython-310.pyc
Normal file
BIN
__pycache__/bfs.cpython-310.pyc
Normal file
Binary file not shown.
BIN
__pycache__/main.cpython-310.pyc
Normal file
BIN
__pycache__/main.cpython-310.pyc
Normal file
Binary file not shown.
3
agent.py
3
agent.py
@ -230,6 +230,3 @@ class Agent(pygame.sprite.Sprite):
|
|||||||
self.game.SCREEN.blit(lvlDisplay, (370,780))
|
self.game.SCREEN.blit(lvlDisplay, (370,780))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# brakuje schematu przeszukiwania
|
|
||||||
|
|
||||||
|
144
main.py
144
main.py
@ -63,7 +63,7 @@ class Game:
|
|||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
mouse_presses = pygame.mouse.get_pressed()
|
mouse_presses = pygame.mouse.get_pressed()
|
||||||
if mouse_presses[0]:
|
if mouse_presses[0]:
|
||||||
self.search_in_breadth_first_approach()
|
self.bfs()
|
||||||
|
|
||||||
def map(self): # tworzenie mapy
|
def map(self): # tworzenie mapy
|
||||||
self.clock.tick(FRAMERATE)
|
self.clock.tick(FRAMERATE)
|
||||||
@ -82,63 +82,60 @@ class Game:
|
|||||||
self.update()
|
self.update()
|
||||||
self.map()
|
self.map()
|
||||||
|
|
||||||
|
# BFS ALGORITHM
|
||||||
|
|
||||||
open_queue = []
|
open_queue = []
|
||||||
close_queue = []
|
close_queue = []
|
||||||
wall_cells = []
|
wall_cells = []
|
||||||
enemy_cells = []
|
enemy_cells = []
|
||||||
|
|
||||||
def search_in_breadth_first_approach(self):
|
def bfs(self):
|
||||||
print("x is: ", self.agent.x, "y is: ", self.agent.y)
|
print("x: ", self.agent.x, "y: ", self.agent.y)
|
||||||
|
|
||||||
self.open_queue.append(self.get_cell_number(self.agent.x,self.agent.y))
|
self.open_queue.append(self.get_cell_number(self.agent.x,self.agent.y))
|
||||||
goal_cell = self.get_cell_number(self.flower.x,self.flower.y)
|
# tutaj dodaje się cel agenta
|
||||||
|
goal_cell = self.get_cell_number(448,128)
|
||||||
|
|
||||||
path = []
|
path = []
|
||||||
processing = True
|
processing = True
|
||||||
find_path = False
|
find_path = False
|
||||||
while processing: # main loop
|
while processing: # główna pętla
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
if len(self.open_queue) > 0 :
|
if len(self.open_queue) > 0 :
|
||||||
current_node_cell = self.open_queue.pop(0) # remove and get first element of open_queue
|
current_node_cell = self.open_queue.pop(0)
|
||||||
|
|
||||||
# check if this cell allready processed
|
|
||||||
if(current_node_cell in self.close_queue):
|
if(current_node_cell in self.close_queue):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print("current cell number is: ", current_node_cell)
|
print("Aktualna kratka: ", current_node_cell)
|
||||||
print("goal cell/s is/are at: ", goal_cell)
|
print("Cel znajduje się na kratce: ", goal_cell)
|
||||||
|
|
||||||
# cheking for goal
|
|
||||||
if (current_node_cell == goal_cell):
|
if (current_node_cell == goal_cell):
|
||||||
# add the goal cell to close queue too, this is helpfull to find the path
|
|
||||||
self.close_queue.append(current_node_cell)
|
self.close_queue.append(current_node_cell)
|
||||||
found_goal_cell = current_node_cell
|
found_goal_cell = current_node_cell
|
||||||
print("goal found, now find the path from close_queue, close_queue is:", self.close_queue)
|
print("Znaleziono cel, szukanie drogi z odwiedzonych węzłów, kolejka odwiedzonych:", self.close_queue)
|
||||||
processing = False
|
processing = False
|
||||||
find_path = True
|
find_path = True
|
||||||
self.clock.tick(2) # stay some time here without closing the windoew
|
self.clock.tick(2)
|
||||||
else:
|
else:
|
||||||
# goal not found, continue processing
|
child_node_cells = self.get_child_nodes(current_node_cell)
|
||||||
child_node_cells = self.get_child_nodes(current_node_cell) # find possible child cells nodes to process
|
self.close_queue.append(current_node_cell)
|
||||||
self.close_queue.append(current_node_cell) # putting the processed node to closed queue
|
print("Sąsiedzi: ", child_node_cells)
|
||||||
|
|
||||||
# add child nodes to open queue only if they are already not in both open_queue and close_queue
|
|
||||||
print("childe nodes: ", child_node_cells)
|
|
||||||
for child_node in child_node_cells:
|
for child_node in child_node_cells:
|
||||||
if child_node not in self.open_queue and child_node not in self.close_queue:
|
if child_node not in self.open_queue and child_node not in self.close_queue:
|
||||||
self.open_queue.append(child_node) # add children to END OF THE OPEN QUEUE (BFS)
|
self.open_queue.append(child_node)
|
||||||
print("open queue: ", self.open_queue, "open_queue length: ", len(self.open_queue), "\n")
|
print("Kolejka: ", self.open_queue, "\n")
|
||||||
else:
|
else:
|
||||||
print("no nodes to processe, open_queue length: ", len(self.open_queue), ", open queue: ",self. open_queue)
|
print("Brak nowych węzłów, kolejka: ",self. open_queue)
|
||||||
print("closed queue (processed nodes) : ", self.close_queue)
|
print("Odwiedzone : ", self.close_queue)
|
||||||
return self.close_queue
|
return self.close_queue
|
||||||
|
|
||||||
dead_end_nodes = []
|
dead_end_nodes = []
|
||||||
while find_path:
|
while find_path:
|
||||||
path.append(self.close_queue[0]) # put the agent starting cell to path.
|
path.append(self.close_queue[0])
|
||||||
|
|
||||||
for i in range(len(self.close_queue) -1):
|
for i in range(len(self.close_queue) -1):
|
||||||
from_cell = path[-1]
|
from_cell = path[-1]
|
||||||
@ -147,22 +144,20 @@ class Game:
|
|||||||
if to_cell in dead_end_nodes:
|
if to_cell in dead_end_nodes:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if self.verify_to_cell_is_navigatable_from_from_cell(from_cell, to_cell):
|
if self.verify_move(from_cell, to_cell):
|
||||||
path.append(to_cell)
|
path.append(to_cell)
|
||||||
|
|
||||||
if path[-1] == found_goal_cell:
|
if path[-1] == found_goal_cell:
|
||||||
find_path = False
|
find_path = False
|
||||||
else:
|
else:
|
||||||
# a dead end has occured, start finding path avoiding this dead end cell
|
|
||||||
dead_end_nodes.append(path[-1])
|
dead_end_nodes.append(path[-1])
|
||||||
path = [] # to start again
|
path = []
|
||||||
|
|
||||||
print("path: ", path)
|
print("Droga: ", path)
|
||||||
self.move_agent(path)
|
self.move_agent(path)
|
||||||
|
|
||||||
|
|
||||||
|
def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki
|
||||||
def get_cell_number(self,x, y):
|
|
||||||
cell_number = None
|
cell_number = None
|
||||||
cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE)))
|
cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE)))
|
||||||
return cell_number
|
return cell_number
|
||||||
@ -187,83 +182,88 @@ class Game:
|
|||||||
|
|
||||||
return children
|
return children
|
||||||
|
|
||||||
|
|
||||||
def get_up_cell(self,cell_number):
|
def get_up_cell(self,cell_number):
|
||||||
cell_row_number = cell_number // NUM_ROWS # current row number of agent
|
cell_row_number = cell_number // NUM_ROWS
|
||||||
if (cell_row_number - 1 < 0): # above /up row number of agent
|
if (cell_row_number - 1 < 0):
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return (cell_number - NUM_ROWS)
|
return (cell_number - NUM_ROWS)
|
||||||
|
|
||||||
def get_right_cell(self,cell_number):
|
def get_right_cell(self,cell_number):
|
||||||
cell_column_number = cell_number % NUM_ROWS # current column number of agent
|
cell_column_number = cell_number % NUM_ROWS
|
||||||
if (cell_column_number + 1 >= NUM_ROWS):
|
if (cell_column_number + 1 >= NUM_ROWS):
|
||||||
# current cell is at the right edge, so no rigth child / right cell available
|
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return (cell_number + 1) # else return next cell number
|
return (cell_number + 1)
|
||||||
|
|
||||||
def get_down_cell(self,cell_number):
|
def get_down_cell(self,cell_number):
|
||||||
cell_row_number = cell_number // NUM_ROWS # current row number of agent
|
cell_row_number = cell_number // NUM_ROWS
|
||||||
if (cell_row_number + 1 >= NUM_ROWS): # down / next row number of agent
|
if (cell_row_number + 1 >= NUM_ROWS):
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return (cell_number + NUM_ROWS)
|
return (cell_number + NUM_ROWS)
|
||||||
|
|
||||||
def get_left_cell(self,cell_number):
|
def get_left_cell(self,cell_number):
|
||||||
cell_column_number = cell_number % NUM_ROWS # current column number of agent
|
cell_column_number = cell_number % NUM_ROWS
|
||||||
if (cell_column_number - 1 < 0):
|
if (cell_column_number - 1 < 0):
|
||||||
# current cell is at the left edge, so no left child / right cell available
|
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return (cell_number - 1) # else return previous cell number
|
return (cell_number - 1)
|
||||||
|
|
||||||
def verify_to_cell_is_navigatable_from_from_cell(self,from_cell, to_cell):
|
def verify_move(self,from_cell, to_cell): #sprawdzenie czy ruch jest poprawny czyt. czy następna kratka to przeszkoda lub mob
|
||||||
if (to_cell in self.wall_cells or to_cell in self.enemy_cells): # if to_cell is a wall cell, return False
|
if (to_cell in self.wall_cells or to_cell in self.enemy_cells):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if(from_cell + 1 == to_cell): # check to_cell is the right cell
|
if(from_cell + 1 == to_cell):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if(from_cell - 1 == to_cell): # check to_cell is the left cell
|
if(from_cell - 1 == to_cell):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if(from_cell - NUM_ROWS == to_cell): # check to_cell is the top / up cell
|
if(from_cell - NUM_ROWS == to_cell):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if(from_cell + NUM_ROWS == to_cell): # check to_cell is the down / bottom cell
|
if(from_cell + NUM_ROWS == to_cell):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False # Else not navigatable, return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# trzeba poprawić poruszanie się agenta, sam bfs działa dobrze raczej
|
|
||||||
def move_agent(self,path):
|
def move_agent(self,path):
|
||||||
for cell_to_move in path:
|
for cell_to_move in path:
|
||||||
x, y = self.get_top_left_cordinates_given_cell_number(cell_to_move)
|
x, y = self.get_coordinates(cell_to_move)
|
||||||
print("moving to cell : ", cell_to_move, " of cordinates x: ", x, ", y: ", y, ", line_width: ", TILE_SIZE)
|
print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
||||||
if x > self.agent.x and y == self.agent.y:
|
if(self.get_cell_number(self.agent.x,self.agent.y)!=cell_to_move):
|
||||||
self.agent.direction = 0
|
if x > self.agent.rect.x:
|
||||||
if x == self.agent.x and y > self.agent.y:
|
self.agent.direction = 0
|
||||||
self.agent.direction = 1
|
elif y > self.agent.rect.y:
|
||||||
if x < self.agent.x and y == self.agent.y:
|
self.agent.direction = 1
|
||||||
self.agent.direction = 2
|
elif x < self.agent.rect.x:
|
||||||
if x == self.agent.x and y < self.agent.y:
|
self.agent.direction = 2
|
||||||
self.agent.direction = 3
|
elif y < self.agent.rect.y:
|
||||||
if self.agent.direction==0:
|
self.agent.direction = 3
|
||||||
self.agent.x += TILE_SIZE
|
if self.agent.direction==0:
|
||||||
if self.agent.direction==1:
|
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||||
self.agent.y += TILE_SIZE
|
self.agent.x_change += TILE_SIZE
|
||||||
if self.agent.direction==2:
|
elif self.agent.direction==1:
|
||||||
self.agent.x -= TILE_SIZE
|
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||||
if self.agent.direction==3:
|
self.agent.y_change += TILE_SIZE
|
||||||
self.agent.y -= TILE_SIZE
|
elif self.agent.direction==2:
|
||||||
|
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||||
|
self.agent.x_change -= TILE_SIZE
|
||||||
|
elif self.agent.direction==3:
|
||||||
|
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
||||||
|
self.agent.y_change -= TILE_SIZE
|
||||||
|
|
||||||
print("moved agent attributes: agent.x: ", self.agent.x, ", agent.y: ", self.agent.y)
|
self.update()
|
||||||
|
self.map()
|
||||||
|
|
||||||
|
print("Położenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
||||||
self.clock.tick(2)
|
self.clock.tick(2)
|
||||||
|
|
||||||
def get_top_left_cordinates_given_cell_number(self,cell_to_move):
|
def get_coordinates(self,cell_to_move): #zamienia numer kratki na koordynaty
|
||||||
cell_row_number = cell_to_move // NUM_ROWS # cell row number
|
cell_row_number = cell_to_move // NUM_ROWS
|
||||||
cell_column_number = cell_to_move % NUM_ROWS # cell column number
|
cell_column_number = cell_to_move % NUM_ROWS
|
||||||
|
|
||||||
y = cell_row_number * TILE_SIZE
|
y = cell_row_number * TILE_SIZE
|
||||||
x = cell_column_number * TILE_SIZE
|
x = cell_column_number * TILE_SIZE
|
||||||
|
Loading…
Reference in New Issue
Block a user