class Piece(object): def __init__(self, x, y, width, height): self.grid = [] self.pos = Point(x, y) self.origin = Point(0, 0) self.size = Dimension(width, height) self.rotState = rotState() self.tranState = tranState() # Set the grid values. def set(self, grid): self.grid = grid self.origin = Point(self.left(), self.top()) # Rotate piece grid counter clockwise def rotate_left(self): rotated = zip(*self.grid)[::-1] self.set(rotated) self.size = self.size.rotate() self.rotState.rot(1) # print self.rotState.r # Rotate piece grid clockwise def rotate_right(self): rotated = zip(*self.grid[::-1]) self.set(rotated) self.size = self.size.rotate() self.rotState.rot(-1) # Return the first non-empty row def top(self): for y in xrange(len(self.grid)): if len([x for x in xrange(len(self.grid[y])) if self.grid[y][x]]): return y return 0 # Return the first non-empty column def left(self): left = 5 for y in xrange(len(self.grid)): for x in xrange(len(self.grid[y])): if self.grid[y][x] and x < left: left = x return left def copy(self): new_piece = Piece(self.pos.x, self.pos.y, self.size.width, self.size.height) rows = len(self.grid) cols = len(self.grid[0]) new_piece.grid = [[self.grid[y][x] for x in xrange(cols)] for y in xrange(rows)] return new_piece
class Piece(object): def __init__(self, x, y, width, height): self.grid = [] self.pos = Point(x, y) self.origin = Point(0, 0) self.size = Dimension(width, height) # Set the grid values. def set(self, grid): self.grid = grid self.origin = Point(self.left(), self.top()) # Rotate piece grid counter clockwise def rotate_left(self): rotated = zip(*self.grid)[::-1] self.set(rotated) self.size = self.size.rotate() # Rotate piece grid clockwise def rotate_right(self): rotated = zip(*self.grid[::-1]) self.set(rotated) self.size = self.size.rotate() # Return the first non-empty row def top(self): for y in xrange(len(self.grid)): if len([x for x in xrange(len(self.grid[y])) if self.grid[y][x]]): return y return 0 # Return the first non-empty column def left(self): left = 5 for y in xrange(len(self.grid)): for x in xrange(len(self.grid[y])): if self.grid[y][x] and x < left: left = x return left