简体   繁体   中英

Python text based game - TypeError: bool oject not callable

I am creating my first python program and have been struggling with a "TypeError: 'bool' object is not callable" error. I have only been studying for a few weeks so any assistance would be great, Also, if there are any tips on how to reduce some of the code that'd be great! Thanks!

import random

class Spacecraft:
    def __init__(self, type, health):
        self.type = type
        self.health = health
        self.ship_destroyed = False

    def ship_destroyed(self):
        self.ship_destroyed = True
        if self.health != 0:
            self.health = 0
        print("Your spacecraft has been destroyed!")

    def ship_destroyed_def(self):
        self.ship_destroyed = True
        if self.health != 0:
            self.health = 0
        print("Your enemy spacecraft has been destroyed! YOU WON!!")

    def lose_health(self, amount):
        self.health -= amount
        if self.health <= 0:
            self.ship_destroyed()
        else:
            print("Your spacecraft now has {health} hit points remaining.".format(health=self.health))

    def lose_health_def(self, amount):
        self.health -= amount
        if self.health <= 0:
            self.health = 0
            self.ship_destroyed()
        else:
            print("The enemy spacecraft now has {health} hit points remaining".format(health=self.health))
            print()

    def attack(self, enemy_ship):
        while True:
            damage_fighter = random.randrange(2, 14)
            damage_defender = random.randrange(4, 10)
            if self.type == "fighter":
                print(
                    'Your {type} spacecraft attacked the enemy ship for {damage} damage and the enemy {enemy_ship} spacecraft attacked your ship for {damage2} damage.'.format(
                        type=self.type,
                        damage=damage_fighter,
                        enemy_ship=enemy_ship.type,
                        damage2=damage_defender))
                self.lose_health(damage_defender)
                enemy_ship.lose_health_def(damage_fighter)
            elif self.type == "defender":
                print(
                    'Your {type} spacecraft attacked the enemy ship for {damage} damage and the enemy {enemy_ship} spacecraft attacked your ship for {damage2} damage.'.format(
                        type=self.type,
                        damage=damage_defender,
                        enemy_ship=enemy_ship.type,
                        damage2=damage_fighter))
                self.lose_health(damage_fighter)
                enemy_ship.lose_health_def(damage_defender)

class Player:
    def __init__(self, type):
        self.type = type
        self.current_type = 0

    def attack_enemy_ship(self, enemy_ship):
        my_ship = self.type[self.current_type]
        their_ship = enemy_ship.type[enemy_ship.current_type]
        my_ship.attack(their_ship)

a = Spacecraft("fighter", 40)
b = Spacecraft("defender", 50)

print()
player_name = input('Welcome to Space Warriors! Please enter your name and hit enter. ')
player_style = input('''
Welcome ''' + player_name + '''! Space Warriors is a game that allows you to chose between two classes of spacecraft. If you select a 
fighter spacecraft when you will fight again a defender spacecraft. If you choose a defender space craft
then you will fight a fighter spacecraft. Please select "Fighter" or "Defender" and press enter. ''').lower()

player_selected = []
computer_selected = []

if player_style == 'fighter':
    player_selected.append(a)
    computer_selected.append(b)

else:
    player_selected.append(b)
    computer_selected.append(a)

live_player = Player(player_selected)
computer_player = Player(computer_selected)

print()
print("Let's get ready to fight! Both ships are launched!")
print()

live_player.attack_enemy_ship(computer_player)

Once one of the two ships reaches zero hit points I am attempting to call ship_destroyed or ship_destroyed_def, to end the game; however the program stops once one of the ships hits "0". This is when I receive the error.

def ship_destroyed(self):
    self.ship_destroyed = True

You're trying to make a function and an attribute that are both named ship_destroyed . You can't do that. Pick a different name for one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM