Dear StackOverflow Angels,
I am really stuck with this problem. I am coding a game and am receiving this error message:
File "VillageGame.py", line 120, in <module>
launch.play()
File "VillageGame.py", line 84, in play
returned_scene = future_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'
Here is the code referenced by the error message:
class Room(object):
def __init__(self):
pass
def enter(self):
pass
class Road(Room):
def __init__(self):
pass
def enter(self):
pass
def walk(self):
print "It is afternoon and you stand by the road of Red Village. It is a small, run-down place"
print "with a few shops and houses. A few residents walk about. It is a quiet place. You need"
print "to steal gold to feed yourself and pay your rent. This evening you need to steal 800 gold"
print "to pay your rent and bills. There are houses and businesses in the area. But plan carefully,"
print "the police here are armed and the locals are suspicious of odd outsiders like yourself. But"
print "you can fight, which should help. In front of you is a food shop, a weapons shop,"
print "a medicine shop, a bank and an inn. Where would you like to go first?"
return 'road'
def run(self):
pass
class Engine(object):
def __init__(self, game_map):
self.game_map = game_map
def play(self):
future_scene = self.game_map.launch_scene()
last_scene = self.game_map.next_scene('finished')
while future_scene != last_scene:
returned_scene = future_scene.enter()
future_scene = self.game_map.next_scene(returned_scene)
# make sure you run the last scene
future_scene.enter()
class Map(object):
rooms = {
'road' : Road(),
'food_shop' : FoodShop(),
'weapons_shop' : WeaponsShop(),
'medicine_shop' : MedicineShop(),
'bank' : Bank(),
'inn' : Inn(),
'death' : Death(),
'finished' : Finished()
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, returned_scene):
val = Map.rooms.get(returned_scene)
return val
def launch_scene(self):
return self.next_scene(self.start_scene)
firstscene = Map('road')
launch = Engine(firstscene)
launch.play()
Apologies if this seems like a code dump, but I don't know what parts are relevant to the error message and what parts are not linked to it - because I am quite new to this you see.
If anyone has any insight into what code I could cut out of this message, I Would appreciate that too. It would help me to keep future questions shorter.
2 Answers 2
The problem is Map('road') returns None. I think what you're trying to do is this.
map = Map('road')
firstscene = map.launch_scene()
launch = Engine(firstscene)
launch.play()
But even that is not quite right. The line below doesn't instantiate a map.
val = Map.rooms.get(returned_scene)
If you want to call a function of the Map class from within that class, use the self keyword such as self.rooms.get(returned_scene).
2 Comments
val = self.rooms.get(returned_scene) and changed this: firstscene = Map('road') launch = Engine(firstscene) launch.play() to: map = Map('road') firstscene = map.launch_scene() launch = Engine(firstscene) launch.play() I just got this error message: File "VillageGame.py", line 121, in <module> launch.play() File "VillageGame.py", line 80, in play future_scene = self.game_map.launch_scene() AttributeError: 'Road' object has no attribute 'launch_scene'self.game_map refers to a Road() object within your engine and you are calling launch_scene() on a Road(). Road doesn't have that method so the interpreter is telling you that. I'm not 100% sure what you are intending here, but I think removing .launch_scence() from the line future_scene = self.game_map.launch_scene() will be moving in the right direction. Let me know how that goes.This line returned_scene = future_scene.enter() expects a return value but you haven't implemented anything in the methoddef enter(self) (you're just pass-ing it, and so it returns a None object.
1 Comment
File "VillageGame.py", line 118, in <module> launch.play() File "VillageGame.py", line 77, in play future_scene = self.game_map.launch_scene() AttributeError: 'Road' object has no attribute 'launch_scene'