Advertisement
pytheous

Untitled

Oct 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1.  
  2. find_neighbours(world) -> list -- finds all surrounding tiles and puts them in a list
  3.  
  4. :param grid: the grid that the tile is in
  5. :return: the 8 tiles surrounding self.pos
  6. :rtype: list
  7. """
  8. neighbours = []
  9.  
  10. surroundingPoints = self._getSurroundingPoints()
  11.  
  12. for point in surroundingPoints:
  13. if Tile(point).pos in grid.getGridPositions():
  14. neighbours.append(point)
  15.  
  16. # for tile in grid.world:
  17. # t = tile[2].pos
  18. # if t[0] == self.pos[0] - 1 and t[1] == self.pos[1] + 1: # south-west
  19. # neighbours.append(tile)
  20. # elif t[0] == self.pos[0] + 0 and t[1] == self.pos[1] + 1: # south
  21. # neighbours.append(tile)
  22. # elif t[0] == self.pos[0] + 1 and t[1] == self.pos[1] + 1: # south-east
  23. # neighbours.append(tile)
  24. # elif t[0] == self.pos[0] + 1 and t[1] == self.pos[1]: # east
  25. # neighbours.append(tile)
  26. # elif t[0] == self.pos[0] + 1 and t[0] == self.pos[0] - 1: # north-east
  27. # neighbours.append(tile)
  28. # elif t[0] == self.pos[0] and t[1] == self.pos[1] - 1: # north
  29. # neighbours.append(tile)
  30. # elif t[0] == self.pos[0] - 1 and t[1] == self.pos[1] - 1: # north-west
  31. # neighbours.append(tile)
  32. # elif t[0] == self.pos[0] - 1 and t[1] == self.pos[1]: # west
  33. # neighbours.append(tile)
  34. return neighbours
  35.  
  36. def _getSurroundingPoints(self):
  37. """returns a namedtuple of 8 surrounding cords by location
  38. ie. north, northwest,west,southwest,south,southeast,east"""
  39.  
  40. surrounding = namedtuple('surround',['north','northwest','west','southwest','south','southeast','east','northeast'])
  41. return surrounding(
  42. north=self.POS(self.pos.x,self.pos.y+1),
  43. northwest=self.POS(self.pos.x-1,self.pos.y+1),
  44. west=self.POS(self.pos.x-1,self.pos.y),
  45. southwest=self.POS(self.pos.x-1,self.pos.y-1),
  46. south=self.POS(self.pos.x,self.pos.y),
  47. southeast=self.POS(self.pos.x+1,self.pos.y-1),
  48. east=self.POS(self.pos.x+1,self.pos.y),
  49. northeast=self.POS(self.pos.x+1,self.pos.y+1)
  50. )
  51.  
  52.  
  53. class Grid:
  54. def __init__(self, height, width):
  55. self.height = height
  56. self.width = width
  57. self.world = tuple()
  58.  
  59. def print(self):
  60.  
  61. msg=""
  62. for x in self.world:
  63. msg+='0'
  64. if x.pos.y >= self.height -1:
  65. print(msg)
  66. msg=""
  67.  
  68. def generate(self):
  69. """Generates a 2d grid containing the params Height, Width and a Tile object
  70.  
  71. generate() -> list
  72.  
  73. """
  74. 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
  75. self.world = grid
  76.  
  77. def getGridPositions(self):
  78. return tuple(tile.pos for tile in self.world)
  79.  
  80.  
  81. my_grid = Grid(5, 10) # larger grids massively increase the time required for find_neighbours()
  82. my_grid.generate()
  83. # for tile in my_grid.grid:
  84. # tile[2].neighbours.append(tile.find_neighbours())
  85. # my_grid.world[50].find_neighbours(my_grid)
  86.  
  87. # for tile in my_grid.world:
  88. # print(tile.find_neighbours(my_grid))
  89. my_grid.print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement