PinmikPanik/gamedata/objects/ingame/tiles.py
2021-09-13 22:16:02 +02:00

65 lines
2.3 KiB
Python

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=22,cellsize=32):
super().__init__(x,y,game,nbcells*cellsize,nbcells*cellsize)
self.nbcells = nbcells
self.cellsize = cellsize
self.saferadius = 2
self.grid = self.gengrid(game.sprite_lib["collisionmap.png"])
self.spawns = self.getspawnpoints(self.grid,saferadius=self.saferadius)
self.debugcell = game.pygame.Surface([cellsize]*2)
self.debugcell.fill([255]*3)
self.debugcell.set_alpha(100)
def gengrid(self,sprite):
# Generating a list of lists based on an image
width,height = sprite.get_size()
grid = []
for y in range(height):
line = []
for x in range(width):
if sprite.get_at((x,y))!=(0,0,0,255):
line.append(1)
else:
line.append(0)
grid.append(line)
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 ground
self.game.window.blit(self.game.sprite_lib["ingame.png"],(0,0))
if self.game.globals["debug"]:
# 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.window.blit(self.debugcell,[self.rect[0]+x*self.cellsize,self.rect[1]+y*self.cellsize])
# Draw spawn points
for point in self.spawns:
self.game.pygame.draw.circle(self.game.window,[100,100,0],point,radius=3)