def animate_move(self, direction, d=0, r=None, original_row=None,original_column=None): self.moving = True g = constants.grid_size delta = maps.move(direction, 1) PACE = 10 g = g/PACE d+=1 if r is None: r = PACE-1 original_row = self.row original_column = self.column angle = 20 if d % 2 == 1: angle = -20 if 0 == r: angle = 0 self.row+= delta["y"]/PACE self.column+= delta["x"]/PACE self.round_pos(1) self.rotate_sprite(angle) self.canvas.move(self.canvas_sprite,g*delta["x"],g*delta["y"]) print("r="+str(r)) print("row="+str(self.row)) if r > 0: self.app.root.after(int(constants.INTERVAL), lambda: self.animate_move(direction, d=d, r=r-1, original_row = original_row, original_column = original_column)) else: self.update_pos(delta, original_row, original_column)
def move(self, direction): if self.moving == True: return None s = self.app delta = maps.move(direction, 1) g = constants.grid_size square = s.screen.grid[s.tux.row+delta["y"]][s.tux.column + delta["x"]] if square.passable or square.has_bridge[direction.val]: self.animate_move(direction)
def neighbor_has_tux(self, direction): if direction is None: return self.has_tux delta = maps.move(direction, 1) try: neighbor = self.app.screen.grid[self.row + delta["y"]][self.column + delta["x"]] except IndexError: # Neighbor doesn't exist # print("Neighbor doesn't exist") return False return neighbor.has_tux
def move(self, app): g = constants.grid_size not_moved = True d_y = self.row - app.tux.row d_x = self.column - app.tux.column grid = app.screen.grid distance, flee_direction = self.fire_flee_test(app=app) tux_direction = maps.direction_to_target(source=self, target=app.tux) move_directions = [] current_square = grid[self.row][self.column] if len(flee_direction) > 0: move_directions = flee_direction elif len(tux_direction) > 0: move_directions = tux_direction for move_direction in move_directions: if ( self.fire_test(app=app, direction=move_direction, current_fire_distance=distance) and self.moved == False ): self.moved = True if current_square.neighbor_is(direction=move_direction, allowed_types=["grass"]): delta = maps.move(move_direction, 1) grid[self.row][self.column].occupied = False grid[self.row][self.column].has_monster = False self.row += delta["y"] self.column += delta["x"] app.screen.canvas.move(self.sprite,delta["x"] * g, delta["y"] * g) grid[self.row][self.column].occupied = True grid[self.row][self.column].has_monster = True elif current_square.neighbor_has_tux(direction=move_direction): self.sound.play() time.sleep(0.25) print("tux hit!") print("") app.tux.hit(10) else: self.moved = False
def neighbor_is(self, direction=None, allowed_types=["grass"], allowed_features=None, forbidden_types=None, forbidden_features=None, passable=True, occupied=False): if direction is None: return False delta = maps.move(direction, 1) try: neighbor = self.app.screen.grid[self.row + delta["y"]][self.column + delta["x"]] except IndexError: # Neighbor doesn't exist # print("Neighbor doesn't exist") return False # Must be one of the allowed types unless None if allowed_types is not None and neighbor.square_type not in allowed_types: return False # Must not be one of the forbidden types if forbidden_types is not None and neighbor.square_type in forbidden_types: return False # Must have at least one of the allowed features unless None: # If passed as a list, must have all features in the list if allowed_features is not None: for feature in allowed_features: if isinstance(feature, list): required_features = feature else: required_features = [feature] for required_feature in required_features: if neighbor["has_" + required_feature] == False: return False # May not have any of the forbidden features unless None: # If passed as a list, have that specific combination forbidden = True if forbidden_features is not None: for feature in forbidden_features: if isinstance(feature, list): required_features = feature else: required_features = [feature] forbidden = True for required_feature in required_features: v = neighbor["has_" + required_feature] forbidden = forbidden and v if forbidden: return False if passable != neighbor.passable or occupied != neighbor.occupied: return False # Finally return True