Advertisement
Guest User

Untitled

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