def __init__(self, width, height, chain, debug=False, gui=False, copy=False): self.chain = chain self.debug = debug self.width = width self.height = height if not copy: self.chain.length = self.width * self.height self.gui = None if gui: self.gui = Wall_Visualizer(self.width, self.height) else: self.gui = gui
class Wall(object): def __init__(self, width, height, chain, debug=False, gui=False, copy=False): self.chain = chain self.debug = debug self.width = width self.height = height if not copy: self.chain.length = self.width * self.height self.gui = None if gui: self.gui = Wall_Visualizer(self.width, self.height) else: self.gui = gui def clear(self, draw=False): self.chain.clear(draw) def ascii(self): txt = '' for y in range(self.height): for x in range(self.width): txt += self.pixel(x, y).ascii() txt += '\n' return txt def draw(self): self.chain.draw() if self.gui: self.gui.draw(self) def copy(self, clear=False): chain2 = self.chain.copy(clear) return self.__class__(self.width, self.height, chain2, self.debug, self.gui, True) def __len__(self): return len(self.chain) def __iter__(self): return iter(self.chain) def __getitem__(self, key): return self.chain[key] def pixel(self, x, y, debug=False): """ Turn x and y indices into a matrix into the corresponding pixel in the linear chain that actually describes the lights. x: integer y: integer Returns: the ShiftBright at that location The assumed light configuration is a snake, ie: 1 -- 2 -- 3 | 6 -- 5 -- 4 | 7 -- 8 -- 9 """ if debug: print "==>", x, y if x < 0 or x > self.width - 1 or y < 0 or y > self.height - 1: return None # Get the length of the snake corresponding to the full-width rectangle # that sits above the pixel. Then add the remaining x length. This gives # you the index into the physical light chain. # # For example, if this is your matrix: # # O O O # O O O # O X O # O O O # # If X is the pixel we are dealing with, we were given x = 1, y = 2. The # length of the snake corresponding to the full-width rectangle that # sits above the pixel is the area of the * rectangle, minus 1: # # * * * 0 1 2 # * * * 5 4 3 # O X O ===> == (3 x 2 - 1) # O O O # # To finish the length of the snake, we need to know which direction the # snake is going to get the remaining x length. In this example, the # snake is curving right, so we get: # # 0 1 2 # 5 4 3 # 6 7 # rectangle = self.width * y - 1 if y % 2: return self.chain[rectangle + x + 1] else: return self.chain[rectangle + self.width - x]