Reputation: 1
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)
Upvotes: 0
Views: 37
Reputation: 11171
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]
.
Upvotes: 1