Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import product
- from collections import namedtuple
- class Tile:
- POS = namedtuple('pos', ['x','y'])
- def __init__(self, pos:tuple):
- """
- :param x: x position
- :param y: y position
- :param grid:
- :type x: int
- :type y: int
- """
- self.pos = self.POS(pos[0],pos[1])
- #self.grid = grid
- self.neighbours = []
- def find_neighbours(self, grid):
- """
- find_neighbours(world) -> list -- finds all surrounding tiles and puts them in a list
- :param grid: the grid that the tile is in
- :return: the 8 tiles surrounding self.pos
- :rtype: list
- """
- has_printed_percent = False
- if has_printed_percent is False:
- print(str(round(self.pos[1] / grid.height * 100)) + "%")
- neighbours = []
- surroundingPoints = self._getSurroundingPoints()
- for point in surroundingPoints:
- if Tile(point).pos in grid.getGridPositions():
- neighbours.append(point)
- # for tile in grid.world:
- # t = tile[2].pos
- # if t[0] == self.pos[0] - 1 and t[1] == self.pos[1] + 1: # south-west
- # neighbours.append(tile)
- # elif t[0] == self.pos[0] + 0 and t[1] == self.pos[1] + 1: # south
- # neighbours.append(tile)
- # elif t[0] == self.pos[0] + 1 and t[1] == self.pos[1] + 1: # south-east
- # neighbours.append(tile)
- # elif t[0] == self.pos[0] + 1 and t[1] == self.pos[1]: # east
- # neighbours.append(tile)
- # elif t[0] == self.pos[0] + 1 and t[0] == self.pos[0] - 1: # north-east
- # neighbours.append(tile)
- # elif t[0] == self.pos[0] and t[1] == self.pos[1] - 1: # north
- # neighbours.append(tile)
- # elif t[0] == self.pos[0] - 1 and t[1] == self.pos[1] - 1: # north-west
- # neighbours.append(tile)
- # elif t[0] == self.pos[0] - 1 and t[1] == self.pos[1]: # west
- # neighbours.append(tile)
- return neighbours
- def _getSurroundingPoints(self):
- """returns a namedtuple of 8 surrounding cords by location
- ie. north, northwest,west,southwest,south,southeast,east"""
- surrounding = namedtuple('surround',['north','northwest','west','southwest','south','southeast','east','northeast'])
- return surrounding(
- north=self.POS(self.pos.x,self.pos.y+1),
- northwest=self.POS(self.pos.x-1,self.pos.y+1),
- west=self.POS(self.pos.x-1,self.pos.y),
- southwest=self.POS(self.pos.x-1,self.pos.y-1),
- south=self.POS(self.pos.x,self.pos.y),
- southeast=self.POS(self.pos.x+1,self.pos.y-1),
- east=self.POS(self.pos.x+1,self.pos.y),
- northeast=self.POS(self.pos.x+1,self.pos.y+1)
- )
- class Grid:
- def __init__(self, height, width):
- self.height = height
- self.width = width
- self.world = tuple()
- def generate(self):
- """Generates a 2d grid containing the params Height, Width and a Tile object
- generate() -> list
- """
- grid = tuple(Tile(x) for x in tuple(product(range(self.width), range(self.height)))) # could probably use double list comprehensions, slap in the reverse() there
- self.world = grid
- def getGridPositions(self):
- return tuple(tile.pos for tile in self.world)
- my_grid = Grid(30, 80) # larger grids massively increase the time required for find_neighbours()
- my_grid.generate()
- # for tile in my_grid.grid:
- # tile[2].neighbours.append(tile.find_neighbours())
- my_grid.world[5].find_neighbours(my_grid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement