def _create_fractures(self, asteroid, torpedo, asteroid_size): """ :param asteroid: an Asteroid object :param torpedo: a Torpedo object :param asteroid_size: the size of the asteroid :return: The function splits the given asteroid by creating 2 smaller asteroids, drawing them to the screen. The velocity of the smaller asteroids is calculated according to the formula. """ fracture1 = Asteroid() fracture1.set_size(asteroid_size) fracture2 = Asteroid() fracture2.set_size(asteroid_size) fracture1.set_x_position(asteroid.get_x_position()) fracture1.set_y_position(asteroid.get_y_position()) fracture2.set_x_position(asteroid.get_x_position()) fracture2.set_y_position(asteroid.get_y_position()) self._set_collision_velocity(asteroid, torpedo, fracture1) self._set_collision_velocity(asteroid, torpedo, fracture2) fracture2.set_x_velocity(COLLISION_CONSTANT * fracture2.get_x_velocity()) fracture2.set_y_velocity(COLLISION_CONSTANT * fracture2.get_y_velocity()) self._screen.unregister_asteroid(asteroid) self._asteroids.remove(asteroid) self._asteroids.append(fracture1) self._asteroids.append(fracture2) self._screen.register_asteroid(fracture1, fracture1.get_size()) self._screen.register_asteroid(fracture2, fracture2.get_size())
def __split_asteroid(self, ast, torp): """ Method splits the beaten asteroid :param ast: object of type Asteroid - asteroid that we gonna split :param torp: object of type Torpedo - torpedo that beat the asteroid """ # unregister the beaten asteroid self._screen.unregister_asteroid(ast) self.ast_lst.remove(ast) new_ast_speed_x = (torp.get_speed_x() + ast.get_speed_x()) / \ m.sqrt(ast.get_speed_x()**2 + ast.get_speed_y()**2) new_ast_speed_y = (torp.get_speed_y() + ast.get_speed_y()) / \ m.sqrt(ast.get_speed_x()**2 + ast.get_speed_y()**2) if ast.get_size() == self.MIN_ASTEROID_SIZE: return # the smallest asteroid isn't given to splitting else: # the size is bigger then minimal, split asteroid # to two smaller asteroids by size smaller by 1 size = ast.get_size() - 1 # creating two new asteroids new_ast_1 = Asteroid(ast.get_loc_x(), new_ast_speed_x, ast.get_loc_y(), new_ast_speed_y, size) new_ast_2 = Asteroid(ast.get_loc_x(), -new_ast_speed_x, ast.get_loc_y(), -new_ast_speed_y, size) self._screen.register_asteroid(new_ast_1, new_ast_1.get_size()) self._screen.register_asteroid(new_ast_2, new_ast_2.get_size()) self.ast_lst.append(new_ast_1) self.ast_lst.append(new_ast_2)
def __split_asteroid(self, asteroid): """The function handles the split of an asteroid upon hit""" location = asteroid.get_location() # T velocity = asteroid.get_velocity() new_size = asteroid.get_size() - 1 if asteroid.get_size() > 1: # As long as the size is greater than 1 if asteroid.get_size() == 3: # Case for asteroid of size 3 (MAX) self.__points += 20 # adds 20 points self.__screen.set_score(self.__points) # points to screen elif asteroid.get_size() == 2: # Case for asteroid of size 2 self.__points += 50 # adds 50 points self.__screen.set_score(self.__points) # points to screen # Here we begin the process of splitting the asteroid self.__screen.unregister_asteroid(asteroid) self.__asteroid_list.remove(asteroid) new_asteroid1 = Asteroid(location, velocity, new_size) negative_velocity_tup = ((-1) * velocity[0], (-1) * velocity[1]) new_asteroid2 = Asteroid(location, negative_velocity_tup, new_size) self.__asteroid_list.append(new_asteroid1) self.__asteroid_list.append(new_asteroid2) self.__screen.register_asteroid(new_asteroid1, new_asteroid1.get_size()) self.__screen.register_asteroid(new_asteroid2, new_asteroid1.get_size()) elif asteroid.get_size() == 1: # Case for asteroid of size 1 self.__points += 100 # adds 100 points self.__screen.set_score(self.__points) # Update the screen # Remove the asteroid as the Radius of the Asteroid is less than 1 self.__screen.unregister_asteroid(asteroid) self.__asteroid_list.remove(asteroid)
def change_size_asteroid(self, asteroid, torpedo): """split the asteroid by his size""" speed_x, speed_y = self.update_asteroid_speed(asteroid, torpedo) size = asteroid.get_size() asteroid1 = Asteroid(asteroid.get_x(),asteroid.get_y(), speed_x, speed_y, size-1) self.__asteroid_lst.append(asteroid1) asteroid2 = Asteroid(asteroid.get_x(), asteroid.get_y(), -speed_x, -speed_y,size-1) self.__asteroid_lst.append(asteroid2) self.__screen.register_asteroid(asteroid1, asteroid1.get_size()) self.__screen.register_asteroid(asteroid2, asteroid2.get_size())
def split_asteroid(self, asteroid, torpedo): """split an asteroid as described when being hit by a torpedo""" if asteroid.get_size() == 3: # split to two fragments of size 2, that go in different directions, # according to a given formula fragment_1_x_vel = (torpedo.get_x_velocity() + asteroid.get_x_velocity()) \ / (asteroid.get_x_velocity() ** 2 + asteroid.get_y_velocity() ** 2) ** 0.5 fragment_1_y_vel = (torpedo.get_y_velocity() + asteroid.get_y_velocity()) \ / (asteroid.get_x_velocity() ** 2 + asteroid.get_y_velocity() ** 2) ** 0.5 fragment_1 = Asteroid(asteroid.get_x_coordinate(), asteroid.get_y_coordinate(), fragment_1_x_vel, fragment_1_y_vel, size=2) # of size 2 self.__screen.register_asteroid(fragment_1, fragment_1.get_size()) self.__screen_asteroids.append(fragment_1) fragment_2_x_vel = (torpedo.get_x_velocity() + (-1)*asteroid.get_x_velocity()) \ / (((-1)*asteroid.get_x_velocity()) ** 2 + ((-1)*asteroid.get_y_velocity()) ** 2) ** 0.5 # asteroid multiplied by -1 so the fragments go in opposite directions fragment_2_y_vel = (torpedo.get_y_velocity() + (-1)*asteroid.get_y_velocity()) \ / (((-1)*asteroid.get_x_velocity()) ** 2 + ((-1)*asteroid.get_y_velocity()) ** 2) ** 0.5 # asteroid multiplied by -1 so the fragments go in opposite directions fragment_2 = Asteroid(asteroid.get_x_coordinate(), asteroid.get_y_coordinate(), fragment_2_x_vel, fragment_2_y_vel, size=2) # of size 2 self.__screen.register_asteroid(fragment_2, fragment_2.get_size()) self.__screen_asteroids.append(fragment_2) if asteroid.get_size() == 2: # split to two fragments of size 1, # that go in different directions, according to a given formula fragment_1_x_vel = (torpedo.get_x_velocity() + asteroid.get_x_velocity()) \ / (asteroid.get_x_velocity() ** 2 + asteroid.get_y_velocity() ** 2) ** 0.5 fragment_1_y_vel = (torpedo.get_y_velocity() + asteroid.get_y_velocity()) \ / (asteroid.get_x_velocity() ** 2 + asteroid.get_y_velocity() ** 2) ** 0.5 fragment_1 = Asteroid(asteroid.get_x_coordinate(), asteroid.get_y_coordinate(), fragment_1_x_vel, fragment_1_y_vel, size=1) # of size 1 self.__screen.register_asteroid(fragment_1, fragment_1.get_size()) self.__screen_asteroids.append(fragment_1) fragment_2_x_vel = \ (torpedo.get_x_velocity() + (-1)*asteroid.get_x_velocity()) /\ (((-1)*asteroid.get_x_velocity()) ** 2 + ((-1)*asteroid.get_y_velocity()) ** 2) ** 0.5 # asteroid multiplied by -1 so the fragments go in opposite directions fragment_2_y_vel =\ (torpedo.get_y_velocity() + (-1)*asteroid.get_y_velocity()) /\ (((-1)*asteroid.get_x_velocity()) ** 2 + ((-1)*asteroid.get_y_velocity()) ** 2) ** 0.5 # asteroid multiplied by -1 so the fragments go in opposite directions fragment_2 = Asteroid(asteroid.get_x_coordinate(), asteroid.get_y_coordinate(), fragment_2_x_vel, fragment_2_y_vel, size=1) # of size 1 self.__screen.register_asteroid(fragment_2, fragment_2.get_size()) self.__screen_asteroids.append(fragment_2) self.__screen.unregister_asteroid(asteroid) self.__screen_asteroids.remove(asteroid) # remove the original asteroid from list.
def asteroid_burst(self, asteroid, torpedo): """This function divide an asteroid to smaller asteroids with opposite velocity and register them""" for i in range(ASTEROID_DIVIDE): # if we can divide the asteroid, it's mane the asteroid size larger # than the smaller size if asteroid.get_size() > SMALL_SIZE: norm = math.sqrt(asteroid.get_velocity_x()**2 + asteroid.get_velocity_y()**2) * (-1)**i new_velocity_x = (torpedo.get_velocity_x() + asteroid.get_velocity_x()) / norm new_velocity_y = (torpedo.get_velocity_y() + asteroid.get_velocity_y()) / norm # the size of the asteroid decrease new_size = asteroid.get_size() - ASTEROID_DIVIDE_SIZE new_asteroid = Asteroid(asteroid.get_position_x(), asteroid.get_position_y(), new_velocity_x, new_velocity_y, new_size) # add the asteroid to the register self.asteroids.append(new_asteroid) self._screen.register_asteroid(new_asteroid, new_asteroid.get_size())
def split_asteroid(self, asteroid, torpedo, correction): """ This function create a new smaller asteroid :param asteroid: destroyed asteroid :param torpedo: torpedo that destroyed it :param correction: the direction correction (1 or -1) :return: """ # Create the new asteroid (it will be at a random place) new_asteroid = Asteroid(asteroid.get_size() - 1) # Putting his speed ad location as wanted new_asteroid.set_x(asteroid.get_x()) new_asteroid.set_y(asteroid.get_y()) new_asteroid.set_speed_x( ((torpedo.get_speed_x() + asteroid.get_speed_x()) / math.sqrt((asteroid.get_speed_x())**2 + (asteroid.get_speed_y())**2)) * correction) new_asteroid.set_speed_y( ((torpedo.get_speed_y() + asteroid.get_speed_y()) / math.sqrt((asteroid.get_speed_x())**2 + (asteroid.get_speed_y())**2)) * correction) # Registering it self._screen.register_asteroid(new_asteroid, new_asteroid.get_size()) self.asteroids_list.append(new_asteroid)
def create_asteroids(self, amount, asteroid=None, new_size=None, torpedo=None): """ first, init asteroids and draw them on screen. if asteroid was sent, func splits it into 2 new asteroids and draw them on screen (orig asteroid is deleted). """ if asteroid: # split given asteroid x_old, y_old, x_speed_old, y_speed_old, size_old = self._obj_prams( asteroid) x_speed_new1, y_speed_new1, x_speed_new2, y_speed_new2 = self.new_asteroid_speed( x_speed_old, y_speed_old, torpedo) asteroid1 = Asteroid(x_old, y_old, x_speed_new1, y_speed_new1, new_size) asteroid2 = Asteroid(x_old, y_old, x_speed_new2, y_speed_new2, new_size) self.update_game_asteroids(asteroid, asteroid1, asteroid2) self.draw_asteroid(asteroid1, new_size, x_old, y_old) self.draw_asteroid(asteroid2, new_size, x_old, y_old) # init asteroids for i in range(amount): x, y = self._random_coor() x_speed, y_speed = self._random_speed() asteroid = Asteroid(x, y, x_speed, y_speed) size = asteroid.get_size() self.update_game_asteroids(asteroid) self.draw_asteroid(asteroid, size, x, y)
def _split_asteroid(self, asteroid, torpedo): """ Create 2 new smaller asteroids, according to torpedo intersection, and removing the old asteroid """ if asteroid.get_size() == 1: self.asteroids_list.remove(asteroid) self.__screen.unregister_asteroid(asteroid) else: ast1 = Asteroid(self._splited_asteroud_speed(asteroid, torpedo, 1), asteroid.get_x_y_loc(), (asteroid.get_size() - 1)) as2 = Asteroid(self._splited_asteroud_speed(asteroid, torpedo, -1), asteroid.get_x_y_loc(), (asteroid.get_size() - 1)) self.__screen.register_asteroid(ast1, ast1.get_size()) self.__screen.register_asteroid(as2, as2.get_size()) self.asteroids_list.append(ast1) self.asteroids_list.append(as2) self.asteroids_list.remove(asteroid) self.__screen.unregister_asteroid(asteroid)
def _init_asteroid_start(self, asteroids_amount): """ launch new asteroid. :param asteroids_amount: """ for i in range(asteroids_amount): new_asteroid = Asteroid() self.__asteroid_list.append(new_asteroid) self.__screen.register_asteroid(asteroid=new_asteroid, size=new_asteroid.get_size())
def create_asteroids(self, num_of_asteroids_to_create, location_vectors_list=None, speed_vectors_list=None, size_list=None): """ :param num_of_asteroids_to_create: default number of 5 as given in the exercise :param location_vectors_list: list of the asteroids random locations :param speed_vectors_list: list of the asteroids random speeds :param size_list: list of the asteroids sizes (the size is 3 at the beginning) The function creates 5 asteroids with the information in the lists :return: list of 5 asteroids """ asteroids_added = [] # starts an empty list which the new asteroids is going to be if location_vectors_list is None: # uses create_random_location_vectors_for_asteroids to # create list of random locations for the new asteroids location_vectors_list = self.create_random_location_vectors_for_asteroids( num_of_asteroids_to_create) if speed_vectors_list is None: # uses create_random_speed_vectors_for_asteroids # to create list of random speed locations for the new asteroids speed_vectors_list = self.create_random_speed_vectors_for_asteroids( num_of_asteroids_to_create) if size_list is None: # uses create_size_list_for_asteroids # to create list of sizes for the new asteroids size_list = self.create_size_list_for_asteroids( num_of_asteroids_to_create) # the for loop creates the asteroids with the data in lists which # created in the 3 if's above append them to the # list of asteroids and screen them for i in range(num_of_asteroids_to_create): new_asteroid = Asteroid(location_vectors_list[i], speed_vectors_list[i], size_list[i]) asteroids_added.append(new_asteroid) self._screen.register_asteroid(new_asteroid, new_asteroid.get_size()) self.draw_asteroids(asteroids_added) return asteroids_added
def create_asteroids(self): """ creating the no. of asteroids given, providing them with ID and assigning them to a designated dictionary """ for count in range(self.asteroids_amnt): # create new asteroid and assign it to the dictionary new_ast = Asteroid(ASTEROID_START_SIZE, self.screen_max_x, self.screen_max_y, self.screen_min_x, self.screen_min_y) self.asteroids_list.append(new_ast) self._screen.register_asteroid(new_ast, new_ast.get_size())
def split_asteroid(self, ast, torp): """This method is responsible to split an asteroid if it gets hit by a torpedo. The method gets the asteroid the was being hit and the torpedo that hit it.""" new_asteroids_speed_x, new_asteroids_speed_y = self.__calculate_speed_for_spliting_asts( ast, torp) ast1 = Asteroid( ast.get_location_x(), ast.get_location_y(), new_asteroids_speed_x, new_asteroids_speed_y, ast.get_size() - 1) # the first asteroid is created with positive speed. ast2 = Asteroid(ast.get_location_x(), ast.get_location_y(), (-1) * new_asteroids_speed_x, (-1) * new_asteroids_speed_y, ast.get_size() - 1) # the first asteroid is created with negative speed self.__asteroids.append( ast1) # adds the first asteroid to the asteroids list self.__screen.register_asteroid( ast1, ast1.get_size()) # adds the asteroid to the screen self.__asteroids.append( ast2) # adds the second asteroid to the asteroids list self.__screen.register_asteroid( ast2, ast2.get_size()) # adds the asteroid to the screen
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)
def __init__(self, asteroids_amnt): self._screen = Screen() self._ship = Ship() self.screen_max_x = Screen.SCREEN_MAX_X self.screen_max_y = Screen.SCREEN_MAX_Y self.screen_min_x = Screen.SCREEN_MIN_X self.screen_min_y = Screen.SCREEN_MIN_Y self._asteroids = list() self._torpedoes = list() for i in range(asteroids_amnt): asteroid = Asteroid() self._screen.register_asteroid(asteroid, asteroid.get_size()) self._asteroids.append(asteroid) for i in range(TORPEDO_AMNT): self._torpedoes.append(Torpedo(self._ship))
def __init__(self, asteroids_amount): """ :param asteroids_amount: the number of asteroids that are in the start of the game by user input or defult if there is no input :initializing: the parameters to the game """ self._screen = Screen() self.ship = Ship() self.screen_max_x = Screen.SCREEN_MAX_X self.screen_max_y = Screen.SCREEN_MAX_Y self.screen_min_x = Screen.SCREEN_MIN_X self.screen_min_y = Screen.SCREEN_MIN_Y self.asteroids_amount = asteroids_amount self.torpedo_lst = [] self.asteroid_lst = [] for i in range(self.asteroids_amount): # update asteroids asteroid = Asteroid(self.ship) self.asteroid_lst.append(asteroid) self._screen.register_asteroid(asteroid, asteroid.get_size()) self.score = 0 self.rotation = random.random() * 5
def split_asteroid(self, asteroid, torpedo): """ function splits asteroid after collision with a torpedo according to game rules :param asteroid: object which have to be splited :param torpedo: object which had an intersection with asteroid :return: """ if asteroid.get_size() == 3: self.__screen.unregister_asteroid(asteroid) self.__asteroids.remove(asteroid) new_asteroid_1_speed_x = torpedo.get_speed( )[0] + asteroid.get_speed()[0] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_1_speed_y = torpedo.get_speed( )[1] + asteroid.get_speed()[1] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_2_speed_x = -torpedo.get_speed( )[0] + asteroid.get_speed()[0] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_2_speed_y = -torpedo.get_speed( )[1] + asteroid.get_speed()[1] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_1 = Asteroid(asteroid.get_coor()[0], asteroid.get_coor()[1], new_asteroid_1_speed_x, new_asteroid_1_speed_y, 2) new_asteroid_2 = Asteroid(asteroid.get_coor()[0], asteroid.get_coor()[1], new_asteroid_2_speed_x, new_asteroid_2_speed_y, 2) self.__screen.register_asteroid(new_asteroid_1, new_asteroid_1.get_size()) self.__screen.register_asteroid(new_asteroid_2, new_asteroid_2.get_size()) self.__asteroids.append(new_asteroid_1) self.__asteroids.append(new_asteroid_2) elif asteroid.get_size() == 2: self.__screen.unregister_asteroid(asteroid) self.__asteroids.remove(asteroid) new_asteroid_1_speed_x = torpedo.get_speed( )[0] + asteroid.get_speed()[0] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_1_speed_y = torpedo.get_speed( )[1] + asteroid.get_speed()[1] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_2_speed_x = -torpedo.get_speed( )[0] + asteroid.get_speed()[0] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_2_speed_y = -torpedo.get_speed( )[1] + asteroid.get_speed()[1] / math.sqrt( math.pow(asteroid.get_speed()[0], 2) + math.pow(asteroid.get_speed()[1], 2)) new_asteroid_1 = Asteroid(asteroid.get_coor()[0], asteroid.get_coor()[1], new_asteroid_1_speed_x, new_asteroid_1_speed_y, 1) new_asteroid_2 = Asteroid(asteroid.get_coor()[0], asteroid.get_coor()[1], new_asteroid_2_speed_x, new_asteroid_2_speed_y, 1) self.__screen.register_asteroid(new_asteroid_1, new_asteroid_1.get_size()) self.__screen.register_asteroid(new_asteroid_2, new_asteroid_2.get_size()) self.__asteroids.append(new_asteroid_1) self.__asteroids.append(new_asteroid_2) elif asteroid.get_size() == 1: self.__screen.unregister_asteroid(asteroid) self.__asteroids.remove(asteroid)