PinmikPanik/gamedata/objects/ingame/tiles.py

42 lines
1.3 KiB
Python
Raw Normal View History

2021-09-11 15:12:53 +02:00
from gamedata.objects.base import BaseObject
from math import sqrt
import random
# Tiles object, displaying and managing the play grid
class Tiles(BaseObject):
def __init__(self,x,y,game,nbcells=20,cellsize=30):
super().__init__(x,y,game,nbcells*cellsize,nbcells*cellsize)
self.nbcells = nbcells
self.cellsize = cellsize
self.grid = self.gengrid(nbcells,nbcells)
def gengrid(self,w,h):
# Generating various rectangles on a map
grid = [ [0 for x in range(w)] for y in range(h) ]
nb_rects = int(sqrt(w*h)/1.5+0.5)
for i in range(nb_rects):
# Generating the rects
rectw = random.randint(3,7)
recth = random.randint(3,7)
rectx = random.randint(0,w-rectw)
recty = random.randint(0,h-recth)
for offx in range(rectw):
for offy in range(recth):
grid[offy+recty][offx+rectx]=1
return grid
def draw(self):
# Drawing the grid
for y in range(len(self.grid)):
for x in range(len(self.grid[y])):
if self.grid[y][x]==1:
self.game.pygame.draw.rect(self.game.window,[255]*3,[self.rect[0]+x*self.cellsize,self.rect[1]+y*self.cellsize,self.cellsize,self.cellsize])