diff --git a/game.py b/game.py index 87956ec..831d74b 100644 --- a/game.py +++ b/game.py @@ -8,6 +8,7 @@ from helpers import draw_text from logs import Logs from stats import Stats from knight import Knight +from spawner import Spawner class Game: @@ -111,6 +112,14 @@ class Game: grid = Grid(self.tiles) stats = Stats() logs = Logs() + knight1 = Knight("/resources/textures/knight.png") + knight2 = Knight("/resources/textures/knight.png") + spawn = Spawner(grid, [knight1, knight2], 4, 5, 8, 0) + spawn.spawn() + knights_list = pygame.sprite.Group() + knights_list.add(knight1) + knights_list.add(knight2) + while running: self.screen.blit(self.bg, (0, 0)) @@ -124,13 +133,9 @@ class Game: running = False grid.draw(self.screen) + knights_list.draw(self.screen) stats.draw(self.screen) logs.draw(self.screen) - knight1 = Knight(400, 200, "/resources/textures/knight.png") - knights_list = pygame.sprite.Group() - knights_list.add(knight1) - knights_list.draw(self.screen) - pygame.display.update() self.clock.tick(FPS_COUNT) diff --git a/grid.py b/grid.py index 891c618..7553f1a 100644 --- a/grid.py +++ b/grid.py @@ -33,3 +33,4 @@ class Grid: GRID_CELL_HEIGHT] image = self.grid[row][column].converted_texture screen.blit(pygame.transform.scale(image, (GRID_CELL_WIDTH, GRID_CELL_HEIGHT)), box_rect) + diff --git a/knight.py b/knight.py index a9e634c..b11128c 100644 --- a/knight.py +++ b/knight.py @@ -4,10 +4,8 @@ from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CE WINDOW_WIDTH, WINDOW_HEIGHT class Knight(pygame.sprite.Sprite): - def __init__(self, x, y, img): + def __init__(self, img): super().__init__() - self.x = x - self.y = y self.images = [] self.image = pygame.image.load("resources/textures/knight.png") self.image = pygame.transform.scale(self.image, (40, 40)) diff --git a/spawner.py b/spawner.py new file mode 100644 index 0000000..4117262 --- /dev/null +++ b/spawner.py @@ -0,0 +1,28 @@ +from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, BORDER_RADIUS +import random + + +class Spawner: + def __init__(self, grid, objs_to_spawn_list: list, width, height, pos_row, pos_column): + self.objs_to_spawn_list = objs_to_spawn_list + self.width = width + self.height = height + self.pos_row = pos_row + self.pos_column = pos_column + self.grid = grid + + def spawn(self): + coords = [] + for row in range(self.pos_row, (self.pos_row + self.width)): + for column in range(self.pos_column, (self.pos_column + self.height)): + if self.grid.grid[row][column].texture_path != ("water.png" or "grass_with_tree.jpg"): + coords.append([(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 7, + (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7]) + + for obj in self.objs_to_spawn_list: + random_tile = random.randint(0, len(coords)-1) + obj.rect.x = coords[random_tile][0] + obj.rect.y = coords[random_tile][1] + coords.pop(random_tile) + obj.update() +