forked from s444417/ProjektAI
klasa kelner
This commit is contained in:
parent
a89bac8bd6
commit
8b19428481
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 156 KiB After Width: | Height: | Size: 156 KiB |
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |
@ -1,60 +1,43 @@
|
||||
import pygame
|
||||
from src.components.GridBoard import GridBoard
|
||||
from src.components.Waiter import Waiter
|
||||
|
||||
#initialize the pygame
|
||||
pygame.init()
|
||||
|
||||
#create the screen
|
||||
screen = pygame.display.set_mode((1000,1000))
|
||||
#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
|
||||
|
||||
#caption
|
||||
pygame.display.set_caption("Bardzo mądry kelner")
|
||||
#initialize background
|
||||
gridBoard = GridBoard(ScreenWidth, ScreenHeight, CellSize)
|
||||
|
||||
#grid
|
||||
gridImg = pygame.image.load("20x20grid.png")
|
||||
gridX = 0
|
||||
gridY = 0
|
||||
|
||||
#weiter
|
||||
waiterImg = pygame.image.load("waiter.png")
|
||||
waiterX = 10
|
||||
waiterY = 1
|
||||
waiterX_change = 0
|
||||
waiterY_change = 0
|
||||
|
||||
def grid():
|
||||
screen.blit(gridImg, (gridX,gridY))
|
||||
|
||||
def waiter(x, y):
|
||||
screen.blit(waiterImg, (x,y))
|
||||
#initialize waiter component
|
||||
waiter = Waiter(1, 1, 0, GridCountX - 1, 0, GridCountY - 1, CellSize)
|
||||
|
||||
#loop
|
||||
doRepaint = True
|
||||
running = True
|
||||
while running:
|
||||
|
||||
# RGB
|
||||
screen.fill((255, 255, 255))
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT:
|
||||
waiterX_change = -50
|
||||
waiter.moveLeft()
|
||||
if event.key == pygame.K_RIGHT:
|
||||
waiterX_change = 50
|
||||
waiter.moveRight()
|
||||
if event.key == pygame.K_UP:
|
||||
waiterY_change = -50
|
||||
waiter.moveUp()
|
||||
if event.key == pygame.K_DOWN:
|
||||
waiterY_change = 50
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
|
||||
waiterX_change = 0
|
||||
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
|
||||
waiterY_change = 0
|
||||
waiter.moveDown()
|
||||
doRepaint = True
|
||||
|
||||
grid()
|
||||
waiterX += waiterX_change
|
||||
waiterY += waiterY_change
|
||||
waiter(waiterX, waiterY)
|
||||
pygame.display.update()
|
||||
if doRepaint:
|
||||
gridBoard.reinitialize()
|
||||
gridBoard.draw(waiter)
|
||||
gridBoard.udpdate()
|
||||
doRepaint = False
|
24
kelner/src/components/GridBoard.py
Normal file
24
kelner/src/components/GridBoard.py
Normal file
@ -0,0 +1,24 @@
|
||||
import pygame
|
||||
|
||||
class GridBoard:
|
||||
|
||||
def __init__(self, width, height, cellSize):
|
||||
pygame.init() #initialize the pygame
|
||||
pygame.display.set_caption("Bardzo mądry kelner") #window caption
|
||||
self.__width = width
|
||||
self.__height = height
|
||||
self.__cellSize = cellSize
|
||||
self.__screen = pygame.display.set_mode((width, height)) #initialize screen
|
||||
|
||||
def reinitialize(self):
|
||||
self.__screen.fill((255, 255, 255))
|
||||
for x in range(0, self.__width, self.__cellSize):
|
||||
pygame.draw.line(self.__screen, (0,0,0), (x,0), (x,(self.__height - 1)))
|
||||
for y in range(0, self.__height, self.__cellSize):
|
||||
pygame.draw.line(self.__screen, (0,0,0), (0,y), ((self.__width - 1),y))
|
||||
|
||||
def draw(self, component):
|
||||
component.draw(self.__screen)
|
||||
|
||||
def udpdate(self):
|
||||
pygame.display.update()
|
68
kelner/src/components/Waiter.py
Normal file
68
kelner/src/components/Waiter.py
Normal file
@ -0,0 +1,68 @@
|
||||
import pygame
|
||||
|
||||
|
||||
class Waiter:
|
||||
|
||||
def __init__(self, x, y, minX, maxX, minY, maxY, ratio):
|
||||
self.__minX = minX
|
||||
self.__maxX = maxX
|
||||
self.__minY = minY
|
||||
self.__maxY = maxY
|
||||
self.setX(x)
|
||||
self.setY(y)
|
||||
self.__ratio = ratio #cell size
|
||||
self.__image = self.__loadImg('./images/waiter.png')
|
||||
|
||||
def setX(self, x):
|
||||
if (x < self.__minX or self.__maxX < x):
|
||||
return False
|
||||
else:
|
||||
self.__x = x
|
||||
return True
|
||||
|
||||
def setY(self, y):
|
||||
if (y < self.__minY or self.__maxY < y):
|
||||
return False
|
||||
else:
|
||||
self.__y = y
|
||||
return True
|
||||
|
||||
def getX(self):
|
||||
return self.__x
|
||||
|
||||
def getY(self):
|
||||
return self.__y
|
||||
|
||||
def moveUp(self):
|
||||
if self.__y > self.__minY:
|
||||
self.__y -= 1
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def moveDown(self):
|
||||
if self.__y < self.__maxY:
|
||||
self.__y += 1
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def moveLeft(self):
|
||||
if self.__x > self.__minX:
|
||||
self.__x -= 1
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def moveRight(self):
|
||||
if self.__x < self.__maxX:
|
||||
self.__x += 1
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def __loadImg(self, filePath):
|
||||
return pygame.image.load(filePath)
|
||||
|
||||
def draw(self, screen):
|
||||
screen.blit(self.__image, (self.__x * self.__ratio, self.__y * self.__ratio))
|
Loading…
Reference in New Issue
Block a user