spacepaste

  1.  
  2. import pygame, pygame.locals
  3. RED, GREY, LIGHT_GREY = (255, 0, 0), (154, 154, 147), (169, 169, 169)
  4. OFF, ON = 0, 1
  5. def get_grid(orient=OFF):
  6. return [(j*20 - 4, i*20 - 4, orient) for i in range(1, 6) for j in range(1, 8)]
  7. class Board(object):
  8. """ Simulates a display grid of LEDs """
  9. def __init__(self, title, size=(152, 110)):
  10. pygame.init()
  11. self.window = pygame.display.set_mode(size)
  12. pygame.display.set_caption(title)
  13. self.window.fill(LIGHT_GREY)
  14. self.screen = pygame.display.get_surface()
  15. self.grid = get_grid()
  16. self.update_grid()
  17. def update_grid(self):
  18. for coords in self.grid:
  19. pygame.draw.circle(self.window, [GREY, RED][coords[2]], coords[0:2], 8, 0)
  20. pygame.display.update()
  21. def clear(self):
  22. self.grid = get_grid()
  23. self.update_grid()
  24. def fill(self):
  25. self.grid = get_grid(ON)
  26. self.update_grid()
  27. def dot_flip(self, coords):
  28. loc = coords[0]*7 + coords[1]
  29. if coords[0] < 5 and coords[1] < 7 and 0 <= loc < len(self.grid):
  30. self.grid[loc] = self.grid[loc][0:2] + ([ON, OFF][self.grid[loc][2]],)
  31. self.update_grid()
  32.