Madra_smieciarka/classes/Tilemap.py

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