All 14 and 15 defined with graphics.height and graphics.width, set_default_matrix added

This commit is contained in:
s444427 2020-04-05 16:37:25 +02:00
parent 7660f83334
commit 37a34e22fb
6 changed files with 62 additions and 49 deletions

View File

@ -1,14 +1,15 @@
import sys import sys
from src.graphics import * from src.graphics import *
from src.waiter import Waiter from src.waiter import *
if __name__ == "__main__": if __name__ == "__main__":
# SETUP # SETUP
pygame.init() pygame.init()
clock = pygame.time.Clock() clock = pygame.time.Clock()
fps = 40 fps = 40
waiter = Waiter()
graphics = Graphics() graphics = Graphics()
waiter = Waiter(graphics)
# init functions # init functions
graphics.drawBackground(waiter.matrix) graphics.drawBackground(waiter.matrix)
@ -28,7 +29,7 @@ if __name__ == "__main__":
break break
graphics.clear(waiter.X, waiter.Y) graphics.clear(waiter.X, waiter.Y)
waiter.update(event) waiter.update(event, graphics)
graphics.update(waiter.X, waiter.Y) graphics.update(waiter.X, waiter.Y)
pygame.display.flip() pygame.display.flip()
clock.tick(fps) clock.tick(fps)

View File

@ -12,4 +12,4 @@ __KW_____KW___
______________ ______________
______________ ______________
BBBBB_________ BBBBB_________
____B_________ FFFFB_________

View File

@ -5,8 +5,9 @@ class Graphics:
def __init__(self): def __init__(self):
self.image = { self.image = {
'floor': pygame.image.load('../resources/images/floor.jpg'), 'floor': pygame.image.load('../resources/images/floor.jpg'),
'wall': pygame.image.load('../resources/images/table3.png'), # 'wall': pygame.image.load('../resources/images/table2.png'), #
'bar': pygame.image.load('../resources/images/table3.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'), 'table': pygame.image.load('../resources/images/table3.png'),
'waiter': pygame.image.load('../resources/images/waiter.png'), 'waiter': pygame.image.load('../resources/images/waiter.png'),
'table2': pygame.image.load('../resources/images/table2.png'), 'table2': pygame.image.load('../resources/images/table2.png'),
@ -19,16 +20,16 @@ class Graphics:
} }
self.block_size = 50 self.block_size = 50
self.height = 15 self.height = 15
self.width = 14 self.width: int = 14
self.screen = pygame.display.set_mode((self.block_size*self.width, self.block_size*self.height)) self.screen = pygame.display.set_mode((self.block_size * self.width, self.block_size * self.height))
def drawBackground(self, matrix): def drawBackground(self, matrix):
for y in range(15): for y in range(self.height):
for x in range(14): for x in range(self.width):
self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size)) self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size))
for y in range(15): for y in range(self.height):
for x in range(14): for x in range(self.width):
self.screen.blit(self.image[matrix.get_type(x, y)], (x * self.block_size, y * self.block_size)) self.screen.blit(self.image[matrix.get_type(x, y)], (x * self.block_size, y * self.block_size))
def clear(self, x, y): def clear(self, x, y):

View File

@ -2,15 +2,35 @@ from src.tile import Tile
class Matrix: class Matrix:
def __init__(self): def __init__(self, graphics):
self.matrix = [] self.matrix = []
for x in range(14): self.set_default_matrix(graphics)
self.matrix.append([0] * 15) self.set_default_restaurant(graphics)
for x in range(14): def set_default_matrix(self, graphics):
for y in range(15): 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') self.matrix[x][y] = Tile(type_='floor')
self.set_default_restaurant()
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',
'K': 'chair',
'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): def get_type(self, x, y):
return self.matrix[x][y].type return self.matrix[x][y].type
@ -18,19 +38,5 @@ class Matrix:
def walk_through(self, x, y): def walk_through(self, x, y):
return self.matrix[x][y].walk_through return self.matrix[x][y].walk_through
def set_default_restaurant(self): def watch_through(self, x, y):
symbols = { return self.matrix[x][y].watch_through
'W': 'waiter',
'S': 'table',
'B': 'bar',
'X': 'wall',
'K': 'chair',
'_': 'floor'
}
lines = [line.rstrip('\n') for line in open('../resources/simulations/simulation_1.txt')]
for row in range(14):
for column in range(15):
sign = lines[column][row]
self.matrix[row][column] = Tile(symbols[sign])

View File

@ -3,12 +3,15 @@ class Tile:
def __init__(self, type_): def __init__(self, type_):
self.type = type_ self.type = type_
if self.type == 'table' or self.type == 'wall' or self.type == 'bar': 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.watch_through = 1
self.walk_through = 0 self.walk_through = 0
else: else:
self.watch_through = 1
self.walk_through = 1 self.walk_through = 1
self.watch_through = 1
self.action_required = 0 self.action_required = 0

View File

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