[英]I'm having a hard time figuring out why I can't call from my dictionary
所以我正在制作一個太空入侵者游戲,但我找不到我做錯了什么,我正在按照教程進行操作,一切都應該與那里相同,但不知何故對我來說它不起作用。
這是我的主要 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
和船舶的子類:
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)
所以我要做的是用這條線為船只生成隨機顏色:
enemy = Enemy(random.randrange(50, WIDTH-100), random.randrange(-1500, -100), random.choice(["r", "b", "g"]))
enemies.append(enemy)
第一個輸入基本上是坐標,第三個輸入顏色。 隨機選擇應該從字典中選擇一對用於船舶和激光顏色,紅色、綠色或藍色。
感謝任何幫助,我從昨天早上開始嘗試,每次都會遇到同樣的錯誤(TypeError: 'dict' object is not callable)
錯誤消息說明了問題:
TypeError: 'dict' object 不可調用
self.SHIP_COLOR(color)
嘗試“調用”字典,就好像它是 function 一樣。使用[...]
通過將self.SHIP_COLOR(color)
更改為self.SHIP_COLOR[color]
來索引字典。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.