Add chairs and wall graphics

This commit is contained in:
s450026 2020-04-06 22:10:21 +02:00
parent 399062786d
commit 1c14696f4f
17 changed files with 187 additions and 4 deletions

35
main.py Normal file
View File

@ -0,0 +1,35 @@
import sys
from src.graphics import *
from src.waiter import *
if __name__ == "__main__":
# SETUP
pygame.init()
clock = pygame.time.Clock()
fps = 40
graphics = Graphics()
waiter = Waiter(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)
graphics.update(waiter.X, waiter.Y)
pygame.display.flip()
clock.tick(fps)

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
resources/images/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

@ -0,0 +1,15 @@
______________
______________
__PW_____PW___
_USWP___USWP__
__OWS____OWSI_
__PWO____PWO__
_USW____USW___
__OWP____OWP__
__PWS____PWSI_
_USWO___USWO__
__OW_____OW___
______________
______________
BBBBB_________
FFFFB_________

View File

@ -0,0 +1,8 @@
X___TXFX
T_X_X__X
X_X_X_XX
T_X____T
X_X_XX_X
X_X____T
T_X_XXXX
XXX____W

View File

@ -0,0 +1,2 @@
__X
___

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

36
src/graphics.py Normal file
View File

@ -0,0 +1,36 @@
import pygame
class Graphics:
def __init__(self):
self.image = {
'floor': pygame.image.load('../resources/images/floor.jpg'),
'wall': pygame.image.load('../resources/images/wall.png'), #
'bar': pygame.image.load('../resources/images/table3.png'), #
'bar_floor': pygame.image.load('../resources/images/waiter.png'), #
'table': pygame.image.load('../resources/images/table3.png'),
'waiter': pygame.image.load('../resources/images/waiter.png'),
'chair_front': pygame.image.load('../resources/images/chair-front.png'), #
'chair_back': pygame.image.load('../resources/images/chair-back.png'), #
'chair_left': pygame.image.load('../resources/images/chair-left.png'), #
'chair_right': pygame.image.load('../resources/images/chair-right.png') #
}
self.block_size = 50
self.height = 15
self.width: int = 14
self.screen = pygame.display.set_mode((self.block_size * self.width, self.block_size * self.height))
def drawBackground(self, matrix):
for y in range(self.height):
for x in range(self.width):
self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size))
for y in range(self.height):
for x in range(self.width):
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))

45
src/matrix.py Normal file
View File

@ -0,0 +1,45 @@
from src.tile import Tile
class Matrix:
def __init__(self, graphics):
self.matrix = []
self.set_default_matrix(graphics)
self.set_default_restaurant(graphics)
def set_default_matrix(self, graphics):
for x in range(graphics.width):
self.matrix.append([0] * graphics.height)
for x in range(graphics.width):
for y in range(graphics.height):
self.matrix[x][y] = Tile(type_='floor')
def set_default_restaurant(self, graphics):
lines = [line.rstrip('\n') for line in open('../resources/simulations/simulation_1.txt')]
symbols = {
'_': 'floor',
'W': 'wall',
'S': 'table',
'B': 'bar',
'F': 'bar_floor',
'P': 'chair_front',
'O': 'chair_back',
'I': 'chair_left',
'U': 'chair_right',
'X': 'waiter'
}
for x in range(graphics.width):
for y in range(graphics.height):
sign = lines[y][x]
self.matrix[x][y] = Tile(symbols[sign])
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
def watch_through(self, x, y):
return self.matrix[x][y].watch_through

View File

@ -1,7 +1,17 @@
# TILE
class Tile:
def __init__(self, type_, watch_through):
self.watch_through = watch_through
self.walk_through = 1
self.action_required = 0
def __init__(self, type_):
self.type = type_
if self.type == 'wall':
self.watch_through = 0
self.walk_through = 0
elif self.type == 'table' or self.type == 'bar' or self.type == 'chair':
self.watch_through = 1
self.walk_through = 0
else:
self.walk_through = 1
self.watch_through = 1
self.action_required = 0

32
src/waiter.py Normal file
View File

@ -0,0 +1,32 @@
import pygame
from src.matrix import Matrix
# WAITER
class Waiter(pygame.sprite.Sprite):
def __init__(self, graphics):
pygame.sprite.Sprite.__init__(self)
self.X = 0
self.Y = 0
self.frame = 0
self.matrix = Matrix(graphics=graphics)
# Borders
def move(self, x, y, graphics):
if 0 <= self.X + x <= graphics.width - 1 and 0 <= self.Y + y <= graphics.height - 1:
if self.matrix.walk_through(self.X + x, self.Y + y) == 1:
self.X += x
self.Y += y
def update(self, event, graphics):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move(-1, 0, graphics)
if event.key == pygame.K_RIGHT:
self.move(1, 0, graphics)
if event.key == pygame.K_UP:
self.move(0, -1, graphics)
if event.key == pygame.K_DOWN:
self.move(0, 1, graphics)
print(self.X, self.Y)