Dodanie A*
This commit is contained in:
parent
a0bfe9fcf7
commit
3188f3e7f6
122
tiles.py
122
tiles.py
@ -57,7 +57,6 @@ class Waiter:
|
||||
self.loc[0] += 32
|
||||
|
||||
|
||||
|
||||
class Table:
|
||||
def __init__(self, loc, num):
|
||||
self.loc = loc
|
||||
@ -219,12 +218,12 @@ def numToX(num):
|
||||
|
||||
|
||||
def numToY(num):
|
||||
y = (math.floor(num/20)) * 32
|
||||
y = (math.floor(num / 20)) * 32
|
||||
return y
|
||||
|
||||
|
||||
def coordsToNum(coords):
|
||||
num = ((coords[1]//32) * 20) + (coords[0]//32)
|
||||
num = ((coords[1] // 32) * 20) + (coords[0] // 32)
|
||||
return int(num)
|
||||
|
||||
|
||||
@ -237,29 +236,30 @@ def waiterGo(dest):
|
||||
if go[x] == -20:
|
||||
waiter.down()
|
||||
display.blit(waiterImgD, (waiter.loc[0], waiter.loc[1]))
|
||||
#waiter.render(display)
|
||||
# waiter.render(display)
|
||||
pygame.display.update()
|
||||
|
||||
time.sleep(0.2)
|
||||
elif go[x] == 20:
|
||||
waiter.up()
|
||||
display.blit(waiterImg, (waiter.loc[0], waiter.loc[1]))
|
||||
#waiter.render(display)
|
||||
# waiter.render(display)
|
||||
pygame.display.update()
|
||||
time.sleep(0.2)
|
||||
elif go[x] == 1:
|
||||
waiter.left()
|
||||
display.blit(waiterImgL, (waiter.loc[0], waiter.loc[1]))
|
||||
#waiter.render(display)
|
||||
# waiter.render(display)
|
||||
pygame.display.update()
|
||||
time.sleep(0.2)
|
||||
elif go[x] == -1:
|
||||
waiter.right()
|
||||
display.blit(waiterImgR, (waiter.loc[0], waiter.loc[1]))
|
||||
#waiter.render(display)
|
||||
# waiter.render(display)
|
||||
pygame.display.update()
|
||||
time.sleep(0.2)
|
||||
|
||||
|
||||
def mouseToNum():
|
||||
x = pygame.mouse.get_pos()[0]
|
||||
y = pygame.mouse.get_pos()[1]
|
||||
@ -268,6 +268,86 @@ def mouseToNum():
|
||||
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])
|
||||
tables = []
|
||||
chairs = []
|
||||
@ -280,12 +360,14 @@ for i in range(16):
|
||||
arr = []
|
||||
for j in range(0, tables[i].get_number_of_chairs()):
|
||||
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)
|
||||
|
||||
for table in tables:
|
||||
map.add_table(table.loc)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
direction = []
|
||||
while True:
|
||||
|
||||
clock.tick(10)
|
||||
@ -294,7 +376,6 @@ def main():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
quit()
|
||||
|
||||
if keys[pygame.K_ESCAPE]:
|
||||
quit()
|
||||
|
||||
@ -305,28 +386,37 @@ def main():
|
||||
display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))
|
||||
|
||||
waiter.handle_events()
|
||||
|
||||
for table in tables:
|
||||
table.render(display)
|
||||
for chair_list in chairs:
|
||||
for chair in chair_list:
|
||||
chair.render(display)
|
||||
|
||||
key = pygame.key.get_pressed()
|
||||
left, middle, right = pygame.mouse.get_pressed()
|
||||
|
||||
if left:
|
||||
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()
|
||||
if key[pygame.K_p]:
|
||||
pass
|
||||
|
||||
waiter.render(display)
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user