dodanie bfs bez ruszenia agentem
This commit is contained in:
parent
3de4a8966b
commit
b7cf4c914f
Binary file not shown.
Binary file not shown.
8
agent.py
8
agent.py
@ -84,13 +84,13 @@ class Agent(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
if keys[pygame.K_UP]:
|
if keys[pygame.K_UP]:
|
||||||
if self.direction==0 and self.rect.x < 832 - 64:
|
if self.direction==0 and self.rect.x < 832 - 64:
|
||||||
self.x_change += TILE_SIZE/2
|
self.x_change += TILE_SIZE
|
||||||
if self.direction==1 and self.rect.y < 768 - 64:
|
if self.direction==1 and self.rect.y < 768 - 64:
|
||||||
self.y_change += TILE_SIZE/2
|
self.y_change += TILE_SIZE
|
||||||
if self.direction==2 and self.rect.x > 0:
|
if self.direction==2 and self.rect.x > 0:
|
||||||
self.x_change -= TILE_SIZE/2
|
self.x_change -= TILE_SIZE
|
||||||
if self.direction==3 and self.rect.y > 0:
|
if self.direction==3 and self.rect.y > 0:
|
||||||
self.y_change -= TILE_SIZE/2
|
self.y_change -= TILE_SIZE
|
||||||
"""
|
"""
|
||||||
if keys[pygame.K_LEFT] and self.rect.x > 0:
|
if keys[pygame.K_LEFT] and self.rect.x > 0:
|
||||||
self.x_change -= TILE_SIZE
|
self.x_change -= TILE_SIZE
|
||||||
|
@ -4,3 +4,4 @@ TILE_SIZE = 64
|
|||||||
BLACK = ((0,0,0))
|
BLACK = ((0,0,0))
|
||||||
WHITE = ((255,255,255))
|
WHITE = ((255,255,255))
|
||||||
#PLAYER_SPEED = 3
|
#PLAYER_SPEED = 3
|
||||||
|
NUM_ROWS = WIDTH//TILE_SIZE
|
197
main.py
197
main.py
@ -37,13 +37,18 @@ class Game:
|
|||||||
#self.unknown_mobs = pygame.sprite.LayeredUpdates() #unknown mob
|
#self.unknown_mobs = pygame.sprite.LayeredUpdates() #unknown mob
|
||||||
self.agent = Agent(self,1,1)
|
self.agent = Agent(self,1,1)
|
||||||
self.archer_ork = Archer_ork(self,10,10)
|
self.archer_ork = Archer_ork(self,10,10)
|
||||||
|
self.enemy_cells.append(self.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
||||||
self.infantry_ork = Infantry_ork(self,10,4)
|
self.infantry_ork = Infantry_ork(self,10,4)
|
||||||
|
self.enemy_cells.append(self.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
||||||
self.infantry_ork2 = Infantry_ork2(self,6,3)
|
self.infantry_ork2 = Infantry_ork2(self,6,3)
|
||||||
|
self.enemy_cells.append(self.get_cell_number(self.infantry_ork2.x,self.infantry_ork2.y))
|
||||||
self.sauron = Sauron(self, 1, 10)
|
self.sauron = Sauron(self, 1, 10)
|
||||||
|
self.enemy_cells.append(self.get_cell_number(self.sauron.x,self.sauron.y))
|
||||||
self.flower = Health_flower(self, 8,2)
|
self.flower = Health_flower(self, 8,2)
|
||||||
#self.unknown_mob = Unknown_mob(self,8,8) #unknown mob
|
#self.unknown_mob = Unknown_mob(self,8,8) #unknown mob
|
||||||
for y in range(5):
|
for y in range(5):
|
||||||
self.rock = Rocks(self,3,y)
|
self.rock = Rocks(self,3,y)
|
||||||
|
self.wall_cells.append(self.get_cell_number(self.rock.x,self.rock.y))
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
@ -55,6 +60,10 @@ class Game:
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.running = False
|
self.running = False
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
mouse_presses = pygame.mouse.get_pressed()
|
||||||
|
if mouse_presses[0]:
|
||||||
|
self.search_in_breadth_first_approach()
|
||||||
|
|
||||||
def map(self): # tworzenie mapy
|
def map(self): # tworzenie mapy
|
||||||
self.clock.tick(FRAMERATE)
|
self.clock.tick(FRAMERATE)
|
||||||
@ -73,6 +82,194 @@ class Game:
|
|||||||
self.update()
|
self.update()
|
||||||
self.map()
|
self.map()
|
||||||
|
|
||||||
|
open_queue = []
|
||||||
|
close_queue = []
|
||||||
|
wall_cells = []
|
||||||
|
enemy_cells = []
|
||||||
|
|
||||||
|
def search_in_breadth_first_approach(self):
|
||||||
|
print("x is: ", self.agent.x, "y is: ", 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)
|
||||||
|
|
||||||
|
path = []
|
||||||
|
processing = True
|
||||||
|
find_path = False
|
||||||
|
while processing: # main loop
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
exit()
|
||||||
|
|
||||||
|
if len(self.open_queue) > 0 :
|
||||||
|
current_node_cell = self.open_queue.pop(0) # remove and get first element of open_queue
|
||||||
|
|
||||||
|
# check if this cell allready processed
|
||||||
|
if(current_node_cell in self.close_queue):
|
||||||
|
continue
|
||||||
|
|
||||||
|
print("current cell number is: ", current_node_cell)
|
||||||
|
print("goal cell/s is/are at: ", goal_cell)
|
||||||
|
|
||||||
|
# cheking for goal
|
||||||
|
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)
|
||||||
|
found_goal_cell = current_node_cell
|
||||||
|
print("goal found, now find the path from close_queue, close_queue is:", self.close_queue)
|
||||||
|
processing = False
|
||||||
|
find_path = True
|
||||||
|
self.clock.tick(2) # stay some time here without closing the windoew
|
||||||
|
else:
|
||||||
|
# goal not found, continue processing
|
||||||
|
child_node_cells = self.get_child_nodes(current_node_cell) # find possible child cells nodes to process
|
||||||
|
self.close_queue.append(current_node_cell) # putting the processed node to closed queue
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
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)
|
||||||
|
print("open queue: ", self.open_queue, "open_queue length: ", len(self.open_queue), "\n")
|
||||||
|
else:
|
||||||
|
print("no nodes to processe, open_queue length: ", len(self.open_queue), ", open queue: ",self. open_queue)
|
||||||
|
print("closed queue (processed nodes) : ", self.close_queue)
|
||||||
|
return self.close_queue
|
||||||
|
|
||||||
|
dead_end_nodes = []
|
||||||
|
while find_path:
|
||||||
|
path.append(self.close_queue[0]) # put the agent starting cell to path.
|
||||||
|
|
||||||
|
for i in range(len(self.close_queue) -1):
|
||||||
|
from_cell = path[-1]
|
||||||
|
to_cell = self.close_queue[i+1]
|
||||||
|
|
||||||
|
if to_cell in dead_end_nodes:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if self.verify_to_cell_is_navigatable_from_from_cell(from_cell, to_cell):
|
||||||
|
path.append(to_cell)
|
||||||
|
|
||||||
|
if path[-1] == found_goal_cell:
|
||||||
|
find_path = False
|
||||||
|
else:
|
||||||
|
# a dead end has occured, start finding path avoiding this dead end cell
|
||||||
|
dead_end_nodes.append(path[-1])
|
||||||
|
path = [] # to start again
|
||||||
|
|
||||||
|
print("path: ", path)
|
||||||
|
self.move_agent(path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_cell_number(self,x, y):
|
||||||
|
cell_number = None
|
||||||
|
cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE)))
|
||||||
|
return cell_number
|
||||||
|
|
||||||
|
def get_child_nodes(self,cell_number):
|
||||||
|
children = []
|
||||||
|
up = self.get_up_cell(cell_number)
|
||||||
|
if up is not None and up not in self.wall_cells and up not in self.enemy_cells:
|
||||||
|
children.append(up)
|
||||||
|
|
||||||
|
right = self.get_right_cell(cell_number)
|
||||||
|
if right is not None and right not in self.wall_cells and up not in self.enemy_cells:
|
||||||
|
children.append(right)
|
||||||
|
|
||||||
|
down = self.get_down_cell(cell_number)
|
||||||
|
if down is not None and down not in self.wall_cells and up not in self.enemy_cells:
|
||||||
|
children.append(down)
|
||||||
|
|
||||||
|
left = self.get_left_cell(cell_number)
|
||||||
|
if left is not None and left not in self.wall_cells and up not in self.enemy_cells:
|
||||||
|
children.append(left)
|
||||||
|
|
||||||
|
return children
|
||||||
|
|
||||||
|
def get_up_cell(self,cell_number):
|
||||||
|
cell_row_number = cell_number // NUM_ROWS # current row number of agent
|
||||||
|
if (cell_row_number - 1 < 0): # above /up row number of agent
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return (cell_number - NUM_ROWS)
|
||||||
|
|
||||||
|
def get_right_cell(self,cell_number):
|
||||||
|
cell_column_number = cell_number % NUM_ROWS # current column number of agent
|
||||||
|
if (cell_column_number + 1 >= NUM_ROWS):
|
||||||
|
# current cell is at the right edge, so no rigth child / right cell available
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return (cell_number + 1) # else return next cell number
|
||||||
|
|
||||||
|
def get_down_cell(self,cell_number):
|
||||||
|
cell_row_number = cell_number // NUM_ROWS # current row number of agent
|
||||||
|
if (cell_row_number + 1 >= NUM_ROWS): # down / next row number of agent
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return (cell_number + NUM_ROWS)
|
||||||
|
|
||||||
|
def get_left_cell(self,cell_number):
|
||||||
|
cell_column_number = cell_number % NUM_ROWS # current column number of agent
|
||||||
|
if (cell_column_number - 1 < 0):
|
||||||
|
# current cell is at the left edge, so no left child / right cell available
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return (cell_number - 1) # else return previous cell number
|
||||||
|
|
||||||
|
def verify_to_cell_is_navigatable_from_from_cell(self,from_cell, to_cell):
|
||||||
|
if (to_cell in self.wall_cells or to_cell in self.enemy_cells): # if to_cell is a wall cell, return False
|
||||||
|
return False
|
||||||
|
|
||||||
|
if(from_cell + 1 == to_cell): # check to_cell is the right cell
|
||||||
|
return True
|
||||||
|
|
||||||
|
if(from_cell - 1 == to_cell): # check to_cell is the left cell
|
||||||
|
return True
|
||||||
|
|
||||||
|
if(from_cell - NUM_ROWS == to_cell): # check to_cell is the top / up cell
|
||||||
|
return True
|
||||||
|
|
||||||
|
if(from_cell + NUM_ROWS == to_cell): # check to_cell is the down / bottom cell
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False # Else not navigatable, return False
|
||||||
|
|
||||||
|
|
||||||
|
# trzeba poprawić poruszanie się agenta, sam bfs działa dobrze raczej
|
||||||
|
def move_agent(self,path):
|
||||||
|
for cell_to_move in path:
|
||||||
|
x, y = self.get_top_left_cordinates_given_cell_number(cell_to_move)
|
||||||
|
print("moving to cell : ", cell_to_move, " of cordinates x: ", x, ", y: ", y, ", line_width: ", TILE_SIZE)
|
||||||
|
if x > self.agent.x and y == self.agent.y:
|
||||||
|
self.agent.direction = 0
|
||||||
|
if x == self.agent.x and y > self.agent.y:
|
||||||
|
self.agent.direction = 1
|
||||||
|
if x < self.agent.x and y == self.agent.y:
|
||||||
|
self.agent.direction = 2
|
||||||
|
if x == self.agent.x and y < self.agent.y:
|
||||||
|
self.agent.direction = 3
|
||||||
|
if self.agent.direction==0:
|
||||||
|
self.agent.x += TILE_SIZE
|
||||||
|
if self.agent.direction==1:
|
||||||
|
self.agent.y += TILE_SIZE
|
||||||
|
if self.agent.direction==2:
|
||||||
|
self.agent.x -= TILE_SIZE
|
||||||
|
if self.agent.direction==3:
|
||||||
|
self.agent.y -= TILE_SIZE
|
||||||
|
|
||||||
|
print("moved agent attributes: agent.x: ", self.agent.x, ", agent.y: ", self.agent.y)
|
||||||
|
self.clock.tick(2)
|
||||||
|
|
||||||
|
def get_top_left_cordinates_given_cell_number(self,cell_to_move):
|
||||||
|
cell_row_number = cell_to_move // NUM_ROWS # cell row number
|
||||||
|
cell_column_number = cell_to_move % NUM_ROWS # cell column number
|
||||||
|
|
||||||
|
y = cell_row_number * TILE_SIZE
|
||||||
|
x = cell_column_number * TILE_SIZE
|
||||||
|
return x, y
|
||||||
|
|
||||||
|
|
||||||
g = Game()
|
g = Game()
|
||||||
g.new()
|
g.new()
|
||||||
while g.running:
|
while g.running:
|
||||||
|
Loading…
Reference in New Issue
Block a user