这是我的代码:
class MinerNotFullAction:
def __init__(self, entity, image_store):
self.entity = entity
self.image_store = image_store
def miner_to_ore(self, world, ore):
entity_pt = MinerNotFull.get_position(self.entity)
if not ore:
return ([entity_pt], False)
ore_pt = MinerNotFull.get_position(ore)
if adjacent(entity_pt, ore_pt):
MinerNotFull.set_resource_count(self.entity,
1 + MinerNotFull.get_resource_count(self.entity))
remove_entity(world, ore)
return ([ore_pt], True)
else:
new_pt = next_position(world, entity_pt, ore_pt)
return (worldmodel.move_entity(world, entity, new_pt), False)
def miner_not_full_action(world, action, ticks):
entity = action.entity
entity_pt = MinerNotFull.get_position(entity)
ore = find_nearest(world, entity_pt, entities.Ore)
(tiles, found) = self.miner_to_ore(world, entity, ore)
if found:
entity = try_transform_miner(world, entity,
try_transform_miner_not_full)
schedule_action(world, entity,
create_miner_action(entity, action.image_store),
ticks + entities.get_rate(entity))
return tiles
def take_action(world, action, ticks):
entities.remove_pending_action(action.entity, action)
if isinstance(action, MinerNotFullAction):
return self.miner_not_full_action(world, action, ticks)
return []
当我不断收到此错误时:
Traceback (most recent call last):
File "main.py", line 68, in <module>
main(len(sys.argv) <= MIN_ARGS or sys.argv[INIT_ARG_IDX] != INIT_ARG_RANDOM)
File "main.py", line 64, in main
controller.activity_loop(view, world)
File "/Users/KarenLee/CPE102/hw01/controller.py", line 50, in activity_loop
handle_timer_event(world, view)
File "/Users/KarenLee/CPE102/hw01/controller.py", line 27, in handle_timer_event
rects = worldmodel.update_on_time(world, pygame.time.get_ticks())
File "/Users/KarenLee/CPE102/hw01/worldmodel.py", line 78, in update_on_time
tiles.extend(actions.take_action(world, next.item, ticks))
AttributeError: 'module' object has no attribute 'take_action'
感谢任何反馈!
答案 0 :(得分:0)
您的包中有一个名为actions.py
的文件。当你这样做
actions.take_action(world, next.item, ticks)
它在take_action()
模块中查找actions.py
并抛出错误。
从代码中可以清楚地看出take_action
实际上是MinerNotFullAction
的方法,因此行actions.take_action(world, next.item, ticks)
应该为某个对象调用方法actions
。这是一个命名冲突,因此错误
解决方案是将您的MinerNotFullAction
对象(当前名为actions
)重命名为其他内容
miner_action = MinerNotFullAction()
然后在代码中而不是actions.take_action
执行miner_action.take_action
。
POSTSCRIPT
即使你删除了这个命名冲突,也存在一些其他问题(正如人们在评论中已经说过的那样),特别是你的方法缺少self
个参数。