5th part of AStar implementation
This commit is contained in:
parent
7a65079aa2
commit
fefb9419b5
20
main.py
20
main.py
@ -14,11 +14,10 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# init functions
|
# init functions
|
||||||
graphics.drawBackground(waiter.matrix)
|
graphics.drawBackground(waiter.matrix)
|
||||||
graphics.update(waiter.X, waiter.Y)
|
graphics.update(waiter)
|
||||||
|
|
||||||
goal = None
|
goal = None
|
||||||
path = [(0, 0)]
|
path = ''
|
||||||
# print(waiter.findPath())
|
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
@ -43,19 +42,12 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
goal = (x, y)
|
goal = (x, y)
|
||||||
path = waiter.findPath(goal)
|
path = waiter.findPath(goal)
|
||||||
print('goal: {}'.format(goal))
|
path = waiter.translatePath(path)
|
||||||
|
|
||||||
if path != []:
|
if path != '':
|
||||||
print(path)
|
nextStep = path[0]
|
||||||
nextStep = path.pop(0)
|
path = path[1:]
|
||||||
print(nextStep)
|
|
||||||
waiter.travel(nextStep, graphics)
|
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()
|
pygame.display.flip()
|
||||||
clock.tick(fps)
|
clock.tick(fps)
|
||||||
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
BIN
resources/images/waiter_N.png
Normal file
BIN
resources/images/waiter_N.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
BIN
resources/images/waiter_S.png
Normal file
BIN
resources/images/waiter_S.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
BIN
resources/images/waiter_W.png
Normal file
BIN
resources/images/waiter_W.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
Binary file not shown.
Binary file not shown.
@ -5,32 +5,47 @@ class Graphics:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.image = {
|
self.image = {
|
||||||
'floor': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/floor.jpg'),
|
'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'), #
|
'wall': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/wall.png'),
|
||||||
'bar_floor': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter.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'),
|
'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'),
|
'waiter_N': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_N.png'),
|
||||||
'chair_front': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-front.png'), #
|
'waiter_S': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_S.png'),
|
||||||
'chair_back': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-back.png'), #
|
'waiter_E': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_E.png'),
|
||||||
'chair_left': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-left.png'), #
|
'waiter_W': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/waiter_W.png'),
|
||||||
'chair_right': pygame.image.load('/Users/marcindobrowolski/Desktop/Python/Sztuczna_Inteligencja_2020/resources/images/chair-right.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.block_size = 50
|
||||||
self.height = 15
|
self.height = 15
|
||||||
self.width: int = 14
|
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):
|
def drawBackground(self, matrix):
|
||||||
for y in range(self.height):
|
for y in range(self.height):
|
||||||
for x in range(self.width):
|
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 y in range(self.height):
|
||||||
for x in range(self.width):
|
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):
|
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):
|
def update(self, waiter):
|
||||||
self.screen.blit(self.image['waiter'], (x * self.block_size, y * self.block_size))
|
model = 'waiter_' + waiter.direction
|
||||||
|
self.screen.blit(
|
||||||
|
self.image[model], (waiter.X * self.block_size, waiter.Y * self.block_size))
|
||||||
|
@ -55,11 +55,9 @@ class Waiter(pygame.sprite.Sprite):
|
|||||||
# AStar
|
# AStar
|
||||||
def findPath(self, goal):
|
def findPath(self, goal):
|
||||||
|
|
||||||
# Stworzenie startowego i koncowego wierzcholka
|
|
||||||
startNode = self.matrix.matrix[self.X][self.Y]
|
startNode = self.matrix.matrix[self.X][self.Y]
|
||||||
goalNode = self.matrix.matrix[goal[0]][goal[1]]
|
goalNode = self.matrix.matrix[goal[0]][goal[1]]
|
||||||
|
|
||||||
# Inicjalizacja list
|
|
||||||
openList = []
|
openList = []
|
||||||
closedList = []
|
closedList = []
|
||||||
|
|
||||||
@ -72,7 +70,6 @@ class Waiter(pygame.sprite.Sprite):
|
|||||||
currentNode = openList.pop(0)
|
currentNode = openList.pop(0)
|
||||||
closedList.append(currentNode)
|
closedList.append(currentNode)
|
||||||
|
|
||||||
# Tutaj odbywac sie bedzie budowanie sciezki gdy algorytm osiagnie cel
|
|
||||||
if currentNode == goalNode:
|
if currentNode == goalNode:
|
||||||
path = []
|
path = []
|
||||||
current = currentNode
|
current = currentNode
|
||||||
@ -230,24 +227,21 @@ class Waiter(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def travel(self, goal, graphics):
|
def travel(self, nextStep, graphics):
|
||||||
translatedPath = self.translatePath([goal])
|
if nextStep == 'F':
|
||||||
print('{} - {} - {}'.format(self.direction, goal, translatedPath))
|
|
||||||
for character in translatedPath:
|
|
||||||
if character == 'F':
|
|
||||||
graphics.clear(self.X, self.Y)
|
graphics.clear(self.X, self.Y)
|
||||||
self.update('F', graphics)
|
self.update('F', graphics)
|
||||||
graphics.update(self.X, self.Y)
|
graphics.update(self)
|
||||||
|
|
||||||
if character == 'R':
|
if nextStep == 'R':
|
||||||
#graphics.clear(self.X, self.Y)
|
graphics.clear(self.X, self.Y)
|
||||||
self.update('R', graphics)
|
self.update('R', graphics)
|
||||||
#graphics.update(self.X, self.Y)
|
graphics.update(self)
|
||||||
|
|
||||||
if character == 'L':
|
if nextStep == 'L':
|
||||||
#graphics.clear(self.X, self.Y)
|
graphics.clear(self.X, self.Y)
|
||||||
self.update('L', graphics)
|
self.update('L', graphics)
|
||||||
#graphics.update(self.X, self.Y)
|
graphics.update(self)
|
||||||
|
|
||||||
|
|
||||||
def getTotalCost(tile):
|
def getTotalCost(tile):
|
||||||
|
Loading…
Reference in New Issue
Block a user