225 lines
6.9 KiB
Python
225 lines
6.9 KiB
Python
import pygame
|
|
import random
|
|
import time
|
|
import queue
|
|
|
|
pygame.init()
|
|
|
|
|
|
class Dish(object):
|
|
def __init__(self, dishName, prepTime, eatTime, price):
|
|
self.dishName = dishName
|
|
self.preparationTime = prepTime
|
|
self.eatingTime = eatTime
|
|
self.price = price
|
|
|
|
|
|
class Plate(object):
|
|
def __init__(self, dish):
|
|
self.dishName = dish.dishName
|
|
self.isEmpty = False
|
|
self.preparationTime = dish.preparationTime
|
|
self.eatingTime = dish.eatingTime
|
|
self.price = dish.price
|
|
|
|
def eat(self):
|
|
time.sleep(self.eatingTime)
|
|
self.isEmpty = True #zjadanie dania jest oznaczeniem talerza jako pusty (metoda będzie wywoływana przez klienta)
|
|
|
|
|
|
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)
|
|
|
|
def giveDish(self):
|
|
if not self.readyDishes.empty():
|
|
plate = self.readyDishes.get()
|
|
return plate
|
|
else:
|
|
return None
|
|
|
|
|
|
class Client(object):
|
|
def __init__(self, age, sex, budget):
|
|
self.age = age
|
|
self.sex = sex
|
|
self.myPlate = None
|
|
self.budget = budget
|
|
self.myTable = 0 #domyślnie klient nie siedzi przy żadnym stole
|
|
|
|
def takePlateAndEat(self, plate):
|
|
self.myPlate = plate
|
|
plate.eat()
|
|
|
|
def takeASeat(self, table):
|
|
self.myTable = table
|
|
|
|
class Table(object):
|
|
def __init__(self, pos, capacity):
|
|
self.pos = pos
|
|
self.capacity = capacity
|
|
|
|
def move(self, newx, newy): #metoda do ustawiania stołów poprzez podanie nowych współrzędnych
|
|
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
|
|
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
|
|
self.dirny = 1
|
|
self.plates = [] #lista niesionych przez agenta talerzy, planowo lista par: (talerz, klient)
|
|
|
|
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
|
|
if self.pos[0] == 14:
|
|
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
|
|
if self.pos[1] == 14:
|
|
self.dirny *= (-1)
|
|
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
|
|
|
|
def moveWithKeyboard(self): #funkcja testowa - bot sterowany z klawiatury
|
|
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
|
|
|
|
def resetPosition(self, pos):
|
|
self.pos = pos
|
|
|
|
def takePlates(self, kitchen):
|
|
for i in range(2):
|
|
plate = kitchen.giveDish()
|
|
if plate == None:
|
|
break
|
|
else:
|
|
self.plates.append(plate)
|
|
|
|
def giveClientPlate(self, client):
|
|
plate = self.plates.pop(0)
|
|
client.takePlateAndEat(plate)
|
|
|
|
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
|
|
sizeBetween = width // rows #wielkość pojedynczej kratki
|
|
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:
|
|
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
|
|
|
|
|
|
main()
|