forked from s464965/WMICraft
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:
parent
0d6fdcbfea
commit
86aa939433
@ -10,13 +10,12 @@ BORDER_WIDTH = 10
|
|||||||
BORDER_RADIUS = 5
|
BORDER_RADIUS = 5
|
||||||
FPS_COUNT = 60
|
FPS_COUNT = 60
|
||||||
KNIGHTS_SPAWN_WIDTH = 4
|
KNIGHTS_SPAWN_WIDTH = 4
|
||||||
KNIGHTS_SPAWN_HEIGHT = 5
|
KNIGHTS_SPAWN_HEIGHT = 7
|
||||||
KNIGHTS_SPAWN_FIRST_ROW = 8
|
LEFT_KNIGHTS_SPAWN_FIRST_ROW = 6
|
||||||
KNIGHTS_SPAWN_FIRST_COL = 0
|
LEFT_KNIGHTS_SPAWN_FIRST_COL = 0
|
||||||
MONSTER_SPAWN_WIDTH = 19
|
RIGHT_KNIGHTS_SPAWN_FIRST_ROW = 6
|
||||||
MONSTER_SPAWN_HEIGHT = 15
|
RIGHT_KNIGHTS_SPAWN_FIRST_COL = 20
|
||||||
MONSTER_SPAWN_FIRST_COL = 4
|
|
||||||
MONSTER_SPAWN_FIRST_ROW = 0
|
|
||||||
CASTLE_SPAWN_WIDTH = 6
|
CASTLE_SPAWN_WIDTH = 6
|
||||||
CASTLE_SPAWN_HEIGHT = 5
|
CASTLE_SPAWN_HEIGHT = 5
|
||||||
CASTLE_SPAWN_FIRST_ROW = 7
|
CASTLE_SPAWN_FIRST_ROW = 7
|
||||||
|
@ -1,4 +1,18 @@
|
|||||||
class Field:
|
import pygame
|
||||||
def __init__(self, texture_path, converted_texture):
|
|
||||||
|
|
||||||
|
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.texture_path = texture_path
|
||||||
self.converted_texture = converted_texture
|
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
|
||||||
|
@ -97,24 +97,23 @@ class Game:
|
|||||||
|
|
||||||
knights_queue = KnightsQueue(knights_left, knights_right)
|
knights_queue = KnightsQueue(knights_left, knights_right)
|
||||||
|
|
||||||
spawn_left_team = Spawner(grid, knights_left, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,
|
spawn_left_team = Spawner(grid, knights_left, KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT,
|
||||||
pos_row=KNIGHTS_SPAWN_FIRST_ROW, pos_column=KNIGHTS_SPAWN_FIRST_COL)
|
LEFT_KNIGHTS_SPAWN_FIRST_ROW, LEFT_KNIGHTS_SPAWN_FIRST_COL)
|
||||||
spawn_right_team = Spawner(grid, knights_right, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,
|
spawn_right_team = Spawner(grid, knights_right, KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT,
|
||||||
pos_row=KNIGHTS_SPAWN_FIRST_ROW,
|
RIGHT_KNIGHTS_SPAWN_FIRST_ROW,
|
||||||
pos_column=COLUMNS - KNIGHTS_SPAWN_WIDTH - 1) # - 1 to avoid list index out of range
|
RIGHT_KNIGHTS_SPAWN_FIRST_COL)
|
||||||
|
|
||||||
spawn_left_team.spawn()
|
spawn_left_team.spawn()
|
||||||
spawn_right_team.spawn()
|
spawn_right_team.spawn()
|
||||||
|
|
||||||
spawned_monsters = self.generate_monster(monsters_sprite_group)
|
spawned_monsters = self.generate_monster(monsters_sprite_group)
|
||||||
monster_spawn = Spawner(grid, spawned_monsters, width=MONSTER_SPAWN_WIDTH, height=MONSTER_SPAWN_HEIGHT,
|
monster_spawn = Spawner(grid, spawned_monsters, spawn_where_possible=True)
|
||||||
pos_row=MONSTER_SPAWN_FIRST_ROW, pos_column=MONSTER_SPAWN_FIRST_COL)
|
|
||||||
|
|
||||||
monster_spawn.spawn_monsters()
|
monster_spawn.spawn()
|
||||||
|
|
||||||
spawned_castle = self.generate_castle(castle_sprite_group)
|
spawned_castle = self.generate_castle(castle_sprite_group)
|
||||||
castle_spawn = Spawner(grid, [spawned_castle], width=CASTLE_SPAWN_WIDTH, height=CASTLE_SPAWN_HEIGHT,
|
castle_spawn = Spawner(grid, [spawned_castle], CASTLE_SPAWN_WIDTH, CASTLE_SPAWN_HEIGHT,
|
||||||
pos_row=CASTLE_SPAWN_FIRST_ROW, pos_column=CASTLE_SPAWN_FIRST_COL)
|
CASTLE_SPAWN_FIRST_ROW, CASTLE_SPAWN_FIRST_COL)
|
||||||
|
|
||||||
castle_spawn.spawn()
|
castle_spawn.spawn()
|
||||||
|
|
||||||
@ -129,7 +128,7 @@ class Game:
|
|||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
running = False
|
running = False
|
||||||
|
|
||||||
grid.draw(self.screen)
|
grid.update(self.screen)
|
||||||
stats.draw(self.screen)
|
stats.draw(self.screen)
|
||||||
logs.draw(self.screen)
|
logs.draw(self.screen)
|
||||||
|
|
||||||
|
@ -11,12 +11,35 @@ class Grid:
|
|||||||
def __init__(self, textures):
|
def __init__(self, textures):
|
||||||
self.textures = textures
|
self.textures = textures
|
||||||
self.grid = []
|
self.grid = []
|
||||||
|
self.free_fields = []
|
||||||
|
self.busy_fields = []
|
||||||
for row in range(ROWS):
|
for row in range(ROWS):
|
||||||
self.grid.append([])
|
self.grid.append(pygame.sprite.Group())
|
||||||
for _ in range(COLUMNS):
|
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()
|
texture_path, converted_texture = self.get_random_texture()
|
||||||
field = Field(texture_path, converted_texture)
|
field = Field(
|
||||||
self.grid[row].append(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):
|
def get_random_texture(self):
|
||||||
texture_index = random.randint(0, len(self.textures) - 1)
|
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
|
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)
|
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(5, 5, bg_width, bg_height), 0, BORDER_RADIUS)
|
||||||
|
|
||||||
for row in range(ROWS):
|
for fields_row in self.grid:
|
||||||
for column in range(COLUMNS):
|
fields_row.draw(screen)
|
||||||
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)
|
|
||||||
|
@ -2,42 +2,35 @@ import random
|
|||||||
|
|
||||||
from common.constants import GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT
|
from common.constants import GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT
|
||||||
|
|
||||||
|
|
||||||
class Spawner:
|
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.objs_to_spawn_list = objs_to_spawn_list
|
||||||
self.width = width
|
self.spawn_area_width = spawn_area_width
|
||||||
self.height = height
|
self.spawn_area_height = spawn_area_height
|
||||||
self.pos_row = pos_row
|
self.spawn_area_pos_row = spawn_area_pos_row
|
||||||
self.pos_column = pos_column
|
self.spawn_area_pos_column = spawn_area_pos_column
|
||||||
self.grid = grid
|
self.grid = grid
|
||||||
|
self.spawn_where_possible = spawn_where_possible
|
||||||
|
|
||||||
def spawn(self):
|
def spawn(self):
|
||||||
coords = []
|
if self.spawn_where_possible:
|
||||||
for row in range(self.pos_row, (self.pos_row + self.width)):
|
possible_fields = self.grid.free_fields
|
||||||
for column in range(self.pos_column, (self.pos_column + self.height)):
|
else:
|
||||||
if self.grid.grid[row][column].texture_path != "water.png" and self.grid.grid[row][column].texture_path != "grass_with_tree.jpg":
|
possible_fields = []
|
||||||
coords.append([(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 7,
|
for field in self.grid.free_fields:
|
||||||
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7])
|
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:
|
for obj in self.objs_to_spawn_list:
|
||||||
random_tile = random.randint(0, len(coords) - 1)
|
ran_index = random.randint(0, len(possible_fields)-1)
|
||||||
obj.rect.x = coords[random_tile][0]
|
ran_field = possible_fields[ran_index]
|
||||||
obj.rect.y = coords[random_tile][1]
|
ran_field.busy = True
|
||||||
coords.pop(random_tile)
|
obj.rect = obj.rect.clamp(ran_field)
|
||||||
obj.update()
|
self.grid.busy_fields.append(ran_field)
|
||||||
|
if ran_field in self.grid.free_fields:
|
||||||
def spawn_monsters(self):
|
self.grid.free_fields.remove(ran_field)
|
||||||
coords = []
|
if ran_field in possible_fields:
|
||||||
for row in range(self.pos_row, (self.pos_row + self.width)):
|
possible_fields.remove(ran_field)
|
||||||
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)
|
|
||||||
obj.update()
|
obj.update()
|
||||||
|
@ -15,4 +15,3 @@ class Knight(pygame.sprite.Sprite):
|
|||||||
self.defense = random.randint(1,4)
|
self.defense = random.randint(1,4)
|
||||||
self.points = 1
|
self.points = 1
|
||||||
|
|
||||||
knights_list = pygame.sprite.Group()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user