Prześlij pliki do ''

This commit is contained in:
Michał Ciesiółka 2022-04-05 18:36:43 +02:00
parent 5e58b25265
commit a0bfe9fcf7
4 changed files with 212 additions and 21 deletions

233
tiles.py
View File

@ -1,6 +1,9 @@
import time
import numpy as np import numpy as np
import pygame import pygame
import pytmx import pytmx
from queue import Queue
import math
pygame.init() pygame.init()
@ -8,6 +11,9 @@ display = pygame.display.set_mode((640, 640))
clock = pygame.time.Clock() clock = pygame.time.Clock()
gameMap = pytmx.load_pygame("cafe.tmx") gameMap = pytmx.load_pygame("cafe.tmx")
waiterImg = pygame.image.load("waiter.png") waiterImg = pygame.image.load("waiter.png")
waiterImgD = pygame.image.load("waiterD.png")
waiterImgL = pygame.image.load("waiterL.png")
waiterImgR = pygame.image.load("waiterR.png")
tableImg = pygame.image.load('table.png') tableImg = pygame.image.load('table.png')
chairImg = pygame.image.load('chair.png') chairImg = pygame.image.load('chair.png')
@ -38,6 +44,19 @@ class Waiter:
elif key[pygame.K_d] or key[pygame.K_RIGHT]: elif key[pygame.K_d] or key[pygame.K_RIGHT]:
self.loc[0] += 32 self.loc[0] += 32
def up(self):
self.loc[1] -= 32
def down(self):
self.loc[1] += 32
def left(self):
self.loc[0] -= 32
def right(self):
self.loc[0] += 32
class Table: class Table:
def __init__(self, loc, num): def __init__(self, loc, num):
@ -97,7 +116,159 @@ class Order(Table):
self.status = True self.status = True
waiter = Waiter([64, 320]) class Graph:
def __init__(self, num_of_nodes, directed=True):
self.m_num_of_nodes = num_of_nodes
self.m_nodes = range(self.m_num_of_nodes)
self.m_directed = directed
self.m_adj_list = {node: set() for node in self.m_nodes}
def add_edge(self, node1, node2, weight=1):
self.m_adj_list[node1].add((node2, weight))
if not self.m_directed:
self.m_adj_list[node2].add((node1, weight))
def print_adj_list(self):
for key in self.m_adj_list.keys():
print("node", key, ": ", self.m_adj_list[key])
def bfs(self, start_node, target_node):
visited = set()
queue = Queue()
queue.put(start_node)
visited.add(start_node)
parent = dict()
parent[start_node] = None
path_found = False
while not queue.empty():
current_node = queue.get()
if current_node == target_node:
path_found = True
break
for (next_node, weight) in self.m_adj_list[current_node]:
if next_node not in visited:
queue.put(next_node)
parent[next_node] = current_node
visited.add(next_node)
path = []
if path_found:
path.append(target_node)
while parent[target_node] is not None:
path.append(parent[target_node])
target_node = parent[target_node]
path.reverse()
return path
def pathFromTo(start, dest):
tab2 = [42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
tab4 = [41, 58]
tab3 = []
tab5 = []
for x in range(16):
for num in tab4:
tab5.append(num + x * 20)
for x in range(16):
for num in tab2:
tab3.append(num + x * 20)
graph = Graph(400, directed=False)
for x in tab5:
b = x + 1
c = x + 20
d = x - 20
graph.add_edge(x, b)
graph.add_edge(x, c)
graph.add_edge(x, d)
for x in tab3:
b = x + 1
c = x + 20
d = x - 1
e = x - 20
graph.add_edge(x, b)
graph.add_edge(x, c)
graph.add_edge(x, d)
graph.add_edge(x, e)
graph.add_edge(21, 22)
graph.add_edge(21, 41)
graph.add_edge(378, 358)
graph.add_edge(378, 377)
graph.add_edge(38, 37)
graph.add_edge(38, 58)
graph.add_edge(361, 362)
graph.add_edge(361, 341)
# graph.print_adj_list()
path = []
path = graph.bfs(start, dest)
return path
def numToX(num):
digits = [int(a) for a in str(num)]
if num >= 100:
if digits[1] % 2 == 0:
x = digits[2]
else:
x = digits[2] + 10
else:
if digits[0] % 2 == 0:
x = digits[1]
else:
x = digits[1] + 10
x = x * 32
return x
def numToY(num):
y = (math.floor(num/20)) * 32
return y
def coordsToNum(coords):
num = ((coords[1]//32) * 20) + (coords[0]//32)
return int(num)
def waiterGo(dest):
print("Path: ", (pathFromTo(coordsToNum(waiter.loc), dest)))
go = []
for numb in range(len(pathFromTo(coordsToNum(waiter.loc), dest)) - 1):
go.append(pathFromTo(coordsToNum(waiter.loc), dest)[numb] - pathFromTo(coordsToNum(waiter.loc), dest)[numb + 1])
for x in range(len(go)):
if go[x] == -20:
waiter.down()
display.blit(waiterImgD, (waiter.loc[0], waiter.loc[1]))
#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)
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)
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)
pygame.display.update()
time.sleep(0.2)
def mouseToNum():
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
cordTab = [x, y]
squareNum = coordsToNum(cordTab)
return squareNum
waiter = Waiter([32, 32])
tables = [] tables = []
chairs = [] chairs = []
tables_coordinates = ((32 * 3, 32 * 2), (32 * 7, 32 * 2), (32 * 12, 32 * 2), (32 * 16, 32 * 2), tables_coordinates = ((32 * 3, 32 * 2), (32 * 7, 32 * 2), (32 * 12, 32 * 2), (32 * 16, 32 * 2),
@ -111,31 +282,51 @@ for i in range(16):
arr.append(Chair(pos_of_chair(tables_coordinates[i], j))) arr.append(Chair(pos_of_chair(tables_coordinates[i], j)))
chairs.append(arr) chairs.append(arr)
while True:
clock.tick(10)
keys = pygame.key.get_pressed()
for event in pygame.event.get(): def main():
if event.type == pygame.QUIT:
while True:
clock.tick(10)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if keys[pygame.K_ESCAPE]:
quit() quit()
if keys[pygame.K_ESCAPE]: for layer in gameMap.visible_layers:
quit() for x, y, gid, in layer:
tile = gameMap.get_tile_image_by_gid(gid)
if tile is not None:
display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))
for layer in gameMap.visible_layers: waiter.handle_events()
for x, y, gid, in layer:
tile = gameMap.get_tile_image_by_gid(gid)
if tile is not None:
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)
for table in tables: key = pygame.key.get_pressed()
table.render(display) left, middle, right = pygame.mouse.get_pressed()
for chair_list in chairs:
for chair in chair_list: if left:
chair.render(display) waiterGo(mouseToNum())
key = pygame.key.get_pressed()
if key[pygame.K_p]:
pass
waiter.render(display)
pygame.display.update()
if __name__ == '__main__':
main()
waiter.render(display)
pygame.display.update()

BIN
waiterD.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
waiterL.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
waiterR.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB