2020-03-23 10:49:27 +01:00
|
|
|
import pygame
|
2020-04-05 13:50:07 +02:00
|
|
|
import random
|
|
|
|
import time
|
2020-04-05 16:53:59 +02:00
|
|
|
import queue
|
2020-03-23 10:49:27 +01:00
|
|
|
|
2020-04-05 13:50:07 +02:00
|
|
|
pygame.init()
|
2020-03-23 10:49:27 +01:00
|
|
|
|
2020-04-05 16:53:59 +02:00
|
|
|
|
2020-04-05 14:04:55 +02:00
|
|
|
class Dish(object):
|
2020-04-05 18:50:57 +02:00
|
|
|
def __init__(self, dishName, prepTime, eatTime, price):
|
2020-04-05 14:04:55 +02:00
|
|
|
self.dishName = dishName
|
|
|
|
self.preparationTime = prepTime
|
2020-04-05 16:53:59 +02:00
|
|
|
self.eatingTime = eatTime
|
2020-04-05 18:50:57 +02:00
|
|
|
self.price = price
|
2020-04-05 16:53:59 +02:00
|
|
|
|
2020-04-05 14:04:55 +02:00
|
|
|
|
|
|
|
class Plate(object):
|
|
|
|
def __init__(self, dish):
|
|
|
|
self.dishName = dish.dishName
|
|
|
|
self.isEmpty = False
|
|
|
|
self.preparationTime = dish.preparationTime
|
2020-04-05 16:53:59 +02:00
|
|
|
self.eatingTime = dish.eatingTime
|
2020-04-05 14:04:55 +02:00
|
|
|
|
|
|
|
def eat(self):
|
2020-04-05 16:53:59 +02:00
|
|
|
time.sleep(self.eatingTime)
|
2020-04-05 14:04:55 +02:00
|
|
|
self.isEmpty = True #zjadanie dania jest oznaczeniem talerza jako pusty (metoda będzie wywoływana przez klienta)
|
|
|
|
|
2020-04-05 16:53:59 +02:00
|
|
|
|
|
|
|
class Kitchen(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.readyDishes = queue.queue(32) #kolejka o maksymalnej długości 32
|
|
|
|
self.orders = queue.queue(32)
|
|
|
|
|
|
|
|
def makeDish(self):
|
|
|
|
if not self.orders.empty():
|
|
|
|
plate = self.orders.get()
|
|
|
|
time.sleep(plate.preparationTime) #kuchnia przygotowuje danie przez określony czas
|
|
|
|
self.readyDishes.put(plate)
|
|
|
|
|
|
|
|
|
|
|
|
class Client(object):
|
|
|
|
def __init__(self, age, sex, budget):
|
|
|
|
self.age = age
|
|
|
|
self.sex = sex
|
|
|
|
self.myPlate = None
|
|
|
|
self.budget = budget
|
2020-04-05 18:50:57 +02:00
|
|
|
self.myTable = 0 #domyślnie klient nie siedzi przy żadnym stole
|
2020-04-05 16:53:59 +02:00
|
|
|
|
|
|
|
def takePlateAndEat(self, plate):
|
|
|
|
self.myPlate = plate
|
|
|
|
plate.eat()
|
|
|
|
|
2020-04-05 18:50:57 +02:00
|
|
|
def takeASeat(self, table):
|
|
|
|
self.myTable = table
|
2020-04-05 16:53:59 +02:00
|
|
|
|
2020-03-23 10:49:27 +01:00
|
|
|
class Table(object):
|
2020-04-05 18:50:57 +02:00
|
|
|
def __init__(self, pos, capacity):
|
2020-03-23 10:49:27 +01:00
|
|
|
self.pos = pos
|
2020-04-05 18:50:57 +02:00
|
|
|
self.capacity = capacity
|
2020-03-23 10:49:27 +01:00
|
|
|
|
2020-04-05 14:04:55 +02:00
|
|
|
def move(self, newx, newy): #metoda do ustawiania stołów poprzez podanie nowych współrzędnych
|
2020-03-23 10:49:27 +01:00
|
|
|
self.pos[0] = newx
|
|
|
|
self.pos[1] = newy
|
|
|
|
|
|
|
|
def draw(self, surface):
|
|
|
|
image = pygame.image.load(r'Table-croped.png')
|
|
|
|
image = pygame.transform.scale(image, (sizeBetween - 1, sizeBetween - 1))
|
|
|
|
|
|
|
|
i = self.pos[0]
|
|
|
|
j = self.pos[1]
|
|
|
|
|
|
|
|
surface.blit(image, (i*sizeBetween+1, j*sizeBetween+1))
|
|
|
|
|
|
|
|
|
|
|
|
class Waiter(object):
|
|
|
|
def __init__(self, color, pos):
|
|
|
|
self.color = color
|
2020-04-05 13:50:07 +02:00
|
|
|
self.pos = pos #pozycja agenta, zapisana w formie dwuelementowej listy
|
|
|
|
self.dirnx = 0 #zmienne dirnx i dirny używane są do ruchu bota i ustalania, w którą stronę jest zwrócony
|
2020-03-23 10:49:27 +01:00
|
|
|
self.dirny = 1
|
2020-04-05 16:57:20 +02:00
|
|
|
self.plates = [] #lista niesionych przez agenta talerzy, planowo lista par: (talerz, klient)
|
2020-04-05 13:50:07 +02:00
|
|
|
|
|
|
|
def moveRandomly(self):
|
|
|
|
rand = random.randrange(1, 5, 1) #losuje w zakresie 1-4
|
|
|
|
print(rand)
|
|
|
|
|
|
|
|
if rand == 1:
|
|
|
|
self.dirnx = -1
|
|
|
|
self.dirny = 0
|
|
|
|
if self.pos[0] == 0: #zabezpieczenie przed wyjściem bota poza obszar okna w ruchu losowym
|
|
|
|
self.dirnx *= (-1)
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
elif rand == 2:
|
|
|
|
self.dirnx = 1
|
|
|
|
self.dirny = 0
|
2020-04-05 16:53:59 +02:00
|
|
|
if self.pos[0] == 14:
|
2020-04-05 13:50:07 +02:00
|
|
|
self.dirnx *= (-1)
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
elif rand == 3:
|
|
|
|
self.dirnx = 0
|
|
|
|
self.dirny = -1
|
|
|
|
if self.pos[1] == 0:
|
|
|
|
self.dirny *= (-1)
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
elif rand == 4:
|
|
|
|
self.dirnx = 0
|
|
|
|
self.dirny = 1
|
2020-04-05 16:53:59 +02:00
|
|
|
if self.pos[1] == 14:
|
2020-04-05 13:50:07 +02:00
|
|
|
self.dirny *= (-1)
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
|
|
|
|
def moveWithKeyboard(self): #funkcja testowa - bot sterowany z klawiatury
|
2020-03-23 10:49:27 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
quit()
|
|
|
|
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
|
|
|
|
for key in keys:
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
|
|
self.dirnx = -1
|
|
|
|
self.dirny = 0
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
break
|
|
|
|
# te break musiałam dodać, bo w przeciwnym wypadku zamiast jednego kroku robił 303 - jeden za drugim
|
|
|
|
# nie wiem dlaczego tak było, po zmianie sterowania z klawiatury na bota samego w sobie nie powinno
|
|
|
|
# być z tym problemów, to jest na razie tylko, żeby pokazać, że ten ruch jest
|
|
|
|
|
|
|
|
elif keys[pygame.K_RIGHT]:
|
|
|
|
self.dirnx = 1
|
|
|
|
self.dirny = 0
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
break
|
|
|
|
|
|
|
|
elif keys[pygame.K_UP]:
|
|
|
|
self.dirnx = 0
|
|
|
|
self.dirny = -1
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
break
|
|
|
|
|
|
|
|
elif keys[pygame.K_DOWN]:
|
|
|
|
self.dirnx = 0
|
|
|
|
self.dirny = 1
|
|
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
break
|
|
|
|
|
2020-04-05 16:53:59 +02:00
|
|
|
def resetPosition(self, pos):
|
2020-03-23 10:49:27 +01:00
|
|
|
self.pos = pos
|
|
|
|
|
|
|
|
def draw(self, surface):
|
|
|
|
i = self.pos[0]
|
|
|
|
j = self.pos[1]
|
|
|
|
|
|
|
|
pygame.draw.rect(surface, self.color, (i*sizeBetween+1, j*sizeBetween+1, sizeBetween-1, sizeBetween-1))
|
|
|
|
|
|
|
|
|
|
|
|
def drawGrid(width, rows, surface):
|
|
|
|
x = 0
|
|
|
|
y = 0
|
|
|
|
|
|
|
|
for i in range(rows):
|
|
|
|
x = x + sizeBetween
|
|
|
|
y = y + sizeBetween
|
|
|
|
|
|
|
|
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, width))
|
|
|
|
pygame.draw.line(surface, (255, 255, 255), (0, y), (width, y))
|
|
|
|
|
|
|
|
|
|
|
|
def redrawWindow(surface):
|
|
|
|
surface.fill(beige)
|
|
|
|
bot.draw(surface)
|
|
|
|
table1.draw(surface)
|
|
|
|
drawGrid(width, rows, surface)
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global width, rows, bot, beige, white, black, sizeBetween, table1
|
|
|
|
# skróty do kolorów
|
|
|
|
beige = (255, 205, 178)
|
|
|
|
white = (255, 255, 255)
|
|
|
|
black = (0, 0, 0)
|
|
|
|
|
|
|
|
width = 600
|
|
|
|
rows = 15
|
2020-04-05 13:50:07 +02:00
|
|
|
sizeBetween = width // rows #wielkość pojedynczej kratki
|
2020-03-23 10:49:27 +01:00
|
|
|
window = pygame.display.set_mode((width, width))
|
|
|
|
bot = Waiter((255, 0, 0), [7, 7])
|
|
|
|
table1 = Table([5, 5])
|
|
|
|
flag = True
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
|
|
|
while flag:
|
2020-04-05 13:50:07 +02:00
|
|
|
pygame.time.delay(100)
|
|
|
|
clock.tick(60)
|
|
|
|
for i in range(40): #bot testowo ma wykonać 40 kroków
|
|
|
|
bot.moveRandomly()
|
|
|
|
redrawWindow(window)
|
|
|
|
time.sleep(0.5) #opóźnienie każdego kolejnego kroku o pół sekundy
|
|
|
|
flag = False
|
2020-03-23 10:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
main()
|