2012-05-12 1 views
2

그래서 나는이 문제를 겪고 있습니다. 누군가 제가 올바른 방향으로 나를 가리킬 수 있다면, 너무 감사 할 것입니다. 내가 너를 위해 준비시켜 줘.파이썬에서 하나의 클래스에서 다른 클래스로 "옮겨 다니는 것"

나는 그래서 같은 클래스로 채워진 1 개 파이썬 파일/모듈의 이름 객실 :

모든 멋진 물건 권리가
class Intro(object): 

    def __init__(self): 
     # Setting up some variables here 

    def description(self): 
     print "Here is the description of the room you are in" 

? 그런 다음 다른 파일/모듈 (Engine)에 있습니다. 나는이있다 :

import rooms 

class Engine(object): 

    def __init__(self, first_class): 
     self.lvl = first_class 

    def play_it(self): 
     next_lvl = self.lvl 

     while True: 
      # Get the class we're in & run its description 
      next_lvl.description() # it works. 
      # But how do I get the next class when this class is done? 

내가 각 방/레벨 클래스 내에서 사용자의 결정을 기반으로 일하고자하는 일까지, 그것의 설명 기능/특성/w 새로운 클래스에 따라 엔진 호출. 말이 돼? 아니면 내가 이것에 대해 생각해야하는 또 다른 방법이 있습니까? 엉덩이 - 뒤로 때때로. 감사.

+0

[State Pattern] (http://en.wikipedia.org/wiki/State_pattern) ...을보세요. –

답변

3

다음에 룸에 사용할 클래스를 선택하십시오. 플레이어가 말할 때 예는

class Intro(object): 

    def __init__(self): 
     # Setting up some variables here 

    def description(self): 
     print "Here is the description of the room you are in" 

    def north(self): 
     return Dungeon() 

    def west(self): 
     return Outside() 

    #etc 

그래서 소개 룸에서 "서쪽으로 이동", 엔진은 room.west()를 호출하고 Intro 방은 Outside 방을 반환합니다.

희망 사항으로 충분합니다. 나는 당신이 당신 자신의 디자인을 만들고 싶어 할 것이라고 기대한다.

관련 문제