Exemple #1
0
    def intersection_with_asteroid_torpedo(self, asteroid, torpedo):
        '''

        :param asteroid: An asteroid on the screen
        :param torpedo: A torpedo fired from the ship
        :return: Deals with the occurrence where a torpedo hits an asteroid
        '''
        if asteroid.get_life() > 1:  # If the asteroid's life is greater than 1, then it divides into two smaller asteroids
            x_torpedo, y_torpedo = torpedo.get_speed()
            x_asteroid, y_asteroid = asteroid.get_speed()
            speed_x = (x_torpedo + x_asteroid) / math.sqrt(x_asteroid ** 2 + y_asteroid ** 2)
            speed_y = (y_torpedo + y_asteroid) / math.sqrt(x_asteroid ** 2 + y_asteroid ** 2)
            new_speed1 = speed_x, speed_y
            temp_asteroid1 = Asteroid(asteroid.get_location(), new_speed1, asteroid.get_life() - 1)
            new_speed2 = -speed_x, -speed_y
            temp_asteroid2 = Asteroid(asteroid.get_location(), new_speed2, asteroid.get_life() - 1)
            self._screen.unregister_asteroid(asteroid)
            self._asteroid_list.remove(asteroid)
            self._asteroid_list.append(temp_asteroid1)
            self._asteroid_list.append(temp_asteroid2)
            self._screen.register_asteroid(temp_asteroid1, temp_asteroid1.get_life())
            x_location1, y_location1 = temp_asteroid1.get_location()
            self._screen.draw_asteroid(temp_asteroid1, x_location1, y_location1)
            self._screen.register_asteroid(temp_asteroid2, temp_asteroid2.get_life())
            x_location2, y_location2 = temp_asteroid2.get_location()
            self._screen.draw_asteroid(temp_asteroid2, x_location2, y_location2)
        else:  # Else, it disappears
            self._screen.unregister_asteroid(asteroid)
            self._asteroid_list.remove(asteroid)
Exemple #2
0
    def split_ast(self, ast, torp):
        astro = self.__asteroid_dict[ast]
        torpy = self.__torpedo_dict[torp]
        new_speed_x = (torpy.get_speed(X) + astro.get_speed(X)) / math.sqrt(
            astro.get_speed(X)**2 + astro.get_speed(Y)**2)
        new_speed_y = (torpy.get_speed(Y) + astro.get_speed(Y)) / math.sqrt(
            astro.get_speed(X)**2 + astro.get_speed(Y)**2)
        new_size = astro.get_size() - 1
        ast_1 = Asteroid(astro.get_location(X), astro.get_location(Y),
                         new_speed_x, new_speed_y, new_size)
        ast_2 = Asteroid(astro.get_location(X), astro.get_location(Y),
                         -1 * new_speed_x, -1 * new_speed_y, new_size)

        self.__screen.unregister_asteroid(astro)
        self.__screen.unregister_torpedo(torpy)
        del self.__asteroid_dict[ast]
        del self.__torpedo_dict[torp]

        self.__asteroid_dict[self.__asteroid_counter + 1] = ast_1
        self.__asteroid_counter += 1
        self.__asteroid_dict[self.__asteroid_counter + 1] = ast_2
        self.__asteroid_counter += 1
        self.__screen.register_asteroid(ast_1, new_size)
        self.__screen.register_asteroid(ast_2, new_size)
        self.__screen.draw_asteroid(ast_1, ast_1.get_location(X),
                                    ast_1.get_location(Y))
        self.__screen.draw_asteroid(ast_2, ast_2.get_location(X),
                                    ast_2.get_location(Y))