2022-03-09 16:59:58 +01:00
|
|
|
import random
|
2022-04-06 21:08:32 +02:00
|
|
|
import numpy as np
|
2022-03-11 19:42:17 +01:00
|
|
|
import pygame
|
|
|
|
|
|
|
|
from common.constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, \
|
2022-04-06 21:08:32 +02:00
|
|
|
BORDER_RADIUS, NBR_OF_TREES, NBR_OF_WATER
|
2022-04-08 17:15:01 +02:00
|
|
|
from .exceptions import FieldNotWalkable
|
2022-03-11 19:42:17 +01:00
|
|
|
from .field import Field
|
2022-03-09 16:59:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Grid:
|
|
|
|
def __init__(self, textures):
|
|
|
|
self.textures = textures
|
|
|
|
self.grid = []
|
2022-04-05 10:47:45 +02:00
|
|
|
self.free_fields = []
|
|
|
|
self.busy_fields = []
|
2022-04-06 21:08:32 +02:00
|
|
|
podkladka = np.zeros((ROWS, COLUMNS))
|
|
|
|
for drzewa in range(0, NBR_OF_TREES):
|
2022-04-08 17:15:01 +02:00
|
|
|
rzad = random.randint(0, ROWS - 1)
|
|
|
|
kolumna = random.randint(0, COLUMNS - 1)
|
2022-04-06 21:08:32 +02:00
|
|
|
if podkladka[rzad][kolumna] == 0:
|
|
|
|
podkladka[rzad][kolumna] = 1
|
|
|
|
else:
|
|
|
|
drzewa = drzewa - 1
|
|
|
|
for i in range(0, NBR_OF_WATER):
|
2022-04-08 17:15:01 +02:00
|
|
|
rzad = random.randint(0, ROWS - 1)
|
|
|
|
kolumna = random.randint(0, COLUMNS - 1)
|
2022-04-06 21:08:32 +02:00
|
|
|
if podkladka[rzad][kolumna] == 0:
|
|
|
|
podkladka[rzad][kolumna] = 2
|
|
|
|
else:
|
|
|
|
drzewa = drzewa - 1
|
2022-04-08 11:24:03 +02:00
|
|
|
|
2022-03-09 16:59:58 +01:00
|
|
|
for row in range(ROWS):
|
2022-04-08 11:24:03 +02:00
|
|
|
self.grid.append([])
|
2022-04-05 10:47:45 +02:00
|
|
|
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]
|
2022-04-06 21:08:32 +02:00
|
|
|
if podkladka[row][column] == 1:
|
|
|
|
texture_path, converted_texture = self.textures[-1]
|
|
|
|
elif podkladka[row][column] == 2:
|
|
|
|
texture_path, converted_texture = self.textures[-2]
|
|
|
|
else:
|
|
|
|
texture_path, converted_texture = self.get_random_texture()
|
2022-04-05 10:47:45 +02:00
|
|
|
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)
|
|
|
|
|
2022-04-08 11:24:03 +02:00
|
|
|
self.grid[row].append(pygame.sprite.Group(field))
|
2022-04-05 10:47:45 +02:00
|
|
|
|
|
|
|
def update(self, screen):
|
|
|
|
self.draw(screen)
|
2022-03-09 16:59:58 +01:00
|
|
|
|
|
|
|
def get_random_texture(self):
|
2022-04-06 21:08:32 +02:00
|
|
|
texture_index = random.randint(0, len(self.textures) - 3)
|
2022-03-09 16:59:58 +01:00
|
|
|
return self.textures[texture_index]
|
|
|
|
|
2022-04-08 17:15:01 +02:00
|
|
|
def get_tile(self, row, column):
|
|
|
|
return pygame.sprite.Group.sprites(self.grid[row][column])[0] # iteruj kolumny i wiersze od 0
|
|
|
|
|
|
|
|
def put_on_tile(self, row, column, obj): # iteruj kolumny i wiersze od 0
|
|
|
|
try:
|
|
|
|
self.get_tile(row, column).put_on(obj)
|
|
|
|
except FieldNotWalkable:
|
|
|
|
print("Field busy")
|
|
|
|
|
2022-03-09 16:59:58 +01:00
|
|
|
def draw(self, screen):
|
|
|
|
bg_width = (GRID_CELL_PADDING + GRID_CELL_WIDTH) * COLUMNS + 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)
|
|
|
|
|
2022-04-05 10:47:45 +02:00
|
|
|
for fields_row in self.grid:
|
2022-04-08 11:24:03 +02:00
|
|
|
for fields in fields_row:
|
|
|
|
fields.draw(screen)
|