Draw map colors changed
This commit is contained in:
parent
e0a0e4f456
commit
07ff61b1dd
87
UI/Apath.py
87
UI/Apath.py
@ -1,92 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from heapq import * # pylint: disable=unused-wildcard-import
|
from heapq import * # pylint: disable=unused-wildcard-import
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def astar(table, start, end):
|
|
||||||
"""Returns a list of tuples as a path from the given start to the given end in the given table"""
|
|
||||||
|
|
||||||
# Create start and end node
|
|
||||||
start_node = table[start[0]][start[1]]
|
|
||||||
start_node.g = start_node.h = start_node.f = 0
|
|
||||||
end_node = table[end[0]][end[1]]
|
|
||||||
end_node.g = end_node.h = end_node.f = 0
|
|
||||||
|
|
||||||
# Initialize both open and closed list
|
|
||||||
open_list = []
|
|
||||||
closed_list = []
|
|
||||||
|
|
||||||
# Add the start node
|
|
||||||
open_list.append(start_node)
|
|
||||||
|
|
||||||
# Loop until you find the end
|
|
||||||
while len(open_list) > 0:
|
|
||||||
|
|
||||||
# Get the current node
|
|
||||||
current_node = open_list[0]
|
|
||||||
current_index = 0
|
|
||||||
for index, item in enumerate(open_list):
|
|
||||||
if item.f < current_node.f:
|
|
||||||
current_node = item
|
|
||||||
current_index = index
|
|
||||||
|
|
||||||
# Pop current off open list, add to closed list
|
|
||||||
open_list.pop(current_index)
|
|
||||||
closed_list.append(current_node)
|
|
||||||
|
|
||||||
# Found the goal
|
|
||||||
if current_node == end_node:
|
|
||||||
path = []
|
|
||||||
current = current_node
|
|
||||||
while current is not None:
|
|
||||||
path.append((current.row,current.col))
|
|
||||||
current = current.parent
|
|
||||||
return path[::-1] # Return reversed path
|
|
||||||
|
|
||||||
# Generate children
|
|
||||||
children = []
|
|
||||||
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]: # Adjacent squares
|
|
||||||
|
|
||||||
# Get node position #check
|
|
||||||
node_position = (current_node.row + new_position[0], current_node.col + new_position[1])
|
|
||||||
|
|
||||||
# Make sure within range
|
|
||||||
if node_position[0] > (len(table) - 1) or node_position[0] < 0 or node_position[1] > (len(table[len(table)-1]) -1) or node_position[1] < 0:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Make sure walkable terrain
|
|
||||||
if table[node_position[0]][node_position[1]].field_type == 3:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Create new node
|
|
||||||
table[node_position[0]][node_position[1]].parent = current_node
|
|
||||||
#new_node = table[node_position[0]][node_position[1]]
|
|
||||||
print("Dla :",node_position[0],node_position[1],"rodzicem jest:",current_node.row,current_node.col)
|
|
||||||
|
|
||||||
# Append
|
|
||||||
children.append(table[node_position[0]][node_position[1]])
|
|
||||||
|
|
||||||
# Loop through children
|
|
||||||
for child in children:
|
|
||||||
|
|
||||||
# Child is on the closed list
|
|
||||||
for closed_child in closed_list:
|
|
||||||
if child == closed_child:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Create the f, g, and h values
|
|
||||||
child.g = current_node.g + 1
|
|
||||||
child.h = ((child.row - end_node.row) ** 2) + ((child.col - end_node.col) ** 2)
|
|
||||||
child.f = child.g + child.h
|
|
||||||
|
|
||||||
# Child is already in the open list
|
|
||||||
for open_node in open_list:
|
|
||||||
if child == open_node and child.g > open_node.g:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Add the child to the open list
|
|
||||||
open_list.append(child)
|
|
||||||
|
|
||||||
class AStarNode():
|
class AStarNode():
|
||||||
|
|
||||||
def __init__(self, parent=None, position=None):
|
def __init__(self, parent=None, position=None):
|
||||||
@ -101,7 +15,6 @@ class AStarNode():
|
|||||||
|
|
||||||
|
|
||||||
def APath(table, start, end):
|
def APath(table, start, end):
|
||||||
"""Returns a list of tuples as a path from the given start to the given end in the given table"""
|
|
||||||
|
|
||||||
# Create start and end node
|
# Create start and end node
|
||||||
start_node = AStarNode(None, start)
|
start_node = AStarNode(None, start)
|
||||||
|
@ -39,6 +39,7 @@ class Node:
|
|||||||
RED = (255, 0, 0)
|
RED = (255, 0, 0)
|
||||||
BLUE = (0, 0, 255)
|
BLUE = (0, 0, 255)
|
||||||
ORANGE = (255, 165, 0)
|
ORANGE = (255, 165, 0)
|
||||||
|
PINK = (199,21,133)
|
||||||
|
|
||||||
def __init__(self, row: int, col: int,
|
def __init__(self, row: int, col: int,
|
||||||
field_type: int = 0):
|
field_type: int = 0):
|
||||||
@ -76,3 +77,5 @@ class Node:
|
|||||||
return self.BLUE
|
return self.BLUE
|
||||||
elif self.field_type == 3:
|
elif self.field_type == 3:
|
||||||
return self.ORANGE
|
return self.ORANGE
|
||||||
|
elif self.field_type == 4:
|
||||||
|
return self.PINK
|
||||||
|
26
UI/window.py
26
UI/window.py
@ -2,7 +2,7 @@ import pygame as pg
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
from UI.grid import Grid, Node
|
from UI.grid import Grid, Node
|
||||||
from UI.Apath import APath, astar
|
from UI.Apath import APath
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -32,36 +32,26 @@ class Window():
|
|||||||
grid.change_field(19, 19, 2)
|
grid.change_field(19, 19, 2)
|
||||||
|
|
||||||
#random obsticle
|
#random obsticle
|
||||||
for x in range(70):
|
for x in range(100):
|
||||||
grid.change_field(random.randint(1,18),random.randint(1,18),3)
|
grid.change_field(random.randint(1,18),random.randint(1,18),3)
|
||||||
|
|
||||||
#path
|
#path
|
||||||
path = [(i, i) for i in range(1, 20, 1)]
|
path = [(i, i) for i in range(1, 20, 1)]
|
||||||
self.grid.draw_map(self.screen)
|
self.grid.draw_map(self.screen)
|
||||||
|
|
||||||
#convert table to support Apath algoritm
|
#copy table
|
||||||
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
|
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
|
||||||
|
|
||||||
#for i,x in enumerate(array):
|
|
||||||
# for j,y in enumerate(x):
|
|
||||||
# if y.field_type == 3:
|
|
||||||
# array[i][j] = None
|
|
||||||
|
|
||||||
#nodes_array = np.array(array)
|
|
||||||
|
|
||||||
#Run A star
|
|
||||||
|
|
||||||
#path, check = Astar(nodes_array, (0,0), (19, 19))
|
|
||||||
#print(path,"\n\n",check,"\n\n")
|
|
||||||
path = APath(array,(0,0),(19,19))
|
path = APath(array,(0,0),(19,19))
|
||||||
print(path,"\n\n")
|
print(path,"\n\n")
|
||||||
|
|
||||||
|
for index, t in enumerate(path):
|
||||||
for t in path:
|
x,y =t
|
||||||
x, y = t
|
if index != 0:
|
||||||
self.grid.change_field(x-1, y-1, 0)
|
self.grid.change_field(path[index-1][0],path[index-1][1],4)
|
||||||
self.grid.change_field(x, y, 1)
|
self.grid.change_field(x, y, 1)
|
||||||
self.grid.draw_node(self.screen, x - 1, y - 1)
|
self.grid.draw_node(self.screen, path[index-1][0],path[index-1][1])
|
||||||
self.grid.draw_node(self.screen, x, y)
|
self.grid.draw_node(self.screen, x, y)
|
||||||
pg.time.delay(500)
|
pg.time.delay(500)
|
||||||
pg.quit() # pylint: disable=no-member
|
pg.quit() # pylint: disable=no-member
|
||||||
|
Loading…
Reference in New Issue
Block a user