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) self.spawns = self.getspawnpoints(self.grid) 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 getspawnpoints(self,grid,saferadius=2): spawns = [] for y in range(saferadius,len(grid)-saferadius): for x in range(saferadius,len(grid[y])-saferadius): canspawn = True for safey in range(-saferadius,saferadius+1): for safex in range(-saferadius,saferadius+1): if grid[y+safey][x+safex]==0: # If a pit is inside the radius, can't spawn here canspawn = False if canspawn: spawns.append([self.rect[0]+(x+0.5)*self.cellsize,self.rect[1]+(y+0.5)*self.cellsize]) return spawns 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]) # Draw spawn points if self.game.globals["debug"]: for point in self.spawns: self.game.pygame.draw.circle(self.game.window,[100,100,0],point,radius=3)