让两个类游戏互动
我知道这个网站上关于Zed Shaw的《Learn Python the Hard Way》有很多问题,但我在做第42个练习,额外的第3个任务时遇到了困难。
我在让Engine这个类有效启动并切换到Map这个类时遇到了麻烦,这样游戏才能开始执行我定义的不同功能。
代码如下:
from sys import exit
from random import randint
class Map(object):
def death():
print quips[randint (0, len(quips)-1)]
exit(1)
def princess_lives_here():
print "You see a beautiful Princess with a shiny crown."
print "She offers you some cake."
eat_it = raw_input(">")
if eat_it == "eat it":
print "You explode like a pinata full of frogs."
print "The Princess cackles and eats the frogs. Yum!"
return 'death'
elif eat_it == "do not eat it":
print "She throws the cake at you and it cuts off your head."
print "The last thing you see is her munching on your face. Yum!"
return 'death'
elif eat_it == "make her eat it":
print "The Princess screams as you cram the cake in her mouth."
print "Then she smiles and cries and thank you for saving her."
print "She points to a tiny door and says, 'The Koi needs cake too.'"
print "She gives you the very last bit of cake and shoves you in."
return 'gold_koi_pond'
else:
print "The Princess looks at you confused and just points at the cake."
return 'princess_lives_here'
class Engine(Map):
def _init_(self, start):
self.quips = [
"You died. You suck at this.",
"Your mom would be proud, if she were smarter",
"Such a luser.",
"I have a small puppy that's better at this."
]
self.start = start
def play(self):
next = self.start
while True:
print "\n-----"
room = getattr(self, next)
next = room()
e = "princess_lives_here".Map.Engine
e.play()
问题不在于Map类中的其他函数,而是如何从Engine类切换到Map类,这样游戏才能继续。可能是没有在Engine类中定义'Map',或者是在我最后设置的变量中没有正确运行play()命令。
如果你能给我一些关于类之间互动的链接,那就太好了。再次感谢!
2 个回答
1
from sys import exit
from random import randint
class Map(object):
def __init__(self):
self.quips = [
"You died. You kinda suck at this.",
"Your mom would be proud. If she were smarter.",
"Such a luser.",
"I have a small puppy that's better at this."
]
def death(self):
print self.quips[randint(0, len(self.quips)-1)]
exit(1)
def princess_lives_here(self):
print "You see a beautiful Princess with a shiny crown."
print "She offers you some cake."
eat_it = raw_input("> ")
if eat_it == "eat it":
print "You explode like a pinata full of frogs."
print "The Princess cackles and eats the frogs. Yum!"
return 'death'
elif eat_it == "do not eat it":
print "She throws the cake at you and it cuts off your head."
print "The last thing you see is her munching on your torso. Yum!"
return 'death'
elif eat_it == "make her eat it":
print "The Princess screams as you cram the cake in her mouth."
print "Then she smiles and cries and thanks you for saving her."
print "She points to a tiny door and says, 'The Koi needs cake too.'"
print "She gives you the very last bit of cake and shoves you in."
return 'gold_koi_pond'
else:
print "The princess looks at you confused and just points at the cake."
return 'princess_lives_here'
def gold_koi_pond(self):
print "There is a garden with a koi pond in the center."
print "You walk close and see a massive fin poke out."
print "You peek in and a creepy looking huge Koi stares at you."
print "It opens its mouth waiting for food."
feed_it = raw_input("> ")
if feed_it == "feed it":
print "The Koi jumps up, and rather than eating the cake, eats your arm."
print "You fall in and the Koi shrugs than eats you."
print "You are then pooped out sometime later."
return 'death'
elif feed_it == "do not feed it":
print "The Koi grimaces, then thrashes around for a second."
print "It rushes to the other end of the pond, braces against the wall..."
print "then it *lunges* out of the water, up in the air and over your"
print "entire body, cake and all."
print "You are then pooped out a week later."
return 'death'
elif feed_it == "throw it in":
print "The Koi wiggles, then leaps into the air to eat the cake."
print "You can see it's happy, it then grunts, thrashes..."
print "and finally rolls over and poops a magic diamond into the air"
print "at your feet."
return 'bear_with_sword'
else:
print "The Koi gets annoyed and wiggles a bit."
return 'gold_koi_pond'
def bear_with_sword(self):
print "Puzzled, you are about to pick up the fish poop diamond when"
print "a bear bearing a load bearing sword walks in."
print '"Hey! That\' my diamond! Where\'d you get that!?"'
print "It holds its paw out and looks at you."
give_it = raw_input("> ")
if give_it == "give it":
print "The bear swipes at your hand to grab the diamond and"
print "rips your hand off in the process. It then looks at"
print 'your bloody stump and says, "Oh crap, sorry about that."'
print "It tries to put your hand back on, but you collapse."
print "The last thing you see is the bear shrug and eat you."
return 'death'
elif give_it == "say no":
print "The bear looks shocked. Nobody ever told a bear"
print "with a broadsword 'no'. It asks, "
print '"Is it because it\'s not a katana? I could go get one!"'
print "It then runs off and now you notice a big iron gate."
print '"Where the hell did that come from?" You say.'
return 'big_iron_gate'
def big_iron_gate(self):
print "You walk up to the big iron gate and see there's a handle."
open_it = raw_input("> ")
if open_it == 'open it':
print "You open it and you are free!"
print "There are mountains. And berries! And..."
print "Oh, but then the bear comes with his katana and stabs you."
print '"Who\'s laughing now!? Love this katana."'
return 'death'
else:
print "That doesn't seem sensible. I mean, the door's right there."
return 'big_iron_gate'
class Engine(object):
def __init__(self, o, map):
self.map = map
self.obj = o
def play(self):
nextmap = self.map
while True:
print "\n--------"
room = getattr(self.obj, nextmap)
nextmap = room()
a_map = Map()
a_eng = Engine(a_map, "princess_lives_here")
a_eng.play()
当然可以!请把你想要翻译的内容发给我,我会帮你把它变得更简单易懂。
3
这里有很多问题。
你的代码缩进不正确;看起来 __init__()
和 play()
应该缩进,成为 Engine 类的一部分,而 death()
和 princess_lives_here()
应该缩进,成为 Map 类的一部分。
类的 __init__
方法两边需要用双下划线,而不是单下划线。
这一行:
e = "princess_lives_here".Map.Engine
在 Python 中是没有意义的;它试图在一个字符串中寻找一个 "Map" 属性。如果你想创建一个类的新对象,你应该这样写:
e = Engine( "princess_lives_here" ) # or Map( "...")?
我不太明白你为什么需要 Map 和 Engine 两个东西,但我猜测你可能想让 Map 成为 Engine 的子类(Engine 是所有游戏通用的,而 Map 是这个游戏特有的),或者你想让它们成为独立的对象,其中 Engine 和 Map 互相引用。
目前你的代码是把 Engine 定义成了 Map 的子类。如果它们应该是独立的,那么你应该把 Engine 定义为一个对象,让它的 __init__
方法接收一个起始点和一个地图实例,然后这样做:
m = Map()
e = Engine( m, "princess_lives_here" )