2020-03-22 02:01:57 +01:00
|
|
|
import pygame
|
2020-04-20 23:19:13 +02:00
|
|
|
from kelner.src.components.GridBoard import GridBoard
|
|
|
|
from kelner.src.components.Waiter import Waiter
|
|
|
|
from kelner.src.components.Table import Table
|
|
|
|
from kelner.src.managers.DrawableCollection import DrawableCollection
|
|
|
|
from kelner.src.managers.MenuManager import MenuManager
|
|
|
|
from kelner.src.managers.TableManager import TableManager
|
|
|
|
from kelner.src.managers.WaiterManager import WaiterManager
|
2020-03-22 02:01:57 +01:00
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# create screen consts
|
|
|
|
Scale = 2 # scale for all images used within project
|
|
|
|
CellSize = round(50 * Scale) # pixel size of 1 square cell in the grid
|
|
|
|
PaintOffset = CellSize # pixel size of paint offset for all drawables
|
|
|
|
GridCountX = 15 # number of columns in grid
|
|
|
|
GridCountY = 9 # number of rows in grid
|
|
|
|
ScreenWidth = CellSize * GridCountX + 2 * PaintOffset # screen width in pixels
|
|
|
|
ScreenHeight = CellSize * GridCountY + 2 * PaintOffset # screen height in pixels
|
2020-03-22 02:01:57 +01:00
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# initialize background
|
|
|
|
gridBoard = GridBoard(ScreenWidth, ScreenHeight)
|
2020-03-22 02:01:57 +01:00
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# initialize drawable objects manager
|
2020-04-03 23:54:29 +02:00
|
|
|
drawableManager = DrawableCollection()
|
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# initialize menu manager
|
2020-04-05 17:27:11 +02:00
|
|
|
menuManager = MenuManager()
|
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# initialize waiter component
|
|
|
|
waiter = Waiter(0, 0, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset)
|
2020-04-03 23:54:29 +02:00
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# adds waiter to drawable collection
|
2020-04-05 17:27:11 +02:00
|
|
|
drawableManager.add(waiter)
|
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# initialize a number of tables given in range
|
|
|
|
for i in range(1, 45):
|
|
|
|
table = Table(0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset)
|
2020-04-03 23:54:29 +02:00
|
|
|
drawableManager.generatePosition(table)
|
|
|
|
drawableManager.add(table)
|
2020-03-22 02:01:57 +01:00
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# new thread controlling tables
|
|
|
|
tableTask = TableManager(drawableManager, menuManager)
|
|
|
|
tableTask.start()
|
2020-04-05 17:27:11 +02:00
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# new thread controlling waiter
|
|
|
|
waiterTask = WaiterManager(drawableManager)
|
|
|
|
waiterTask.start()
|
2020-04-05 17:27:11 +02:00
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# main loop
|
2020-03-22 02:01:57 +01:00
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2020-04-20 23:19:13 +02:00
|
|
|
tableTask.stop()
|
|
|
|
waiterTask.stop()
|
2020-03-22 02:01:57 +01:00
|
|
|
running = False
|
|
|
|
|
2020-04-20 23:19:13 +02:00
|
|
|
# handles keyboard events
|
2020-03-22 02:01:57 +01:00
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_LEFT:
|
2020-04-20 23:19:13 +02:00
|
|
|
# checks if new waiter's position to the left is not occupied by other object
|
2020-04-03 23:54:29 +02:00
|
|
|
if drawableManager.isPositionAvailable(waiter.getX() - 1, waiter.getY()):
|
|
|
|
waiter.moveLeft()
|
2020-03-22 02:01:57 +01:00
|
|
|
if event.key == pygame.K_RIGHT:
|
2020-04-03 23:54:29 +02:00
|
|
|
# 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()
|
2020-03-22 02:01:57 +01:00
|
|
|
if event.key == pygame.K_UP:
|
2020-04-03 23:54:29 +02:00
|
|
|
# checks if new waiter's position up is not occupied by other object
|
|
|
|
if drawableManager.isPositionAvailable(waiter.getX(), waiter.getY() - 1):
|
|
|
|
waiter.moveUp()
|
2020-03-22 02:01:57 +01:00
|
|
|
if event.key == pygame.K_DOWN:
|
2020-04-03 23:54:29 +02:00
|
|
|
# checks if new waiter's position down is not occupied by other object
|
|
|
|
if drawableManager.isPositionAvailable(waiter.getX(), waiter.getY() + 1):
|
|
|
|
waiter.moveDown()
|
2020-04-20 23:19:13 +02:00
|
|
|
drawableManager.forceRepaint()
|
2020-03-22 21:52:12 +01:00
|
|
|
|
2020-04-05 17:27:11 +02:00
|
|
|
# repaints all objects to the screen
|
2020-04-20 23:19:13 +02:00
|
|
|
# is set only on initial paint or after keyboard event or call to forceRepaint()
|
|
|
|
if drawableManager.mustRepaint():
|
2020-04-05 17:27:11 +02:00
|
|
|
gridBoard.reinitialize()
|
|
|
|
gridBoard.draw(drawableManager)
|
|
|
|
gridBoard.udpdate()
|