Projekt-Sztuczna-Inteligencja/kelner/main.py

81 lines
3.0 KiB
Python

import pygame
from src.components.GridBoard import GridBoard
from src.managers.DrawableCollection import DrawableCollection
from src.managers.MenuManager import MenuManager
from src.components.Waiter import Waiter
from src.components.Table import Table
from src.managers.TaskManager import TaskManager
#create screen consts
CellSize = 100 #pixel size of 1 square cell in the grid
GridCountX = 15 #number of columns in grid
GridCountY = 9 #number of rows in grid
ScreenWidth = CellSize * GridCountX #screen width in pixels
ScreenHeight = CellSize * GridCountY #screen height in pixels
Offset = 50
#initialize background
gridBoard = GridBoard(ScreenWidth+200, ScreenHeight+200, CellSize)
#initialize drawable objects manager
drawableManager = DrawableCollection()
#initialize menu manager
menuManager = MenuManager()
#initialize waiter component
waiter = Waiter(0, 0, 0, GridCountX - 1, 0, GridCountY - 1, CellSize)
#adds waiter to drawable collection
drawableManager.add(waiter)
#initialize a number of tables given in range
for i in range(1, 20):
table = Table(0, GridCountX - 1, 0, GridCountY - 1, CellSize)
drawableManager.generatePosition(table)
drawableManager.add(table)
#main loop
#object that controlls repainting of changed objects
doRepaint = [True]
#new thread
task = TaskManager(drawableManager, menuManager, doRepaint)
task.start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#handles keyboard events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
#checks if new waiter's position to the left is not occupied by other object
if drawableManager.isPositionAvailable(waiter.getX() - 1, waiter.getY()):
waiter.moveLeft()
if event.key == pygame.K_RIGHT:
# checks if new waiter's position to the right is not occupied by other object
if drawableManager.isPositionAvailable(waiter.getX() + 1, waiter.getY()):
waiter.moveRight()
if event.key == pygame.K_UP:
# checks if new waiter's position up is not occupied by other object
if drawableManager.isPositionAvailable(waiter.getX(), waiter.getY() - 1):
waiter.moveUp()
if event.key == pygame.K_DOWN:
# checks if new waiter's position down is not occupied by other object
if drawableManager.isPositionAvailable(waiter.getX(), waiter.getY() + 1):
waiter.moveDown()
doRepaint[0] = True
# repaints all objects to the screen
# is set only on initial paint or after keyboard event
if doRepaint[0]:
gridBoard.reinitialize()
gridBoard.draw(drawableManager)
gridBoard.udpdate()
doRepaint[0] = False
drawableManager.collectOrders()