WMICraft/grid.py

36 lines
1.5 KiB
Python
Raw Normal View History

2022-03-04 20:57:34 +01:00
import pygame
import random
from field import Field
2022-03-08 12:11:22 +01:00
from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH
2022-03-04 20:57:34 +01:00
class Grid:
def __init__(self, textures):
self.textures = textures
self.grid = []
for row in range(ROWS):
self.grid.append([])
for _ in range(COLUMNS):
texture_path, converted_texture = self.get_random_texture()
field = Field(texture_path, converted_texture)
self.grid[row].append(field)
def get_random_texture(self):
texture_index = random.randint(0, len(self.textures) - 1)
return self.textures[texture_index]
def draw(self, screen):
2022-03-08 12:11:22 +01:00
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(10, 8, bg_width, bg_height))
2022-03-04 20:57:34 +01:00
for row in range(ROWS):
for column in range(COLUMNS):
2022-03-08 12:11:22 +01:00
box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 15,
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 13,
2022-03-04 20:57:34 +01:00
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)