工作代码在这里:http://codepad.org/L0OkeHlk
from sys import exit
class Map(object):
def __init__(self, start):
self.start = start
def play(self):
next = self.start
while True:
print "\n---------"
room = getattr(self,next)
next = room()
def death(self):
exit(1)
def redroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (RED ROOM)"
return 'death'
def whiteroom(self):
prompt = raw_input("> ")
if "red" in prompt:
return "redroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (WHITE ROOM)"
return 'death'
def blueroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "red" in prompt:
return "redroom"
else:
print "DOES NOT COMPUTE (BLUE ROOM)"
return 'death'
class Engine(object):
pass
x = Map("redroom")
x.play()
以上作品。但这只是因为我在一堂课中做了所有事情。我想把戏剧和死亡功能放在他们自己的课堂上。
我的问题来自两个地方。如何让Engine类调用map类中的函数来使游戏工作?我如何知道在Map / Engine类中初始化的内容?我知道我应该初始化一些东西,但我不知道如何提出它。在决定初始化内容时,我应该首先考虑什么?
无效代码(我的最佳尝试):http://codepad.org/ELlaHQiB
from sys import exit
class Map(object):
def redroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (RED ROOM)"
return 'death'
def whiteroom(self):
prompt = raw_input("> ")
if "red" in prompt:
return "redroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (WHITE ROOM)"
return 'death'
def blueroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "red" in prompt:
return "redroom"
else:
print "DOES NOT COMPUTE (BLUE ROOM)"
return 'death'
class Engine(object):
def __init__(self, start):
self.start = start
def play(self):
next = self.start
while True:
print "\n---------"
room = getattr(self,next)
next = room()
def death(self):
exit(1)
x = Engine("redroom")
x.play()
另外,我不确定播放功能。它的工作原理只是因为我从我正在阅读的教程中得到它。下一个变量成为self.start然后在while循环中变成room()的目的是什么?
答案 0 :(得分:0)
这就是我要做的。
class Engine(object):
def __init__(self,rooms):
self.rooms = rooms
self.current = 0
def next(self):
self.rooms[self.current]()
self.current += 1
if len(self.rooms) == self.current:
self.current = 0
# or whatever logic you want.
m = Map()
rooms = [m.redroom, m.whiteroom, m.blueroom]
e = Engines(rooms)
e.next() # should call redroom of class Map
e.next() # should call whiteroon etc