36 lines
944 B
Python
36 lines
944 B
Python
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)
|
|
w, h = self.size
|
|
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}'
|