0

工作代码在这里: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/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() 的目的是什么?

4

1 回答 1

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  
于 2012-04-08T00:33:27.690 回答