Dodanie A*

This commit is contained in:
Łukasz Kinder 2022-04-24 15:33:59 +02:00
parent a0bfe9fcf7
commit 3188f3e7f6

122
tiles.py
View File

@ -57,7 +57,6 @@ class Waiter:
self.loc[0] += 32 self.loc[0] += 32
class Table: class Table:
def __init__(self, loc, num): def __init__(self, loc, num):
self.loc = loc self.loc = loc
@ -219,12 +218,12 @@ def numToX(num):
def numToY(num): def numToY(num):
y = (math.floor(num/20)) * 32 y = (math.floor(num / 20)) * 32
return y return y
def coordsToNum(coords): def coordsToNum(coords):
num = ((coords[1]//32) * 20) + (coords[0]//32) num = ((coords[1] // 32) * 20) + (coords[0] // 32)
return int(num) return int(num)
@ -237,29 +236,30 @@ def waiterGo(dest):
if go[x] == -20: if go[x] == -20:
waiter.down() waiter.down()
display.blit(waiterImgD, (waiter.loc[0], waiter.loc[1])) display.blit(waiterImgD, (waiter.loc[0], waiter.loc[1]))
#waiter.render(display) # waiter.render(display)
pygame.display.update() pygame.display.update()
time.sleep(0.2) time.sleep(0.2)
elif go[x] == 20: elif go[x] == 20:
waiter.up() waiter.up()
display.blit(waiterImg, (waiter.loc[0], waiter.loc[1])) display.blit(waiterImg, (waiter.loc[0], waiter.loc[1]))
#waiter.render(display) # waiter.render(display)
pygame.display.update() pygame.display.update()
time.sleep(0.2) time.sleep(0.2)
elif go[x] == 1: elif go[x] == 1:
waiter.left() waiter.left()
display.blit(waiterImgL, (waiter.loc[0], waiter.loc[1])) display.blit(waiterImgL, (waiter.loc[0], waiter.loc[1]))
#waiter.render(display) # waiter.render(display)
pygame.display.update() pygame.display.update()
time.sleep(0.2) time.sleep(0.2)
elif go[x] == -1: elif go[x] == -1:
waiter.right() waiter.right()
display.blit(waiterImgR, (waiter.loc[0], waiter.loc[1])) display.blit(waiterImgR, (waiter.loc[0], waiter.loc[1]))
#waiter.render(display) # waiter.render(display)
pygame.display.update() pygame.display.update()
time.sleep(0.2) time.sleep(0.2)
def mouseToNum(): def mouseToNum():
x = pygame.mouse.get_pos()[0] x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1] y = pygame.mouse.get_pos()[1]
@ -268,6 +268,86 @@ def mouseToNum():
return squareNum return squareNum
class Map:
def __init__(self):
self.arr = np.zeros((20, 20))
self.arr[1:19, 1:19] = 1
def add_chair(self, loc):
self.arr[loc[0] // 32, loc[1] // 32] = 100
def add_table(self, loc):
self.arr[loc[0] // 32, loc[1] // 32] = 200
def get_arr(self):
return self.arr.transpose()
class Tile():
def __init__(self, parent=None, loc=None):
self.parent = parent
self.position = loc
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
def astar(map, start, end):
start_tile = Tile(None, start)
start_tile.g = start_tile.h = start_tile.f = 0
end_tile = Tile(None, end)
end_tile.g = end_tile.h = end_tile.f = 0
open_list = []
closed_list = []
open_list.append(start_tile)
while len(open_list) > 0:
current_tile = open_list[0]
current_index = 0
for index, item in enumerate(open_list):
if item.f < current_tile.f:
current_tile = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_tile)
if current_tile == end_tile:
path = []
current = current_tile
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1]
children = []
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
tile_position = (current_tile.position[0] + new_position[0], current_tile.position[1] + new_position[1])
if map[tile_position[0]][tile_position[1]] == 0:
continue
new_node = Tile(current_tile, tile_position)
children.append(new_node)
for child in children:
for closed_child in closed_list:
if child == closed_child:
continue
child.g = current_tile.g + map[child.position[0]][child.position[1]]
child.h = ((child.position[0] - end_tile.position[0]) ** 2) + (
(child.position[1] - end_tile.position[1]) ** 2)
child.f = child.g + child.h
for open_node in open_list:
if child == open_node and child.g > open_node.g:
continue
open_list.append(child)
map = Map()
waiter = Waiter([32, 32]) waiter = Waiter([32, 32])
tables = [] tables = []
chairs = [] chairs = []
@ -280,12 +360,14 @@ for i in range(16):
arr = [] arr = []
for j in range(0, tables[i].get_number_of_chairs()): for j in range(0, tables[i].get_number_of_chairs()):
arr.append(Chair(pos_of_chair(tables_coordinates[i], j))) arr.append(Chair(pos_of_chair(tables_coordinates[i], j)))
map.add_chair(Chair(pos_of_chair(tables_coordinates[i], j)).loc)
chairs.append(arr) chairs.append(arr)
for table in tables:
map.add_table(table.loc)
def main(): def main():
direction = []
while True: while True:
clock.tick(10) clock.tick(10)
@ -294,7 +376,6 @@ def main():
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
quit() quit()
if keys[pygame.K_ESCAPE]: if keys[pygame.K_ESCAPE]:
quit() quit()
@ -305,28 +386,37 @@ def main():
display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight)) display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))
waiter.handle_events() waiter.handle_events()
for table in tables: for table in tables:
table.render(display) table.render(display)
for chair_list in chairs: for chair_list in chairs:
for chair in chair_list: for chair in chair_list:
chair.render(display) chair.render(display)
key = pygame.key.get_pressed() key = pygame.key.get_pressed()
left, middle, right = pygame.mouse.get_pressed() left, middle, right = pygame.mouse.get_pressed()
if left: if left:
waiterGo(mouseToNum()) waiterGo(mouseToNum())
elif right:
goal = (18, 18)
route = astar(map.get_arr(), (waiter.loc[1] // 32, waiter.loc[0] // 32), goal)
direction = [(x[1] - y[1], x[0] - y[0]) for x, y in zip(route[1:], route)]
if len(direction) > 0:
d = direction.pop(0)
if d[0] == 1:
waiter.right()
elif d[0] == -1:
waiter.left()
elif d[1] == 1:
waiter.down()
elif d[1] == -1:
waiter.up()
key = pygame.key.get_pressed() key = pygame.key.get_pressed()
if key[pygame.K_p]: if key[pygame.K_p]:
pass pass
waiter.render(display) waiter.render(display)
pygame.display.update() pygame.display.update()
if __name__ == '__main__': if __name__ == '__main__':
main() main()