Grid.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. '''
  2. Created on 7 nov. 2016
  3. Game Grid
  4. @author: olinox
  5. '''
  6. from core import bresenham
  7. from core.constants import GRID_GEOMETRIES
  8. class Grid(object):
  9. def __init__(self, geometry, width, height):
  10. self._geometry = geometry
  11. self._width = width
  12. self._height = height
  13. # properties
  14. @property
  15. def geometry(self):
  16. return self._geometry
  17. @geometry.setter
  18. def geometry(self, geometry):
  19. if not geometry in GRID_GEOMETRIES:
  20. raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
  21. self._geometry = geometry
  22. @property
  23. def width(self):
  24. return self._width
  25. @width.setter
  26. def width(self, width):
  27. if not isinstance(width, int) or not width > 0:
  28. raise ValueError("'width' has to be a strictly positive integer")
  29. self._width = width
  30. @property
  31. def height(self):
  32. return self._height
  33. @height.setter
  34. def height(self, height):
  35. if not isinstance(height, int) or not height > 0:
  36. raise ValueError("'width' has to be a strictly positive integer")
  37. self._height = height
  38. # methods
  39. def cases_number(self):
  40. return self.height * self.width
  41. def line(self, *args):
  42. if len(args) == 4:
  43. x1, y1, x2, y2 = args
  44. return bresenham.line2d(self.geometry, x1, y1, x2, y2)
  45. if len(args) == 6:
  46. x1, y1, z1, x2, y2, z2 = args
  47. return bresenham.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
  48. if __name__ == '__main__':
  49. gr = Grid(5, 100, 100)
  50. print(gr.cases_number())
  51. print(gr.line(1,1,5,10))
  52. print(gr.line(1,1,1,5,10,10))