def valid_placement(board, block, surface_coords): width, height = len(block.value[0]), len(block.value) results = [] #need to consider multiple positions to left for rotation_index in range(4): for offset_x in range(width)[::-1]: test_block = Block.copy_block(block) test_block.rotate_to(rotation_index) test_block.x = surface_coords[0] - offset_x test_block.y = surface_coords[1] - height #True if any of the blocks are above starting_row above_starting_row = any([y < 0 for x, y in test_block.get_coords()]) #sometimes blocks will be above while above_starting_row: test_block.y += 1 above_starting_row = any([y < 0 for x, y in test_block.get_coords()]) coords = valid_placement_helper(board, test_block) if coords: results.append(test_block) return results
def drop_helper(board, block): if block == None: return False #we first test to see if there will be a collision if block moves down test_block = Block.copy_block(block) test_block.y += 1 return not detect_collisions(board, test_block)
def rotate(self): if self.game_running: test_block = Block.copy_block(self.block) #try to rotate right test_block.rotate_right() #if there is no collision detected, rotate actual block if not detect_collisions(self.board, test_block): self.block.rotate_right()
def move(self, amt): if self.game_running: #we first test to see if there will be a collision if block moves down #by using a test_block test_block = Block.copy_block(self.block) test_block.x += amt #checks that ensure block does not go out of screen if test_block.x < test_block.buffer[0]: self.block.x = self.block.buffer[0] elif test_block.x > cols - test_block.length + test_block.buffer[1]: self.block.x = cols - self.block.length + self.block.buffer[1] #if there is a collision, then apply the movement to actual block if not detect_collisions(self.board, test_block): self.block.x += amt
def valid_placement(board, block, surface_coords): width, height = len(block.value[0]), len(block.value) results = [] #need to consider multiple positions to left for rotation_index in range(4): for offset_x in range(width)[::-1]: test_block = Block.copy_block(block) test_block.rotate_to(rotation_index) test_block.x = surface_coords[0] - offset_x test_block.y = surface_coords[1] - height coords = valid_placement_helper(board, test_block) if coords: results.append(test_block) return results