def create_new_asteroid(self, new_size, torpedo, asteroid, reverse=False): """ Creates two sub_asteroids of a given asteroid that was hit by a torpedo :param new_size: the size of the new sub-asteroids :param torpedo: the torpedo that damaged the asteroid :param asteroid: the old asteroid that was hit by a torpedo :param reverse: Boolean value, checking if the new generated direction should be reversed """ new_ast = Asteroid(new_size, self.screen_max_x, self.screen_max_y, self.screen_min_x, self.screen_min_y) self.asteroids_list.append(new_ast) # Assigning new speed according to the asteroid's and torpedo's # old speed new_ast.gen_speed_x(torpedo.get_speed_x(), asteroid.get_speed_x()) new_ast.gen_speed_y(torpedo.get_speed_y(), asteroid.get_speed_y()) # Assigning the position of the dead asteroid to the sub-asteroid new_ast.force_pos_x(asteroid.get_pos_x()) new_ast.force_pos_y(asteroid.get_pos_y()) if reverse: new_ast.reverse_direction() self._screen.register_asteroid(new_ast, new_size) self._screen.draw_asteroid(new_ast, new_ast.get_pos_x(), new_ast.get_pos_y())
def split_asteroid(self, asteroid: Asteroid, torpedo: Torpedo): """if torpedo hit asteroid (in size more then 1) the function split the asteroid to two new smaller asteroids. the new asteroid move in opposite directions and the size of them is one unit less (from the original asteroid). the function remove the original asteroid and the torpedo that hit him!""" # the formula given in ex.pdf sum_speed_x = torpedo.get_speed_x() + asteroid.get_speed_x() sum_speed_y = torpedo.get_speed_y() + asteroid.get_speed_y() current_x_and_y_speed = math.pow(asteroid.get_speed_x(), 2) \ + math.pow(asteroid.get_speed_y(), 2) for i in range(2): # create two new asteroid (split the original asteroid) new_asteroid = Asteroid(asteroid.get_size() - ONE_UNIT_LESS) new_asteroid.set_new_speed_x(sum_speed_x / math.sqrt(current_x_and_y_speed)) new_asteroid.set_new_speed_y(sum_speed_y / math.sqrt(current_x_and_y_speed)) new_asteroid.set_new_pos_in_x(asteroid.get_pos_x()) new_asteroid.set_new_pos_in_y(asteroid.get_pos_y()) # the size of the new asteroids is smaller by one unit self._screen.register_asteroid(new_asteroid, asteroid.get_size() - ONE_UNIT_LESS) self.__asteroids.append(new_asteroid) if i == 1: # change the movement of one of the asteroids to the # opposite direction new_asteroid.set_new_speed_x(new_asteroid.get_speed_x() * OPPOSITE) new_asteroid.set_new_speed_x(new_asteroid.get_speed_y() * OPPOSITE) # remove the original asteroid after we split him self._screen.unregister_asteroid(asteroid) self.__asteroids.remove(asteroid) # remove the torpedo after he hit the asteroid self._screen.unregister_torpedo(torpedo) self.__torpedos.remove(torpedo)