简体   繁体   中英

I'm having a hard time figuring out why I can't call from my dictionary

so I'm making a space invaders game, and I can't find out what I did wrong, I'm following a tutorial, everything should be the same as there but somehow for me it's not working.

Here's my main class:

class Ship:

    def __init__(self, x, y, color, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0

and subclass for ships:

class Enemy(Ship):

    SHIP_COLOR = {"r": (RED_SPACESHIP, RED_LASER), "b": (BLUE_SPACESHIP, BLUE_LASER), "g": (GREEN_SPACESHIP, GREEN_LASER)}
    
    def __init__(self, x, y, color, health=100):
        super().__init__(x, y, health)
        self.ship_img, self.laser_img = self.SHIP_COLOR(color)
        self.mask = pygame.mask.from_surface(self.ship_img)

so what I'm trying to do is generating a random color for the ships with this line:

enemy = Enemy(random.randrange(50, WIDTH-100), random.randrange(-1500, -100), random.choice(["r", "b", "g"]))
enemies.append(enemy)

The first to input are the coordinates basically and the 3rd one should pick the colour. Random choice should pick a pair from the dictionary for ship and laser color, either red, green or blue.

Any help is appreciated, I'm trying since yesterday morning and I get the same error every time (TypeError: 'dict' object is not callable)

The error message tells the issue:

TypeError: 'dict' object is not callable

self.SHIP_COLOR(color) tries to "call" the dictionary as if it were a function. Use [...] to index the dictionary instead by changing self.SHIP_COLOR(color) to self.SHIP_COLOR[color] .

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