5th part of AStar implementation

This commit is contained in:
Marcin Dobrowolski 2020-04-29 11:12:18 +02:00
parent 7a65079aa2
commit fefb9419b5
9 changed files with 48 additions and 47 deletions

20
main.py
View File

@ -14,11 +14,10 @@ if __name__ == "__main__":
# init functions
graphics.drawBackground(waiter.matrix)
graphics.update(waiter.X, waiter.Y)
graphics.update(waiter)
goal = None
path = [(0, 0)]
# print(waiter.findPath())
path = ''
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
@ -43,19 +42,12 @@ if __name__ == "__main__":
goal = (x, y)
path = waiter.findPath(goal)
print('goal: {}'.format(goal))
path = waiter.translatePath(path)
if path != []:
print(path)
nextStep = path.pop(0)
print(nextStep)
if path != '':
nextStep = path[0]
path = path[1:]
waiter.travel(nextStep, graphics)
print('{} {} current position'.format(waiter.X, waiter.Y))
'''
graphics.clear(waiter.X, waiter.Y)
waiter.update(event, graphics)
graphics.update(waiter.X, waiter.Y)
'''
pygame.display.flip()
clock.tick(fps)

View File

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -5,32 +5,47 @@ class Graphics:
def __init__(self):
self.image = {
'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'),
#
'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_N.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') #
'waiter_N': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_N.png'),
'waiter_S': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_S.png'),
'waiter_E': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_E.png'),
'waiter_W': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_W.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
self.width: int = 14
self.screen = pygame.display.set_mode((self.block_size * self.width, self.block_size * self.height))
self.screen = pygame.display.set_mode(
(self.block_size * self.width, self.block_size * self.height))
def drawBackground(self, matrix):
for y in range(self.height):
for x in range(self.width):
self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size))
self.screen.blit(
self.image['floor'], (x * self.block_size, y * self.block_size))
for y in range(self.height):
for x in range(self.width):
self.screen.blit(self.image[matrix.get_type(x, y)], (x * self.block_size, y * self.block_size))
self.screen.blit(self.image[matrix.get_type(
x, y)], (x * self.block_size, y * self.block_size))
def clear(self, x, y):
self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size))
self.screen.blit(self.image['floor'],
(x * self.block_size, y * self.block_size))
def update(self, x, y):
self.screen.blit(self.image['waiter'], (x * self.block_size, y * self.block_size))
def update(self, waiter):
model = 'waiter_' + waiter.direction
self.screen.blit(
self.image[model], (waiter.X * self.block_size, waiter.Y * self.block_size))

View File

@ -55,11 +55,9 @@ class Waiter(pygame.sprite.Sprite):
# 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 = []
@ -72,7 +70,6 @@ class Waiter(pygame.sprite.Sprite):
currentNode = openList.pop(0)
closedList.append(currentNode)
# Tutaj odbywac sie bedzie budowanie sciezki gdy algorytm osiagnie cel
if currentNode == goalNode:
path = []
current = currentNode
@ -230,24 +227,21 @@ class Waiter(pygame.sprite.Sprite):
return output
def travel(self, goal, graphics):
translatedPath = self.translatePath([goal])
print('{} - {} - {}'.format(self.direction, goal, translatedPath))
for character in translatedPath:
if character == 'F':
graphics.clear(self.X, self.Y)
self.update('F', graphics)
graphics.update(self.X, self.Y)
def travel(self, nextStep, graphics):
if nextStep == 'F':
graphics.clear(self.X, self.Y)
self.update('F', graphics)
graphics.update(self)
if character == 'R':
#graphics.clear(self.X, self.Y)
self.update('R', graphics)
#graphics.update(self.X, self.Y)
if nextStep == 'R':
graphics.clear(self.X, self.Y)
self.update('R', graphics)
graphics.update(self)
if character == 'L':
#graphics.clear(self.X, self.Y)
self.update('L', graphics)
#graphics.update(self.X, self.Y)
if nextStep == 'L':
graphics.clear(self.X, self.Y)
self.update('L', graphics)
graphics.update(self)
def getTotalCost(tile):