field zna swoj stan ale go sam nie odswieza(trzeba recznie zmieniac wartosc - todo); field jest teraz spritem(co przyda sie pozniej); spawner.py dostosowany do nowego kodu; game.py dostosowane do zmian; twierdza dalej jest widziana jako 1x1

This commit is contained in:
XsedoX 2022-04-05 10:47:45 +02:00
parent 0d6fdcbfea
commit 86aa939433
6 changed files with 85 additions and 64 deletions

View File

@ -10,13 +10,12 @@ BORDER_WIDTH = 10
BORDER_RADIUS = 5
FPS_COUNT = 60
KNIGHTS_SPAWN_WIDTH = 4
KNIGHTS_SPAWN_HEIGHT = 5
KNIGHTS_SPAWN_FIRST_ROW = 8
KNIGHTS_SPAWN_FIRST_COL = 0
MONSTER_SPAWN_WIDTH = 19
MONSTER_SPAWN_HEIGHT = 15
MONSTER_SPAWN_FIRST_COL = 4
MONSTER_SPAWN_FIRST_ROW = 0
KNIGHTS_SPAWN_HEIGHT = 7
LEFT_KNIGHTS_SPAWN_FIRST_ROW = 6
LEFT_KNIGHTS_SPAWN_FIRST_COL = 0
RIGHT_KNIGHTS_SPAWN_FIRST_ROW = 6
RIGHT_KNIGHTS_SPAWN_FIRST_COL = 20
CASTLE_SPAWN_WIDTH = 6
CASTLE_SPAWN_HEIGHT = 5
CASTLE_SPAWN_FIRST_ROW = 7

View File

@ -1,4 +1,18 @@
class Field:
def __init__(self, texture_path, converted_texture):
import pygame
class Field(pygame.sprite.Sprite):
def __init__(self, texture_path, converted_texture, img, rect, row=0, column=0):
super().__init__()
self.texture_path = texture_path
self.converted_texture = converted_texture
self.image = img
self.row = row
self.column = column
self.rect = rect
self.busy = False
self.busy_detection()
def busy_detection(self):
if self.texture_path == 'water.png' or self.texture_path == 'grass_with_tree.jpg':
self.busy = True

View File

@ -97,24 +97,23 @@ class Game:
knights_queue = KnightsQueue(knights_left, knights_right)
spawn_left_team = Spawner(grid, knights_left, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,
pos_row=KNIGHTS_SPAWN_FIRST_ROW, pos_column=KNIGHTS_SPAWN_FIRST_COL)
spawn_right_team = Spawner(grid, knights_right, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,
pos_row=KNIGHTS_SPAWN_FIRST_ROW,
pos_column=COLUMNS - KNIGHTS_SPAWN_WIDTH - 1) # - 1 to avoid list index out of range
spawn_left_team = Spawner(grid, knights_left, KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT,
LEFT_KNIGHTS_SPAWN_FIRST_ROW, LEFT_KNIGHTS_SPAWN_FIRST_COL)
spawn_right_team = Spawner(grid, knights_right, KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT,
RIGHT_KNIGHTS_SPAWN_FIRST_ROW,
RIGHT_KNIGHTS_SPAWN_FIRST_COL)
spawn_left_team.spawn()
spawn_right_team.spawn()
spawned_monsters = self.generate_monster(monsters_sprite_group)
monster_spawn = Spawner(grid, spawned_monsters, width=MONSTER_SPAWN_WIDTH, height=MONSTER_SPAWN_HEIGHT,
pos_row=MONSTER_SPAWN_FIRST_ROW, pos_column=MONSTER_SPAWN_FIRST_COL)
monster_spawn = Spawner(grid, spawned_monsters, spawn_where_possible=True)
monster_spawn.spawn_monsters()
monster_spawn.spawn()
spawned_castle = self.generate_castle(castle_sprite_group)
castle_spawn = Spawner(grid, [spawned_castle], width=CASTLE_SPAWN_WIDTH, height=CASTLE_SPAWN_HEIGHT,
pos_row=CASTLE_SPAWN_FIRST_ROW, pos_column=CASTLE_SPAWN_FIRST_COL)
castle_spawn = Spawner(grid, [spawned_castle], CASTLE_SPAWN_WIDTH, CASTLE_SPAWN_HEIGHT,
CASTLE_SPAWN_FIRST_ROW, CASTLE_SPAWN_FIRST_COL)
castle_spawn.spawn()
@ -129,7 +128,7 @@ class Game:
if event.key == pygame.K_ESCAPE:
running = False
grid.draw(self.screen)
grid.update(self.screen)
stats.draw(self.screen)
logs.draw(self.screen)

View File

@ -11,12 +11,35 @@ class Grid:
def __init__(self, textures):
self.textures = textures
self.grid = []
self.free_fields = []
self.busy_fields = []
for row in range(ROWS):
self.grid.append([])
for _ in range(COLUMNS):
self.grid.append(pygame.sprite.Group())
for column in range(COLUMNS):
box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 7,
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7,
GRID_CELL_WIDTH,
GRID_CELL_HEIGHT]
texture_path, converted_texture = self.get_random_texture()
field = Field(texture_path, converted_texture)
self.grid[row].append(field)
field = Field(
texture_path,
converted_texture,
pygame.transform.scale(converted_texture,
(GRID_CELL_WIDTH,
GRID_CELL_HEIGHT)),
pygame.Rect(box_rect),
row=row,
column=column
)
if field.busy:
self.busy_fields.append(field)
else:
self.free_fields.append(field)
self.grid[row].add(field)
def update(self, screen):
self.draw(screen)
def get_random_texture(self):
texture_index = random.randint(0, len(self.textures) - 1)
@ -27,11 +50,5 @@ class Grid:
bg_height = (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * ROWS + BORDER_WIDTH
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(5, 5, bg_width, bg_height), 0, BORDER_RADIUS)
for row in range(ROWS):
for column in range(COLUMNS):
box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 7,
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7,
GRID_CELL_WIDTH,
GRID_CELL_HEIGHT]
image = self.grid[row][column].converted_texture
screen.blit(pygame.transform.scale(image, (GRID_CELL_WIDTH, GRID_CELL_HEIGHT)), box_rect)
for fields_row in self.grid:
fields_row.draw(screen)

View File

@ -2,42 +2,35 @@ import random
from common.constants import GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT
class Spawner:
def __init__(self, grid, objs_to_spawn_list: list, width, height, pos_row, pos_column):
def __init__(self, grid, objs_to_spawn_list: list, spawn_area_width=0, spawn_area_height=0, spawn_area_pos_row=0,
spawn_area_pos_column=0, spawn_where_possible=False): # kolumny i wiersze liczymy od 0;
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.spawn_area_width = spawn_area_width
self.spawn_area_height = spawn_area_height
self.spawn_area_pos_row = spawn_area_pos_row
self.spawn_area_pos_column = spawn_area_pos_column
self.grid = grid
self.spawn_where_possible = spawn_where_possible
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" and self.grid.grid[row][column].texture_path != "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])
if self.spawn_where_possible:
possible_fields = self.grid.free_fields
else:
possible_fields = []
for field in self.grid.free_fields:
if (self.spawn_area_pos_column <= field.column < (self.spawn_area_width + self.spawn_area_pos_column)) \
and (self.spawn_area_pos_row <= field.row < (self.spawn_area_height + self.spawn_area_pos_row)):
possible_fields.append(field)
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()
def spawn_monsters(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" and self.grid.grid[row][column].texture_path != "grass_with_tree.jpg" and not ((10 <=column <=14) and (8 <= row <= 12)):
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)
ran_index = random.randint(0, len(possible_fields)-1)
ran_field = possible_fields[ran_index]
ran_field.busy = True
obj.rect = obj.rect.clamp(ran_field)
self.grid.busy_fields.append(ran_field)
if ran_field in self.grid.free_fields:
self.grid.free_fields.remove(ran_field)
if ran_field in possible_fields:
possible_fields.remove(ran_field)
obj.update()

View File

@ -15,4 +15,3 @@ class Knight(pygame.sprite.Sprite):
self.defense = random.randint(1,4)
self.points = 1
knights_list = pygame.sprite.Group()