コード例 #1
0
 def move_torpedo(self):
     """
     moves the torpedo in the direction it was fire and update the game
     score when we hit an asteroid
     """
     if self._screen.is_space_pressed() and len(self.torpedo_lst) <= 15:
         torpedo = Torpedo(self.ship)
         torpedo.set_dx()
         torpedo.set_dy()
         self._screen.register_torpedo(torpedo)
         self.torpedo_lst.append(torpedo)
     dead_torpedo_lst = []
     for i, torpedo in enumerate(self.torpedo_lst):
         if torpedo.get_life() > 0:
             self.move(torpedo)
             self._screen.draw_torpedo(torpedo, torpedo.get_lx(),
                                       torpedo.get_ly(),
                                       torpedo.get_heading())
             torpedo.life_update()
         else:
             dead_torpedo_lst.append(i)
         for j, ast in enumerate(self.asteroid_lst):
             if ast.has_intersection(torpedo):
                 self.score_update(ast.get_size())
                 self.torpedo_hit(ast, torpedo, i, j)
                 dead_torpedo_lst.append(i)
     new_torpedo_list = []
     for i in range(len(self.torpedo_lst)):
         if i not in dead_torpedo_lst:
             new_torpedo_list.append(self.torpedo_lst[i])
         if i in dead_torpedo_lst:
             self._screen.unregister_torpedo(self.torpedo_lst[i])
     self.torpedo_lst = new_torpedo_list
コード例 #2
0
 def create_torpedo(self):
     torp = Torpedo(self.__ship_dict["ship"].get_location(X),
                    self.__ship_dict["ship"].get_location(Y),
                    self.__ship_dict["ship"].get_speed(X),
                    self.__ship_dict["ship"].get_speed(Y),
                    self.__ship_dict["ship"].get_heading())
     self.__screen.register_torpedo(torp)
     self.__screen.draw_torpedo(torp, torp.get_location(X),
                                torp.get_location(Y), torp.get_heading())
     self.__torpedo_dict[self.__torpedo_counter] = torp
コード例 #3
0
    def register_torpedo(self):
        '''

        :return: Generates objects with Class type Torpedo and draws them to the screen
        '''
        if self._screen.is_space_pressed() and len(self._torpedo_list) <= MAX_TORPEDOS:
            temp_x_speed, temp_y_speed = self._ship.get_speed()
            new_x_speed = temp_x_speed + ACC_FACTOR * math.cos(math.radians(self._ship.get_heading()))
            new_y_speed = temp_y_speed + ACC_FACTOR * math.sin(math.radians(self._ship.get_heading()))
            speed = new_x_speed, new_y_speed
            temp_torpedo = Torpedo(self._ship.get_location(), speed, self._ship.get_heading(), TORPEDO_RADIUS)
            if (id(temp_torpedo) not in TORPEDO_LIST_ID):
                TORPEDO_LIST_ID.append(temp_torpedo)
                self._screen.register_torpedo(temp_torpedo)
            x, y = temp_torpedo.get_location()
            self._screen.draw_torpedo(temp_torpedo, x, y, temp_torpedo.get_heading())
            self._torpedo_list.append(temp_torpedo)
コード例 #4
0
    def _game_loop(self):
        """
        Main game logic method
        """
        ship_pos = self.ship.get_position()
        self._screen.draw_ship(ship_pos[self.X], ship_pos[self.Y],
                               self.ship.get_heading())
        ship_vel = self.ship.get_velocity()

        # Rotate ship to the left
        if self._screen.is_left_pressed():
            self.ship.set_heading(self.ship.get_heading() + self.ROTATE_LEFT)

        # Rotate ship to the right
        if self._screen.is_right_pressed():
            self.ship.set_heading(self.ship.get_heading() + self.ROTATE_RIGHT)

        # Accelerate ship
        if self._screen.is_up_pressed():
            self.ship.accelerate()

        # Fire torpedo
        if self._screen.is_space_pressed():
            if self.torpedo_count != self.MAX_TORPEDOS_AT_ONCE:
                torpedo = Torpedo(ship_pos, ship_vel, self.ship.get_heading())
                self._screen.register_torpedo(torpedo)
                self.torpedo_lives.append(self.TORPEDO_LIFESPAN)
                self.torpedo_count += 1
                self.torpedos.append(torpedo)

        # This section of code handles each asteroid objects parameter updates
        for asteroid in self.asteroids:
            ast_pos = asteroid.get_position()
            self._screen.draw_asteroid(asteroid, ast_pos[self.X],
                                       ast_pos[self.Y])
            ast_vel = asteroid.get_velocity()

            # From here till the end of the loop, the code handles
            # intersections (if any) of the asteroid with the ship or with a
            # torpedo
            if asteroid.has_intersection(self.ship):
                self._screen.remove_life()
                self.ship_lives -= 1
                self._screen.show_message(self.HIT_TITLE, self.HIT_MSG)
                self.remove_asteroid(asteroid)
                continue

            ast_size = asteroid.get_size()

            for torpedo in self.torpedos:
                if asteroid.has_intersection(torpedo):
                    i = ast_size - 1  # Score option's index
                    self.score += self.SCORE_OPTIONS[i]
                    self._screen.set_score(self.score)
                    # Split the asteroid into 2 smaller ones
                    # Remove original asteroid
                    self.remove_asteroid(asteroid)
                    # Removes the torpedo after it hit the asteroid
                    self.remove_torpedo(self.torpedos.index(torpedo), torpedo)

                    if i == 0:
                        # Iteration can be terminated if the removed
                        # asteroid had the smallest size
                        break

                    # Calculate new velocity
                    nom = map(add, ast_vel, torpedo.get_velocity())
                    den = sqrt(ast_vel[self.X]**2 + ast_vel[self.Y]**2)
                    # Nominator / Denominator
                    n_vel = tuple(v / den for v in nom)
                    # Create new asteroids
                    self.add_asteroid(ast_pos, n_vel, i)
                    # Second asteroid with the opposing velocity
                    self.add_asteroid(ast_pos, tuple(v * -1 for v in n_vel), i)

            # Move asteroid
            asteroid.set_position(self.get_new_coords(ast_vel, ast_pos))

        # This section of code updates torpedos parameters
        for torpedo in self.torpedos:
            t_pos = torpedo.get_position()
            self._screen.draw_torpedo(torpedo, t_pos[self.X], t_pos[self.Y],
                                      torpedo.get_heading())

            # Move torpedo
            torpedo.set_position(
                self.get_new_coords(torpedo.get_velocity(), t_pos))

            # Decrease lifespan of the torpedo
            torp_index = self.torpedos.index(torpedo)
            self.torpedo_lives[torp_index] -= 1

            # Remove torpedo after its lifespan is depleted
            if self.torpedo_lives[torp_index] == 0:
                self.remove_torpedo(torp_index, torpedo)

        # Exit game and show win message when all asteroids are destroyed
        if not self.asteroids:
            self.exit_game(self.WON_TITLE, self.WON_MSG)

        # Exit game and show loss message when ship is out of lives
        if self.ship_lives == 0:
            self.exit_game(self.LOST_TITLE, self.LOST_MSG)

        # Exit game on request
        if self._screen.should_end():
            self.exit_game(self.QUIT_TITLE, self.QUIT_MSG)

        # Move ship
        self.ship.set_position(self.get_new_coords(ship_vel, ship_pos))