klasa kelner

This commit is contained in:
s444417 2020-03-22 21:52:12 +01:00
parent a89bac8bd6
commit 8b19428481
7 changed files with 115 additions and 40 deletions

View File

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 88 KiB

View File

Before

Width:  |  Height:  |  Size: 156 KiB

After

Width:  |  Height:  |  Size: 156 KiB

View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@ -1,60 +1,43 @@
import pygame import pygame
from src.components.GridBoard import GridBoard
from src.components.Waiter import Waiter
#initialize the pygame
pygame.init()
#create the screen #create screen consts
screen = pygame.display.set_mode((1000,1000)) 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 #initialize background
pygame.display.set_caption("Bardzo mądry kelner") gridBoard = GridBoard(ScreenWidth, ScreenHeight, CellSize)
#grid #initialize waiter component
gridImg = pygame.image.load("20x20grid.png") waiter = Waiter(1, 1, 0, GridCountX - 1, 0, GridCountY - 1, CellSize)
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))
#loop #loop
doRepaint = True
running = True running = True
while running: while running:
# RGB
screen.fill((255, 255, 255))
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: if event.key == pygame.K_LEFT:
waiterX_change = -50 waiter.moveLeft()
if event.key == pygame.K_RIGHT: if event.key == pygame.K_RIGHT:
waiterX_change = 50 waiter.moveRight()
if event.key == pygame.K_UP: if event.key == pygame.K_UP:
waiterY_change = -50 waiter.moveUp()
if event.key == pygame.K_DOWN: if event.key == pygame.K_DOWN:
waiterY_change = 50 waiter.moveDown()
if event.type == pygame.KEYUP: doRepaint = True
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
grid() if doRepaint:
waiterX += waiterX_change gridBoard.reinitialize()
waiterY += waiterY_change gridBoard.draw(waiter)
waiter(waiterX, waiterY) gridBoard.udpdate()
pygame.display.update() doRepaint = False

View 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()

View 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))

View File