Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- pygame.init()
- pygame.font.init()
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- SURFACE_SIZE = (600, 400)
- SCREEN = pygame.display.set_mode(SURFACE_SIZE)
- FONT = pygame.font.SysFont("arial", size=12)
- GRID_SIZE = 4
- CELL_SIZE = SURFACE_SIZE[1] // GRID_SIZE
- LEFT_MARGIN = (SURFACE_SIZE[0] - CELL_SIZE * GRID_SIZE) // 2
- grid = {(x, y) : random.randint(0, 3) for y in range(GRID_SIZE) for x in range(GRID_SIZE)}
- quit_demo = False
- while not quit_demo:
- events = pygame.event.get()
- for event in events:
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_ESCAPE:
- quit_demo = True
- SCREEN.fill(BLACK)
- for indices, number in grid.items():
- cell_x = LEFT_MARGIN + indices[0] * CELL_SIZE
- cell_y = indices[1] * CELL_SIZE
- pygame.draw.rect(SCREEN, WHITE, (cell_x, cell_y, CELL_SIZE, CELL_SIZE), 1)
- text_surface = FONT.render("({} , {})".format(indices[0], indices[1]), True, WHITE)
- text_size = text_surface.get_rect().size
- SCREEN.blit(text_surface, (cell_x + CELL_SIZE // 2 - text_size[0] // 2, cell_y + CELL_SIZE // 2 - text_size[1]))
- text_surface = FONT.render(str(number), True, WHITE)
- text_size = text_surface.get_rect().size
- SCREEN.blit(text_surface, (cell_x + CELL_SIZE // 2 - text_size[0] // 2, cell_y + CELL_SIZE // 2))
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement