klasa spawnujaca dowolna liste obiektow w dowolnym obszarze

This commit is contained in:
XsedoX 2022-03-10 22:13:25 +01:00
parent bfe6405331
commit 89735b34ab
4 changed files with 40 additions and 8 deletions

15
game.py
View File

@ -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)

View File

@ -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)

View File

@ -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))

28
spawner.py Normal file
View File

@ -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()