1st part AStar implementation

This commit is contained in:
Marcin Dobrowolski 2020-04-24 16:14:58 +02:00
parent c227126bb2
commit cdbc599d14
9 changed files with 118 additions and 22 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,16 +4,16 @@ import pygame
class Graphics:
def __init__(self):
self.image = {
'floor': pygame.image.load('../resources/images/floor.jpg'),
'wall': pygame.image.load('../resources/images/wall.png'), #
'bar': pygame.image.load('../resources/images/table3.png'), #
'bar_floor': pygame.image.load('../resources/images/waiter.png'),
'table': pygame.image.load('../resources/images/table3.png'),
'waiter': pygame.image.load('../resources/images/waiter.png'),
'chair_front': pygame.image.load('../resources/images/chair-front.png'), #
'chair_back': pygame.image.load('../resources/images/chair-back.png'), #
'chair_left': pygame.image.load('../resources/images/chair-left.png'), #
'chair_right': pygame.image.load('../resources/images/chair-right.png') #
'floor': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/floor.jpg'),
'wall': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/wall.png'), #
'bar': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/table3.png'), #
'bar_floor': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter.png'),
'table': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/table3.png'),
'waiter': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter.png'),
'chair_front': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-front.png'), #
'chair_back': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-back.png'), #
'chair_left': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-left.png'), #
'chair_right': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-right.png') #
}
self.block_size = 50
self.height = 15

View File

@ -1,4 +1,4 @@
from src.tile import Tile
from .tile import Tile
class Matrix:
@ -13,10 +13,11 @@ class Matrix:
for x in range(graphics.width):
for y in range(graphics.height):
self.matrix[x][y] = Tile(type_='floor')
self.matrix[x][y] = Tile('floor', x, y)
def set_default_restaurant(self, graphics):
lines = [line.rstrip('\n') for line in open('../resources/simulations/simulation_1.txt')]
lines = [line.rstrip('\n') for line in open(
'/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/simulations/simulation_1.txt')]
symbols = {
'_': 'floor',
'W': 'wall',
@ -33,7 +34,7 @@ class Matrix:
for x in range(graphics.width):
for y in range(graphics.height):
sign = lines[y][x]
self.matrix[x][y] = Tile(symbols[sign])
self.matrix[x][y] = Tile(symbols[sign], x, y)
def get_type(self, x, y):
return self.matrix[x][y].type

View File

@ -1,6 +1,6 @@
# TILE
class Tile:
def __init__(self, type_):
def __init__(self, type_, x, y):
self.type = type_
if self.type == 'wall':
@ -15,3 +15,17 @@ class Tile:
self.action_required = 0
# Atrybuty niezbedne dla A*
self.position = (x, y)
self.parent = None
self.totalCost = 0 # Koszt totalny czyli dystans do wierzcholka startowego + heurystyka
self.startCost = 0 # Dystans do wierzcholka startowego
# Dystant do wierzcholka koncowego oszacowany za pomoca funkcji heurystyki
self.heuristic = 0
# Operator porownywania pol
def __eq__(self, other):
return True if (self.position == other.position) else False
# dodanie atrybutu x i y do klasy Tile

View File

@ -1,6 +1,6 @@
import pygame
from src.matrix import Matrix
from .matrix import Matrix
# WAITER
@ -11,6 +11,7 @@ class Waiter(pygame.sprite.Sprite):
self.Y = 0
self.frame = 0
self.matrix = Matrix(graphics=graphics)
self.direction = 'E'
# Borders
def move(self, x, y, graphics):
@ -21,12 +22,92 @@ class Waiter(pygame.sprite.Sprite):
def update(self, event, graphics):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move(-1, 0, graphics)
if event.key == pygame.K_RIGHT:
self.move(1, 0, graphics)
if self.direction == 'N':
self.direction = 'W'
elif self.direction == 'S':
self.direction = 'E'
elif self.direction == 'E':
self.direction = 'N'
elif self.direction == 'W':
self.direction = 'S'
if event.key == pygame.K_LEFT:
if self.direction == 'N':
self.direction = 'E'
elif self.direction == 'S':
self.direction = 'W'
elif self.direction == 'E':
self.direction = 'S'
elif self.direction == 'W':
self.direction = 'N'
if event.key == pygame.K_UP:
self.move(0, -1, graphics)
if event.key == pygame.K_DOWN:
self.move(0, 1, graphics)
if self.direction == 'N':
self.move(0, 1, graphics)
if self.direction == 'S':
self.move(0, -1, graphics)
if self.direction == 'E':
self.move(1, 0, graphics)
if self.direction == 'W':
self.move(-1, 0, graphics)
print(self.X, self.Y)
# AStar
def findPath(self, goal):
#Stworzenie startowego i koncowego wierzcholka
startNode = self.matrix.matrix[self.X][self.Y]
goalNode = self.matrix.matrix[goal[0]][goal[1]]
#Inicjalizacja list
openList = []
closedList = []
openList.append(startNode)
while len(openList) > 0:
openList.sort(key=tile.totalCost)
currentNode = openList.pop(0)
closedList.append(currentNode)
if currentNode == goalNode:
pass
#Tutaj odbywac sie bedzie budowanie sciezki gdy algorytm osiagnie cel
children = []
if currentNode.X >= 0:
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X][currentNode.Y - 1])
if currentNode.X + 1 < len(self.matrix.matrix):
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X + 1][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X + 1][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y - 1])
if currentNode.Y >= 0:
if self.matrix.matrix[currentNode.X + 1][currentNode.Y].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y])
if currentNode.X - 1 >= 0:
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X - 1][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X - 1][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y - 1])
if currentNode.Y >= 0:
if self.matrix.matrix[currentNode.X - 1][currentNode.Y].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y])
for child in children:
if child in closedList:
continue