diff --git a/main.py b/main.py new file mode 100644 index 0000000..1c5ff22 --- /dev/null +++ b/main.py @@ -0,0 +1,35 @@ +import sys +from src.graphics import * +from src.matrix import Matrix +from src.waiter import Waiter + +if __name__ == "__main__": + # SETUP + pygame.init() + clock = pygame.time.Clock() + fps = 40 + waiter = Waiter() + graphics = Graphics() + + # init functions + graphics.drawBackground(waiter.matrix) + graphics.update(waiter.X, waiter.Y) + + 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_ESCAPE: + pygame.quit() + sys.exit() + break + + graphics.clear(waiter.X, waiter.Y) + waiter.update(event) + graphics.update(waiter.X, waiter.Y) + pygame.display.flip() + clock.tick(fps) diff --git a/src/graphics.py b/src/graphics.py new file mode 100644 index 0000000..9369b74 --- /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, matrix): + for y in range(15): + for x in range(14): + self.screen.blit(self.image[matrix.get_type(x, y)], (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/main.py b/src/main.py deleted file mode 100644 index acc039b..0000000 --- a/src/main.py +++ /dev/null @@ -1,77 +0,0 @@ -import sys -import pygame - - -# WAITER -class Waiter(pygame.sprite.Sprite): - def __init__(self): - pygame.sprite.Sprite.__init__(self) - self.moveX = 0 - self.moveY = 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)) - - -# SETUP -pygame.init() -screen = pygame.display.set_mode((700, 750)) -done = False -block_size = 50 -clock = pygame.time.Clock() -fps = 30 -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') - - -def drawBackground(): - for y in range(15): - for x in range(14): - screen.blit(floor, (x * block_size, y * block_size)) - - -waiterPly = Waiter() - - -def main(): - drawBackground() - while not done: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - pygame.quit() - sys.exit() - 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() - pygame.display.flip() - clock.tick(fps) - - -main() diff --git a/src/matrix.py b/src/matrix.py new file mode 100644 index 0000000..bedfffe --- /dev/null +++ b/src/matrix.py @@ -0,0 +1,20 @@ +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) + self.matrix[1][0].type = 'waiter' + self.matrix[1][0].walk_through = 0 + + def get_type(self, x, y): + return self.matrix[x][y].type + + def walk_through(self, x, y): + return self.matrix[x][y].walk_through diff --git a/src/tile.py b/src/tile.py new file mode 100644 index 0000000..c63e8aa --- /dev/null +++ b/src/tile.py @@ -0,0 +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 new file mode 100644 index 0000000..ccb53c6 --- /dev/null +++ b/src/waiter.py @@ -0,0 +1,31 @@ +import pygame +from src.matrix import Matrix + + +# WAITER +class Waiter(pygame.sprite.Sprite): + def __init__(self): + pygame.sprite.Sprite.__init__(self) + self.X = 0 + self.Y = 0 + self.frame = 0 + self.matrix = Matrix() + + # Borders + def move(self, x, y): + if self.matrix.walk_through(self.X + x, self.Y + y) == 1: + if 0 <= self.X + x <= 14: + self.X += x + if 0 <= self.Y + y <= 15: + self.Y += 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)