ProjektAI/kelner/main.py

66 lines
2.6 KiB
Python
Raw Normal View History

2020-03-22 02:01:57 +01:00
import pygame
2020-03-22 21:52:12 +01:00
from src.components.GridBoard import GridBoard
from src.managers.DrawableCollection import DrawableCollection
2020-03-22 21:52:12 +01:00
from src.components.Waiter import Waiter
from src.components.Table import Table
2020-03-22 02:01:57 +01:00
2020-03-22 21:52:12 +01:00
#create screen consts
CellSize = 50 #pixel size of 1 square cell in the grid
GridCountX = 15 #number of columns in grid
GridCountY = 11 #number of rows in grid
ScreenWidth = CellSize * GridCountX #screen width in pixels
ScreenHeight = CellSize * GridCountY #screen height in pixels
2020-03-22 02:01:57 +01:00
2020-03-22 21:52:12 +01:00
#initialize background
gridBoard = GridBoard(ScreenWidth, ScreenHeight, CellSize)
2020-03-22 02:01:57 +01:00
#initialize drawable objects manager
drawableManager = DrawableCollection()
2020-03-22 21:52:12 +01:00
#initialize waiter component
waiter = Waiter(0, 0, 0, GridCountX - 1, 0, GridCountY - 1, CellSize)
#initialize a number of tables given in range
for i in range(1, 15):
table = Table(0, GridCountX - 1, 0, GridCountY - 1, CellSize)
drawableManager.generatePosition(table)
drawableManager.add(table)
2020-03-22 02:01:57 +01:00
#main loop
2020-03-22 21:52:12 +01:00
doRepaint = True
2020-03-22 02:01:57 +01:00
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#handles keyboard events
2020-03-22 02:01:57 +01:00
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()
2020-03-22 02:01:57 +01:00
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()
2020-03-22 02:01:57 +01:00
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()
2020-03-22 02:01:57 +01:00
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()
2020-03-22 21:52:12 +01:00
doRepaint = True
# repaints all objects to the screen
#is set only on initial paint or after keyboard event
2020-03-22 21:52:12 +01:00
if doRepaint:
gridBoard.reinitialize()
gridBoard.draw(drawableManager)
2020-03-22 21:52:12 +01:00
gridBoard.draw(waiter)
gridBoard.udpdate()
doRepaint = False