这个问题实际上是我昨天发布的一个问题的延续。我正在完成一个教程,并且正在花时间让我的“游戏”运行起来。游戏是一组类的场景。然后有一个引擎对象,用于处理每个场景。根据每个场景中的玩家输入,下一个场景是变量。
请注意,有些人已经说过这可能不是如何使用类的最佳示例。抛开这个问题,我想把这个问题集中在具体的问题上。
我试图理解对象和类的原理,但在这种情况下,当它归结为它时,我遇到的麻烦就是不了解如何创建一个确定应该运行什么场景的变量。
另一位SO用户向我提供了行next = self.scenes.get(next).enter()
。作为一个初学者,如果我要将它翻译成英文,它会读取“从self.scenes dict获取与当前”变量“相关的对象,然后运行它的enter方法。但是从代码中我不知道看看下一次在整个游戏交互过程中如何变化?另外,实际上还有错误输出。
if
问题时,第一个场景TheTrolls会抛出一个错误(下面)。这是代码终端输出,代码在下面:
Traceback (most recent call last):
File "ex45.py", line 90, in <module>
GameEngine().run()
File "ex45.py", line 20, in run
next = self.scenes.get(next).enter()
AttributeError: 'NoneType' object has no attribute 'enter'
脚本:
from sys import exit
class Scene(object):
def enter(self):
pass
class GameEngine(object):
def __init__(self):
self.scenes = {'TheTrolls':TheTrolls(), 'MistyMountainsAndGollum':MistyMountainsAndGollum(), "Smaug":Smaug()}
def run(self):
next = 'TheTrolls'
while True:
if next == 'finished':
print "Well done! You won!"
exit(1)
next = self.scenes.get(next).enter()
# the scenes
class TheTrolls(Scene):
def enter(self):
print "You encounter a group of 3 trolls who sneak upon the company while asleep."
print "The trolls are discussing how to eat the dwarves."
print "You remember that trolls cannot survive in sunlight."
print "You must find a way to distract them till sunrise."
print "You also notice in their posession a shiny object"
print "Do you 1\)Fight them, 2\) Tell them jokes or 3\) Trick them"
trollAction = raw_input("What do you do? ")
if "fight" in trollAction:
print "The trolls grab you and fight over who gets to eat you."
print "They pull you apart limb by limb"
return 'death'
elif "joke" in trollAction:
print "You think of a joke to keep the trolls amused."
print "You run out at the trolls and yell 'A man didn't like his haircut, but it started to grow on him.'"
print" The trolls don't laugh at your joke and grab you and then eat you."
return 'death'
elif "trick" in trollAction:
print "By throwing your voice and talking like a troll you confuse the trolls."
print "You manage to make them fight amonst themselves"
print "The sun rises fro behind a rock and turns the trolls to stone."
print "Do you get out of there as fast as you can or take a look around the trolls posessions?"
else:
print "Does not compute"
return 'MistyMountainsAndGollum'
class MistyMountainsAndGollum(Scene):
def enter(self):
print "Since the trolls the company has been to Rivendel and have continued their journey."
print "You are now in the Misty Mountains."
print "Goblins come and chase you and you get seperated from everyone else."
print "While trying to find your way back you come across a lake deep in the mountain."
print "In the dark, you sit in despair and notice a metal object on the ground which you grab."
print "You grab the object and realise that it's a ring."
print "You hear someone talking. You have encountered Gollum."
print "Gollum talks to you, threatens to eat you but then agrees to a game of riddles because he is, in fact, just really bored and lonely."
print "[I'll add some riddles later but for now just complete the game - your programer]"
print "You play a game of riddles and Gollum is winning, till, you put your hand in your pocket and say"
print "aloud \"What have I got in my pocket?\"."
print "Golum freaks out but you put the ring on which makes you invisible."
print "In trying to chase you Gollum actually leads you out of the cave as you follow him."
return 'Smaug'
class Smaug(Scene):
def enter(self):
print "Something about chatting with Smaug and then getting th Arkenstone."
print "You got the Arkenstone from Smaug (Admittidley prematurely by the book version)."
print "Finished"
return 'finished'
class death(Scene):
def enter(self):
print "Well you did something wrong."
print "Game Over"
exit(1)
GameEngine().run()
答案 0 :(得分:1)
next
的值是enter()
方法的返回值。
收到错误后检查next
的值。我怀疑它可能是"death"
,这在场景数组中不存在。您还有一个错误,即回复trick
根本不会返回任何内容,因此next
将为空。