Advertisement
cookertron

Pygame grid with coords and random numbers

Dec 23rd, 2021
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. pygame.font.init()
  6.  
  7. BLACK = (0, 0, 0)
  8. WHITE = (255, 255, 255)
  9.  
  10.  
  11. SURFACE_SIZE = (600, 400)
  12. SCREEN = pygame.display.set_mode(SURFACE_SIZE)
  13. FONT = pygame.font.SysFont("arial", size=12)
  14.  
  15. GRID_SIZE = 4
  16. CELL_SIZE = SURFACE_SIZE[1] // GRID_SIZE
  17. LEFT_MARGIN = (SURFACE_SIZE[0] - CELL_SIZE * GRID_SIZE) // 2
  18.  
  19. grid = {(x, y) : random.randint(0, 3) for y in range(GRID_SIZE) for x in range(GRID_SIZE)}
  20.  
  21. quit_demo = False
  22. while not quit_demo:
  23.     events = pygame.event.get()
  24.     for event in events:
  25.         if event.type == pygame.KEYUP:
  26.             if event.key == pygame.K_ESCAPE:
  27.                 quit_demo = True
  28.  
  29.     SCREEN.fill(BLACK)
  30.  
  31.     for indices, number in grid.items():
  32.  
  33.         cell_x = LEFT_MARGIN + indices[0] * CELL_SIZE
  34.         cell_y = indices[1] * CELL_SIZE
  35.  
  36.         pygame.draw.rect(SCREEN, WHITE, (cell_x, cell_y, CELL_SIZE, CELL_SIZE), 1)
  37.  
  38.  
  39.         text_surface = FONT.render("({} , {})".format(indices[0], indices[1]), True, WHITE)
  40.         text_size = text_surface.get_rect().size
  41.         SCREEN.blit(text_surface, (cell_x + CELL_SIZE // 2 - text_size[0] // 2, cell_y + CELL_SIZE // 2 - text_size[1]))
  42.        
  43.         text_surface = FONT.render(str(number), True, WHITE)
  44.         text_size = text_surface.get_rect().size
  45.         SCREEN.blit(text_surface, (cell_x + CELL_SIZE // 2 - text_size[0] // 2, cell_y + CELL_SIZE // 2))
  46.  
  47.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement