Madra_smieciarka/classes/Tilemap.py

36 lines
944 B
Python
Raw Normal View History

2024-03-25 00:05:56 +01:00
import pygame
import numpy as np
class Tilemap:
def __init__(self, tileset, size=(30, 20), rect=None):
self.size = size
self.tileset = tileset
self.map = np.zeros(size, dtype=int)
2024-03-25 11:21:23 +01:00
w, h = self.size
2024-03-25 00:05:56 +01:00
self.image = pygame.Surface((32*w, 32*h))
if rect:
self.rect = pygame.Rect(rect)
else:
self.rect = self.image.get_rect()
def render(self):
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*32, j*32))
def set_zero(self):
self.map = np.zeros(self.size, dtype=int)
self.render()
def set_random(self):
n = len(self.tileset.tiles)
self.map = np.random.randint(n, size=self.size)
self.render()
def __str__(self):
return f'{self.__class__.__name__} {self.size}'