Advertisement
makispaiktis

Draw Grids

Jun 10th, 2021 (edited)
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import sys
  2. import pygame
  3. from pygame.locals import KEYDOWN, K_q
  4. import numpy as np
  5.  
  6. # CONSTANTS:
  7. SCREENSIZE = WIDTH, HEIGHT = 1000, 1000
  8. BLACK = (0, 0, 0)
  9. GREY = (255, 255, 255)
  10.  
  11. # OUR GRID MAP:
  12. cellMAP = np.random.randint(2, size=(10, 10))
  13.  
  14. # print(cellMAP)
  15. # [[1 0 1 0 0 0 1 0 1 0]
  16. #  [0 0 0 1 0 0 0 1 1 0]
  17. #  [0 0 1 1 0 0 1 1 1 0]
  18. #  [0 0 1 0 0 0 1 0 0 1]
  19. #  [0 1 1 0 0 0 0 1 1 0]
  20. #  [0 1 0 0 1 0 0 1 1 1]
  21. #  [0 1 0 0 1 1 1 1 0 1]
  22. #  [1 0 1 0 1 1 1 1 0 1]
  23. #  [0 1 0 1 0 0 0 1 0 0]
  24. #  [0 1 1 1 0 1 1 0 0 1]]
  25.  
  26. _VARS = {'surf': False, 'gridWH': 400,
  27.          'gridOrigin': (200, 100), 'gridCells': cellMAP.shape[0], 'lineWidth': 2}
  28.  
  29.  
  30. def main():
  31.     pygame.init()
  32.     _VARS['surf'] = pygame.display.set_mode(SCREENSIZE)
  33.     while True:
  34.         checkEvents()
  35.         _VARS['surf'].fill(GREY)
  36.         drawSquareGrid(
  37.             _VARS['gridOrigin'], _VARS['gridWH'], _VARS['gridCells'])
  38.         placeCells()
  39.         pygame.display.update()
  40.  
  41.  
  42. # NEW METHOD FOR ADDING CELLS :
  43. def placeCells():
  44.     # GET CELL DIMENSIONS...
  45.     cellBorder = 6
  46.     celldimX = celldimY = (_VARS['gridWH']/_VARS['gridCells']) - (cellBorder*2)
  47.     # DOUBLE LOOP
  48.     for row in range(cellMAP.shape[0]):
  49.         for column in range(cellMAP.shape[1]):
  50.             # Is the grid cell tiled ?
  51.             if(cellMAP[column][row] == 1):
  52.                 drawSquareCell(
  53.                     _VARS['gridOrigin'][0] + (celldimY*row)
  54.                     + cellBorder + (2*row*cellBorder) + _VARS['lineWidth']/2,
  55.                     _VARS['gridOrigin'][1] + (celldimX*column)
  56.                     + cellBorder + (2*column*cellBorder) + _VARS['lineWidth']/2,
  57.                     celldimX, celldimY)
  58.  
  59. # Draw filled rectangle at coordinates
  60. def drawSquareCell(x, y, dimX, dimY):
  61.     pygame.draw.rect(
  62.         _VARS['surf'], BLACK,
  63.         (x, y, dimX, dimY)
  64.     )
  65.  
  66.  
  67. def drawSquareGrid(origin, gridWH, cells):
  68.  
  69.     CONTAINER_WIDTH_HEIGHT = gridWH
  70.     cont_x, cont_y = origin
  71.  
  72.     # DRAW Grid Border:
  73.     # TOP lEFT TO RIGHT
  74.     pygame.draw.line(
  75.         _VARS['surf'], BLACK,
  76.         (cont_x, cont_y),
  77.         (CONTAINER_WIDTH_HEIGHT + cont_x, cont_y), _VARS['lineWidth'])
  78.     # # BOTTOM lEFT TO RIGHT
  79.     pygame.draw.line(
  80.         _VARS['surf'], BLACK,
  81.         (cont_x, CONTAINER_WIDTH_HEIGHT + cont_y),
  82.         (CONTAINER_WIDTH_HEIGHT + cont_x,
  83.          CONTAINER_WIDTH_HEIGHT + cont_y), _VARS['lineWidth'])
  84.     # # LEFT TOP TO BOTTOM
  85.     pygame.draw.line(
  86.         _VARS['surf'], BLACK,
  87.         (cont_x, cont_y),
  88.         (cont_x, cont_y + CONTAINER_WIDTH_HEIGHT), _VARS['lineWidth'])
  89.     # # RIGHT TOP TO BOTTOM
  90.     pygame.draw.line(
  91.         _VARS['surf'], BLACK,
  92.         (CONTAINER_WIDTH_HEIGHT + cont_x, cont_y),
  93.         (CONTAINER_WIDTH_HEIGHT + cont_x,
  94.          CONTAINER_WIDTH_HEIGHT + cont_y), _VARS['lineWidth'])
  95.  
  96.     # Get cell size, just one since its a square grid.
  97.     cellSize = CONTAINER_WIDTH_HEIGHT/cells
  98.  
  99.     # VERTICAL DIVISIONS: (0,1,2) for grid(3) for example
  100.     for x in range(cells):
  101.         pygame.draw.line(
  102.             _VARS['surf'], BLACK,
  103.             (cont_x + (cellSize * x), cont_y),
  104.             (cont_x + (cellSize * x), CONTAINER_WIDTH_HEIGHT + cont_y), 2)
  105.         # # HORIZONTAl DIVISIONS
  106.         pygame.draw.line(
  107.             _VARS['surf'], BLACK,
  108.             (cont_x, cont_y + (cellSize*x)),
  109.             (cont_x + CONTAINER_WIDTH_HEIGHT, cont_y + (cellSize*x)), 2)
  110.  
  111.  
  112. def checkEvents():
  113.     for event in pygame.event.get():
  114.         if event.type == pygame.QUIT:
  115.             sys.exit()
  116.         elif event.type == KEYDOWN and event.key == K_q:
  117.             pygame.quit()
  118.             sys.exit()
  119.  
  120.  
  121. if __name__ == '__main__':
  122.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement