forked from ayte/PinmikPanik
65 lines
2.5 KiB
Python
65 lines
2.5 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"],(-self.game.globals["camerax"],-self.game.globals["cameray"]))
|
|
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.game.globals["camerax"],self.rect[1]+y*self.cellsize-self.game.globals["cameray"]])
|
|
# Draw spawn points
|
|
for point in self.spawns:
|
|
self.game.pygame.draw.circle(self.game.window,[100,100,0],[point[0]-self.game.globals["camerax"],point[1]-self.game.globals["cameray"]],radius=3)
|