- import pygame, pygame.locals
- RED, GREY, LIGHT_GREY = (255, 0, 0), (154, 154, 147), (169, 169, 169)
- OFF, ON = 0, 1
- def get_grid(orient=OFF):
- return [(j*20 - 4, i*20 - 4, orient) for i in range(1, 6) for j in range(1, 8)]
- class Board(object):
- """ Simulates a display grid of LEDs """
- def __init__(self, title, size=(152, 110)):
- pygame.init()
- self.window = pygame.display.set_mode(size)
- pygame.display.set_caption(title)
- self.window.fill(LIGHT_GREY)
- self.screen = pygame.display.get_surface()
- self.grid = get_grid()
- self.update_grid()
- def update_grid(self):
- for coords in self.grid:
- pygame.draw.circle(self.window, [GREY, RED][coords[2]], coords[0:2], 8, 0)
- pygame.display.update()
- def clear(self):
- self.grid = get_grid()
- self.update_grid()
- def fill(self):
- self.grid = get_grid(ON)
- self.update_grid()
- def dot_flip(self, coords):
- loc = coords[0]*7 + coords[1]
- if coords[0] < 5 and coords[1] < 7 and 0 <= loc < len(self.grid):
- self.grid[loc] = self.grid[loc][0:2] + ([ON, OFF][self.grid[loc][2]],)
- self.update_grid()