From c5c1afaad86f4db6700bd540af9cd5e1999e10bc Mon Sep 17 00:00:00 2001 From: rcoh Date: Tue, 23 Aug 2011 22:11:41 -0700 Subject: Added Pygame renderer and a demo of usage. Also added a renderer for the main lights which is waiting on a layout. --- renderer.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 11 deletions(-) (limited to 'renderer.py') diff --git a/renderer.py b/renderer.py index e264f22..a52bfd7 100644 --- a/renderer.py +++ b/renderer.py @@ -1,17 +1,68 @@ class Renderer(object): + def render_game(self, game_board): + """ + renderBoard + @param gameBoard -- dictionary of tuples of location (x,y), 0 indexed from + the top left of the board. + @param boardIndex -- 0 for the left board, 1 for the right board. + """ + raise NotImplementedError + + def color_deref(self, color_str): + return Color(color_str) + +import pygame +from pygame.locals import Color +class PygameRenderer(Renderer): + """ - renderBoard - @param gameBoard -- dictionary of tuples of location (x,y), 0 indexed from - the top left of the board. - @param boardIndex -- 0 for the left board, 1 for the right board. + Based heavily off of PygameRenderer in SmootLight. Renders Tetris to a + pygame Window. """ - def render_board(gameBoard, boardIndex): - raise NotImplementedError + + DISPLAY_SIZE = (1000,1000) + OFFSET = (100, 100) + SCALE = 10 + def __init__(self): + pygame.init() + self.screen = pygame.display.set_mode(self.DISPLAY_SIZE) + self.background = pygame.Surface(self.screen.get_size()) + self.background = self.background.convert() + self.background.fill(Color(0,0,0)) + + def render_game(self, game_board): + for (x,y) in game_board: + pygame.draw.circle(self.background, self.color_deref(game_board[(x,y)]), + (self.OFFSET[0] + x*self.SCALE, self.OFFSET[1] + y*self.SCALE), self.SCALE) + self.screen.blit(self.background, (0,0)) + pygame.display.flip() + +import util +class LedRenderer(Renderer): """ - renderScore - @param score -- int representing the score of the player - @param board index -- 0 for the left board, 1 for the right board. + Renderer for the LEDs. Based heavily on IndoorRenderer in Smootlight and + general Smootlight abstraction patterns """ - def render_score(score, boardIndex): - raise NotImplementedError + POWER_SUPPLY_IPS = [0,0,0,0] #TODO: Fill in + SOCK_PORT = 6038 + sockets = {} + + def render_game(self, game_board): + packets = self.map_to_packets(game_board) + packets_with_destinations = zip(self.POWER_SUPPLY_IPS, packets) + for (ip, (port, packet)) in packets: + if not ip in self.sockets: + self.sockets[ip] = util.getConnectedSocket(ip, self.SOCK_PORT) + final_packet = util.composePixelStripPacket(packet, port) + try: + self.sockets[ip].send(packet, 0x00) + except: + print 'failure sending packet' + + def map_to_packets(game_board): + """ + Performs the mapping between a game_board and a list of (port,packet) pairs. The port,packet + pairs should line up with the ip's in IP_ADDRESSES + """ + #TODO(rcoh): Right this when we decide on a layout -- cgit v1.2.3