216 lines
5.9 KiB
Python
216 lines
5.9 KiB
Python
import pygame
|
|
import random
|
|
import time
|
|
import numpy
|
|
import threading
|
|
import collections
|
|
from collections import deque
|
|
|
|
kuchnia_xy = 0
|
|
pozycja_startowa = 0
|
|
|
|
# ------------Ustawienia siatki
|
|
blockSize = 60
|
|
rows = 14
|
|
columns = 24
|
|
|
|
|
|
# -----------------------------Inicjacja klas
|
|
class Stan:
|
|
def __init__(self,position, kierunek):
|
|
self.position = position
|
|
self.kierunek = kierunek
|
|
|
|
toVisit = deque()
|
|
visited = deque()
|
|
|
|
def succ(self):
|
|
newStan: Stan(x+1, y, 1)
|
|
queue = deque([x, y, self.kierunek])
|
|
print(queue[0])
|
|
|
|
#def BFS:
|
|
|
|
|
|
class Kelner:
|
|
def __init__(self, x, y):
|
|
self.x = x
|
|
self.y = y
|
|
self.speed = 70 # od 0 do 100, preferowane 70
|
|
self.stan = "stoi" # Stan kelnera: stoi, odbiera lub wraca
|
|
self.stolik_docelowy = None # Stolik, do którego idzie kelner
|
|
self.chodzi = True
|
|
self.cel_x = x
|
|
self.cel_y = y
|
|
self.kierunek = 0 # 0 - północ, 1 - wschód, 2 - południe, 3 - zachód
|
|
|
|
def wklej(self):
|
|
kelnerRotated = pygame.transform.rotate(kelnerImg, -90 * kelner.kierunek)
|
|
screen.blit(kelnerRotated, (self.x * blockSize, self.y * blockSize))
|
|
|
|
#def planuj_ruch(self):
|
|
|
|
def idz_do_stolika(self):
|
|
self.cel_x, self.cel_y = self.stolik_docelowy.x, self.stolik_docelowy.y
|
|
kelner.stan = "odbiera"
|
|
|
|
def idz_do_kuchni(self):
|
|
self.cel_x, self.cel_y = kuchnia_xy, kuchnia_xy
|
|
self.stolik_docelowy = None
|
|
kelner.stan = "wraca"
|
|
|
|
def obrot_w_lewo(self):
|
|
if self.kierunek == 0:
|
|
self.kierunek = 3
|
|
else:
|
|
self.kierunek -= 1
|
|
|
|
def obrot_w_prawo(self):
|
|
if self.kierunek == 3:
|
|
self.kierunek = 0
|
|
else:
|
|
self.kierunek += 1
|
|
|
|
def idz_do_przodu(self):
|
|
if self.kierunek == 0:
|
|
self.y -= 1
|
|
elif self.kierunek == 1:
|
|
self.x += 1
|
|
elif self.kierunek == 2:
|
|
self.y += 1
|
|
elif self.kierunek == 3:
|
|
self.x -= 1
|
|
|
|
|
|
class Stolik:
|
|
def __init__(self, x, y):
|
|
self.x = x
|
|
self.y = y
|
|
self.zamowione = False
|
|
|
|
def wklej(self):
|
|
screen.blit(stolikImg, (self.x * blockSize, self.y * blockSize))
|
|
|
|
|
|
# --------------Inicjacja obiektów
|
|
kelner = Kelner(pozycja_startowa, pozycja_startowa)
|
|
|
|
# -----------wspolrzedne stolikow
|
|
coords = ["8 8", "4 12", "16 8", "12 12", "20 12"]
|
|
|
|
# Tworzenie listy stolikow
|
|
stoliki = []
|
|
for coord in coords:
|
|
x, y = map(int, coord.split())
|
|
stoliki.append(Stolik(x, y))
|
|
|
|
pygame.init()
|
|
pygame.display.set_caption("Automatyczny kelner")
|
|
|
|
# ----------------wymiary okna
|
|
width = columns * blockSize
|
|
height = rows * blockSize
|
|
|
|
screen = pygame.display.set_mode((width, height))
|
|
|
|
kelnerImg = pygame.image.load("kelner.png")
|
|
kelnerImg = pygame.transform.scale(kelnerImg, (blockSize, blockSize))
|
|
stolikImg = pygame.image.load("stolik.png")
|
|
stolikImg = pygame.transform.scale(stolikImg, (blockSize, blockSize))
|
|
menuImg = pygame.image.load("menu.png")
|
|
menuImg = pygame.transform.scale(menuImg, (blockSize / 2, blockSize / 2))
|
|
kitchenImg = pygame.image.load("kitchen.png")
|
|
kitchenImg = pygame.transform.scale(kitchenImg, (blockSize * 2, blockSize * 2))
|
|
|
|
|
|
def kuchnia(x, y):
|
|
screen.blit(kitchenImg, (x * blockSize, y * blockSize))
|
|
|
|
|
|
def menu(x, y):
|
|
screen.blit(menuImg, (x * blockSize, y * blockSize))
|
|
|
|
|
|
def wypiszOkno():
|
|
screen.fill((0, 0, 0))
|
|
for x in range(0, width, blockSize):
|
|
for y in range(0, height, blockSize):
|
|
rect = pygame.Rect(x, y, blockSize, blockSize)
|
|
pygame.draw.rect(screen, (200, 200, 200), rect, 1) # -------------Wypisz kratę -TA
|
|
|
|
|
|
run = True
|
|
|
|
# czcionka = pygame.font.SysFont('Arial',50)
|
|
|
|
licznik = 0
|
|
|
|
while run:
|
|
wypiszOkno()
|
|
kuchnia(kuchnia_xy, kuchnia_xy)
|
|
|
|
for stolik in stoliki:
|
|
stolik.wklej()
|
|
|
|
kelner.wklej()
|
|
|
|
if kelner.stan == "wraca":
|
|
menu(kelner.x, kelner.y)
|
|
|
|
licznik += 1
|
|
|
|
# ------------weź zamowienie
|
|
for stolik in stoliki:
|
|
if stolik.zamowione == True:
|
|
menu(stolik.x, stolik.y)
|
|
if kelner.stan == "stoi":
|
|
kelner.stolik_docelowy = stolik
|
|
#kelner.planuj_ruch()
|
|
kelner.idz_do_stolika()
|
|
|
|
# ----------Losuje stoliki, które dokonają zamówienia
|
|
if kelner.stan == "stoi":
|
|
for stolik in stoliki:
|
|
if stolik.zamowione == True:
|
|
break
|
|
for i in range(len(stoliki)):
|
|
if random.randrange(2) == 1:
|
|
stoliki[i].zamowione = True
|
|
|
|
# print(kelner.stan)--------------------------Wypisuje stan kelnera
|
|
# print(f"{kelner.x} {kelner.y}")-------------Wypisuje wspolrzedne kelnera
|
|
|
|
# --------------Zmiana kierunku kelnera
|
|
if kelner.x != kelner.cel_x:
|
|
if kelner.x < kelner.cel_x:
|
|
kelner.kierunek = 1
|
|
else:
|
|
kelner.kierunek = 3
|
|
else:
|
|
if kelner.y < kelner.cel_y: # zmieniamy pozycje Y dopiero, gdy pozycja X bedzie prawidlowa
|
|
kelner.kierunek = 2
|
|
else:
|
|
kelner.kierunek = 0
|
|
|
|
# ----------------Zmiana pozycji kelnera
|
|
if kelner.chodzi == True and licznik % (101 - kelner.speed) == 0: # ograniczenie prędkości
|
|
kelner.idz_do_przodu()
|
|
|
|
# ----------------Sprawdzenie, czy kelner jest u celu
|
|
if kelner.x == kelner.cel_x and kelner.y == kelner.cel_y:
|
|
if kelner.stan == "odbiera":
|
|
kelner.stolik_docelowy.zamowione = False
|
|
kelner.idz_do_kuchni()
|
|
|
|
elif kelner.stan == "wraca":
|
|
kelner.stan = "stoi"
|
|
|
|
time.sleep(0.001)
|
|
|
|
key = pygame.key.get_pressed()
|
|
pygame.display.update()
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
run = False
|
|
|
|
pygame.quit() |