import pygame import numpy as np class Tilemap: def __init__(self, tileset, mult, size=(30, 20), rect=None): self.size = size self.tileset = tileset self.map = np.zeros(size, dtype=int) w, h = self.size self.image = pygame.Surface((mult*w, mult*h)) if rect: self.rect = pygame.Rect(rect) else: self.rect = self.image.get_rect() def render(self, mult): m, n = self.map.shape for i in range(m): for j in range(n): tile = self.tileset.tiles[self.map[i, j]] self.image.blit(tile, (i*mult, j*mult)) def set_zero(self, mult): self.map = np.zeros(self.size, dtype=int) self.render(mult) def set_random(self, mult): n = len(self.tileset.tiles) self.map = np.random.randint(n, size=self.size) self.render(mult) def __str__(self): return f'{self.__class__.__name__} {self.size}'