From b06e38232261fd51883a269e04d9693a140e5aa3 Mon Sep 17 00:00:00 2001 From: s444427 Date: Fri, 3 Apr 2020 18:49:47 +0200 Subject: [PATCH] File division, Class Matrix, Waiter, Graphics All graphics in Graphics (draw, clear, update) Waiter movement moved to Waiter Main reduced to minimum --- main.py | 125 +++++++++++------------------------------------- src/graphics.py | 23 +++++++++ src/matrix.py | 15 ++++++ src/tile.py | 8 +++- src/waiter.py | 33 +++++++++++++ 5 files changed, 107 insertions(+), 97 deletions(-) create mode 100644 src/graphics.py create mode 100644 src/matrix.py diff --git a/main.py b/main.py index 7500040..4269adf 100644 --- a/main.py +++ b/main.py @@ -1,108 +1,41 @@ import sys -import pygame +from src.graphics import * +from src.matrix import Matrix +from src.waiter import Waiter -# WAITER -class Waiter(pygame.sprite.Sprite): - def __init__(self): - pygame.sprite.Sprite.__init__(self) - self.moveX = 0 - self.moveY = 0 - self.clearX = 0 - self.clearY = 0 - self.frame = 0 - self.imageWaiter = waiter - self.imageFloor = floor - - def move(self, x, y): - self.moveX += x - self.moveY -= y - - def clear(self, x, y): - cx = self.moveX + x - cy = self.moveY + y - screen.blit(self.imageFloor, (cx * block_size, cy * block_size)) - - def update(self): - screen.blit(self.imageWaiter, (self.moveX * block_size, self.moveY * block_size)) - - -# TILE -class Tile: - def __init__(self, x_position, y_position): - self.x_position = x_position - self.y_position = y_position - self.watch_through = 1 - self.walk_through = 1 - self.action_required = 0 - - -# TABLE -class Table(Tile): - def __init__(self, x_position, y_position): - super().__init__(x_position, y_position) - self.walk_through = 0 - - -# SETUP -pygame.init() -screen = pygame.display.set_mode((700, 750)) -done = False -block_size = 50 -clock = pygame.time.Clock() -fps = 40 -ani = 40 -rect = 1 - -floor = pygame.image.load('../resources/images/floor.jpg') -table = pygame.image.load('../resources/images/table.png') -waiter = pygame.image.load('../resources/images/waiter.png') -matrix = [] - - -def drawBackground(): - for y in range(15): - for x in range(14): - screen.blit(floor, (x * block_size, y * block_size)) - - -def matrix_creator(): - for x in range(14): - matrix.append([0] * 15) - - for x in range(14): - for y in range(15): - matrix[x][y] = Tile(x, y) - - -waiterPly = Waiter() - if __name__ == "__main__": - drawBackground() - matrix_creator() + # SETUP + pygame.init() + clock = pygame.time.Clock() + fps = 40 + # matrix = [] + waiterPly = Waiter() + graphics = Graphics() - while not done: + # init functions + graphics.drawBackground() + graphics.update(waiterPly.moveX, waiterPly.moveY) + + # matrix_creator() + + matrix = Matrix() + + while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() + break + if event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: - waiterPly.move(-rect, 0) - waiterPly.clear(rect, 0) - if event.key == pygame.K_RIGHT: - waiterPly.move(rect, 0) - waiterPly.clear(-rect, 0) - if event.key == pygame.K_UP: - waiterPly.move(0, rect) - waiterPly.clear(0, rect) - if event.key == pygame.K_DOWN: - waiterPly.move(0, -rect) - waiterPly.clear(0, -rect) - waiterPly.update() + if event.key == pygame.K_ESCAPE: + pygame.quit() + sys.exit() + break + + graphics.clear(waiterPly.moveX, waiterPly.moveY) + waiterPly.update(event) + graphics.update(waiterPly.moveX, waiterPly.moveY) pygame.display.flip() clock.tick(fps) - - # # Testy funkcjonalne - # matrix[0][0] = Table(0, 0) - # print(matrix[0][0].walk_through, matrix[1][0].walk_through) diff --git a/src/graphics.py b/src/graphics.py new file mode 100644 index 0000000..0f1ba76 --- /dev/null +++ b/src/graphics.py @@ -0,0 +1,23 @@ +import pygame + + +class Graphics: + def __init__(self): + self.image = { + 'floor': pygame.image.load('../resources/images/floor.jpg'), + 'table': pygame.image.load('../resources/images/table.png'), + 'waiter': pygame.image.load('../resources/images/waiter.png'), + } + self.screen = pygame.display.set_mode((700, 750)) + self.block_size = 50 + + def drawBackground(self): + for y in range(15): + for x in range(14): + self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size)) + + def clear(self, x, y): + self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size)) + + def update(self, x, y): + self.screen.blit(self.image['waiter'], (x * self.block_size, y * self.block_size)) diff --git a/src/matrix.py b/src/matrix.py new file mode 100644 index 0000000..2a7f36d --- /dev/null +++ b/src/matrix.py @@ -0,0 +1,15 @@ +from src.tile import Tile + + +class Matrix: + def __init__(self): + self.matrix = [] + for x in range(14): + self.matrix.append([0] * 15) + + for x in range(14): + for y in range(15): + self.matrix[x][y] = Tile(type_='floor', watch_through=1) + + def get_name(self, x, y): + return self.matrix[x][y].type diff --git a/src/tile.py b/src/tile.py index 8b13789..c63e8aa 100644 --- a/src/tile.py +++ b/src/tile.py @@ -1 +1,7 @@ - +# TILE +class Tile: + def __init__(self, type_, watch_through): + self.watch_through = watch_through + self.walk_through = 1 + self.action_required = 0 + self.type = type_ diff --git a/src/waiter.py b/src/waiter.py index e69de29..e1de286 100644 --- a/src/waiter.py +++ b/src/waiter.py @@ -0,0 +1,33 @@ +import pygame + +screen = pygame.display.set_mode((700, 750)) +block_size = 50 + + +# WAITER +class Waiter(pygame.sprite.Sprite): + def __init__(self): + pygame.sprite.Sprite.__init__(self) + self.moveX = 0 + self.moveY = 0 + self.clearX = 0 + self.clearY = 0 + self.frame = 0 + + #Borders + def move(self, x, y): + if 0 <= self.moveX + x <= 14: + self.moveX += x + if 0 <= self.moveY + y <= 15: + self.moveY += y + + def update(self, event): + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + self.move(-1, 0) + if event.key == pygame.K_RIGHT: + self.move(1, 0) + if event.key == pygame.K_UP: + self.move(0, -1) + if event.key == pygame.K_DOWN: + self.move(0, 1)