Implementing game mode in a python

Question:

I implemented a game mode in my alien onslaught game made with python and pygame and the concept is like this:
Players are fighting aliens but each player has a limited number of bullets. When a player remains out of bullets he becomes inactive and, the game ends when both players run out of bullets.
The method that handles what happens with the players when they run out of bullets is like this:

def last_bullet(self, thunderbird, phoenix):
        """Starts the Last Bullet game mode in which the players must fight aliens
        but they have a limited number of bullets, when a player remains with no bullets
        he dies, when both players are out of bullets, the game is over."""
        for player in [thunderbird, phoenix]:
            if player.remaining_bullets <= 0:
                player.state.alive = False

        if all(not player.state.alive for player in [thunderbird, phoenix]):
            self.stats.game_active = False

But there is the case in which the player shoots his last bullet to kill the last alien remaining on the screen and if that alien is destroyed, the player should not become inactive and I don’t know how to implement that. What kind of condition I should add to determine if the player becomes inactive beside the number of bullets? I tried to check the number of aliens remaining, and if it is higher than 1, the player should become inactive because he can t kill them with the last bullet. I also had the idea of reviving the player if after shooting his last bullet, the last alien died and the level progressed. Any ideas of how should I go about this?

Asked By: Ake

||

Answers:

It’s a bit hard to answer without knowing exactly what state your game tracks.

I’d assume you could also track the number of flying_bullets?

If so, I guess the following condition could work for a game over case:

remaining_bullets <= 0 and flying_bullets <= 0 and remaining_aliens > 0
Answered By: Paolo

To implement this feature, you can check if there are any aliens remaining on the screen before setting the player to inactive. You can modify the last_bullet method like this:

def last_bullet(self, thunderbird, phoenix):
    """Starts the Last Bullet game mode in which the players must fight aliens
    but they have a limited number of bullets, when a player remains with no bullets
    he dies, when both players are out of bullets, the game is over."""

    # Check if there are any aliens remaining on the screen
    aliens_remaining = len(self.aliens.sprites())

    for player in [thunderbird, phoenix]:
        if player.remaining_bullets <= 0 and aliens_remaining > 0:
            player.state.alive = False

    if all(not player.state.alive for player in [thunderbird, phoenix]):
        self.stats.game_active = False

In this version of the method, we first count the number of aliens remaining on the screen by checking the length of the self.aliens.sprites() list. We then only set the player to inactive if there are aliens remaining and the player has no bullets left. This way, if the player shoots their last bullet and kills the last alien, they will not become inactive.

Please make sure that you have an aliens attribute in your game class that contains all the alien sprites. If your implementation is different, adjust the aliens_remaining line accordingly.

Answered By: BotName
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.