1
我有兩個班,第一個具有移動功能(生物,板)。然後在生物類中有一個調用移動的函數,那麼在生物類中如何將當前生物傳遞給移動函數呢?如果它只是被移動(個體經營,self.board),因爲當我嘗試,我得到一個「來自導入未定義的變量: 移動」的錯誤?通過自另一個類的功能在自主的功能
下面是相關代碼:
怪物:
class creature:
def __init__(self, social, intelligence, sensory, speed, bravery, strenght, size):
self.traits = [social, intelligence, sensory, speed, bravery, strenght]
self.x = 0
self.y = 0
self.hunger = 10
self.energy = 30
self.shelter = 0
self.dominance = 0
self.boardSize = size - 1
self.SOCIAL = 0
self.INTELLIGENCE = 1
self.SENSORY = 2
self.SPEED = 3
self.BRAVERY = 4
self.STRENGTH = 5
...
def performAction(self, action, location):
...
if action == "storeFood":
food = location.vegetation
location.vegetation = -1
simulation.move(self, self.shelter)
self.shelter.foodStorage += food
...
模擬:
class simulation():
def __init__(self, x):
self.creatures = {creature.creature():"", creature.creature():"", }
self.map = land.landMass
self.lifeCycles = x
self.runStay = ["rfl", "rbf", "rbl", "rbf", ]
self.befriend = ["bbl", "bbf"]
self.fight = ["fbl", "fbf", "bfl", "bff", "ffl", "fff"]
...
def move(self, creature, target):
map[creature.x][creature.y].creatures.remove(creature)
creature.energy -= abs(map[creature.x][creature.y].elevation - target.elevation)/creature.getSpeed()
target.creatures.append(creature)
creature.x, creature.y = target.location
...
編輯: 行,所以我有所解決了這個問題。 Python的要求,我有simulation.simulation.map(self, self.shelter)
我假設這意味着它不僅需要的類文件,而且這個類的一個實例。所以新的問題是我必須在其他地方將該實例傳遞給其他人嗎?或者這可以在其他地方使用仿真實例嗎?
可以告訴你你的代碼? – namit 2013-03-24 03:58:29
我已經添加了它,希望它是有道理的 – EasilyBaffled 2013-03-24 04:18:26
您需要發佈的類和方法簽名(在'class'和'def'線)。爲什麼你有兩個'simulation.move()'調用? – 2013-03-24 04:22:49