Пример #1
0
    def _decrementEnemyCount(self, quadrant: Quadrant, enemyName: str):
        """
        Decrement the appropriate enemy count

        Args:
            quadrant:   A randomly generated quadrant
            enemyName:  The enemy name, 'Klingon', 'Commander', 'SuperCommander'

        I normally don't like to write dynamically generated codes like this.  However,
        I could not in good conscience duplicate this code three times;  I know it is
        very 'Pythonic', but very high maintenance
        """
        gsPropertyName: str = f'remaining{enemyName}s'
        qPropertyName: str = f'{enemyName[0].lower()}{enemyName[1:]}s'

        enemies: Enemies = getattr(quadrant, qPropertyName)
        for enemy in enemies:
            quadrant.decrementEnemyCount(enemy)
            remainingEnemyCount: int = getattr(self._gameState, gsPropertyName)
            remainingEnemyCount -= 1
            assert remainingEnemyCount >= 0, f'{enemyName=}  Logic error;  Fix it !'
            setattr(self._gameState, gsPropertyName, remainingEnemyCount)

        enemyCount: int = len(enemies)
        if enemyCount > 1:
            message: str = f'{len(enemies)} {enemyName}s destroyed'
        else:
            message = f'{len(enemies)} {enemyName} destroyed'

        if enemyCount > 0:
            self.logger.debug(f'{message}')
            self._messageConsole.displayMessage(message)
Пример #2
0
    def _killEnemy(self, quadrant: Quadrant, enemy: Enemy):
        """
        Remove enemy from board and update the game engine
        Args:
            enemy:  The "whacked" bad dude
        """

        deadMsg: str = f'Enemy at {enemy.gameCoordinates} destroyed'

        self._messageConsole.displayMessage(deadMsg)
        self.logger.info(deadMsg)
        self._gameEngine.decrementEnemyCount(enemy=enemy)
        quadrant.decrementEnemyCount(enemy=enemy)

        sprite: Sprite = cast(Sprite, enemy)
        sprite.remove_from_sprite_lists()