forked from s444417/ProjektAI
123 lines
5.3 KiB
Python
123 lines
5.3 KiB
Python
import pygame
|
|
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
|
|
|
|
# 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
|
|
|
|
# initialize background
|
|
gridBoard = GridBoard(ScreenWidth, ScreenHeight)
|
|
|
|
# initialize drawable objects manager
|
|
drawableManager = DrawableCollection()
|
|
|
|
# initialize menu manager
|
|
menuManager = MenuManager()
|
|
|
|
# initialize waiter component
|
|
waiter1 = Waiter(0, 0, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset)
|
|
waiter2 = Waiter(0, 1, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset)
|
|
waiter3 = Waiter(0, 2, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset)
|
|
waiter4 = Waiter(0, 3, 0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset)
|
|
|
|
# adds waiter to drawable collection
|
|
drawableManager.add(waiter1)
|
|
drawableManager.add(waiter2)
|
|
drawableManager.add(waiter3)
|
|
drawableManager.add(waiter4)
|
|
|
|
# initialize a number of tables given in range
|
|
for i in range(1, 40):
|
|
table = Table(0, GridCountX - 1, 0, GridCountY - 1, CellSize, PaintOffset)
|
|
if drawableManager.generatePosition(table):
|
|
drawableManager.add(table)
|
|
|
|
# new thread controlling tables
|
|
tableTask = TableManager(drawableManager, menuManager)
|
|
tableTask.start()
|
|
|
|
# new thread controlling waiter
|
|
waiter1Task = WaiterManager(drawableManager, [waiter1])
|
|
waiter1Task.start()
|
|
|
|
waiter2Task = WaiterManager(drawableManager, [waiter2])
|
|
waiter2Task.start()
|
|
|
|
waiter3Task = WaiterManager(drawableManager, [waiter3])
|
|
waiter3Task.start()
|
|
|
|
waiter4Task = WaiterManager(drawableManager, [waiter4])
|
|
waiter4Task.start()
|
|
|
|
# main loop
|
|
running = True
|
|
while running:
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
tableTask.stop()
|
|
waiter1Task.stop()
|
|
waiter2Task.stop()
|
|
waiter3Task.stop()
|
|
waiter4Task.stop()
|
|
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(waiter1.getX() - 1, waiter1.getY()):
|
|
waiter1.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(waiter1.getX() + 1, waiter1.getY()):
|
|
waiter1.moveRight()
|
|
if event.key == pygame.K_UP:
|
|
# checks if new waiter's position up is not occupied by other object
|
|
if drawableManager.isPositionAvailable(waiter1.getX(), waiter1.getY() - 1):
|
|
waiter1.moveUp()
|
|
if event.key == pygame.K_DOWN:
|
|
# checks if new waiter's position down is not occupied by other object
|
|
if drawableManager.isPositionAvailable(waiter1.getX(), waiter1.getY() + 1):
|
|
waiter1.moveDown()
|
|
|
|
if event.key == pygame.K_w:
|
|
# checks if new waiter's position to the left is not occupied by other object
|
|
if drawableManager.isPositionAvailable(waiter1.getX() - 1, waiter1.getY() - 1):
|
|
waiter1.moveLeft()
|
|
waiter1.moveUp()
|
|
if event.key == pygame.K_d:
|
|
# checks if new waiter's position to the right is not occupied by other object
|
|
if drawableManager.isPositionAvailable(waiter1.getX() + 1, waiter1.getY() - 1):
|
|
waiter1.moveRight()
|
|
waiter1.moveUp()
|
|
if event.key == pygame.K_s:
|
|
# checks if new waiter's position up is not occupied by other object
|
|
if drawableManager.isPositionAvailable(waiter1.getX() + 1, waiter1.getY() + 1):
|
|
waiter1.moveDown()
|
|
waiter1.moveRight()
|
|
if event.key == pygame.K_a:
|
|
# checks if new waiter's position down is not occupied by other object
|
|
if drawableManager.isPositionAvailable(waiter1.getX() - 1, waiter1.getY() + 1):
|
|
waiter1.moveDown()
|
|
waiter1.moveLeft()
|
|
drawableManager.forceRepaint()
|
|
|
|
# repaints all objects to the screen
|
|
# is set only on initial paint or after keyboard event or call to forceRepaint()
|
|
if drawableManager.mustRepaint():
|
|
gridBoard.reinitialize()
|
|
gridBoard.draw(drawableManager)
|
|
gridBoard.udpdate()
|