#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Python 2. text based room.escape help?


    Hi, im a beginner teaching myself(using internet). Basically i made thisbgame but when i dont really understand how to call the def (the rooms) into play, any help would be much appreciated.
    Also if any one knows of a good guide to being able to make apps for phones that would be great.
    Btw Im doing all this on android using py4a and sl4a.

    Code:
    import android
    droid = android.Android()
    
    print """welcome to room escape
    the aim of this game is to find 
    100 pound."""
    print "type inst for instructions."
    pounds = 0
    
    def inst():
        print """try to find the key
        to inspect an object type its 
        name, to move to a different room
        type which room.e.g.type bookshelf
        to look in bookshelf, type classroom
        to move to classroom."""
    
    def library():
        print """you are in the library
        its quite spooky in here theres
        no lighting. 
        you have the frontdesk, bookshelf
        and boxes to look in.
        theres a door to the south that 
        leads to the classroom."""
        bk = "bookshelf"
        fd = "frontdesk"
        bx = "boxes"
        room2 = "classroom"
    
    def classroom():
        print """you opened the door to
        the classroom, at least theres lights
        in here.
        theres a table and cupboard to look 
        around."""
        t = "table"
        cb = "cupboard"
        inst = "inst"
        
    loop = 0
    while loop != 1:
        library()
        command = raw_input("what would you like to do?")
        if command == bk:
            print "nothing here but some ol books."
        elif command == fd:
            print "roumaging around...aha you found 50 pound."
            pounds = pounds + 50
        elif command == bx:
            print "kid jumps out and takes some of your money."
            pounds == pounds - 10
        elif command == inst:
            inst()
        elif command == room2:
            classroom()
            command = raw_input("what you going to do:")
            if command == t:
                print """nothing on the table couldnt you 
                have just looked from where you are or are 
                you blind."""
            elif command == cb:
                print """yes you just got yourself 60
                pound,
                well done."""
                pounds = pounds + 60
                print """you have completed the game
                congratulations."""
                loop = 1
            elif command == inst:
                inst()
            else:
                print "thats not a command."
        else:
            print "thats not a command."
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    Analyzing the classroom function.


    Code:
    # use textwrap for prettier output
    import textwrap
    
    def classroom():
    
        text = """you opened the door to the
                  classroom, at least theres
                  lights in here.   Theres a
                  table and cupboard to look
                  around."""
    
        print(textwrap.fill(text, width=35))
    
        # these variable definitions are local
        # to the function.  You can make them
        # known at module scope with global
        # statement.  Although a bad idea,
        # (classes solve this problem,)
        # globals could improve your
        # failing python program.
    
        global t, cb, inst
    
        t = "table"
        cb = "cupboard"
        inst = "inst"
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Thanks for replying. If classes are easier could you show me a quick example of classes please.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    Slow example.


    Code:
    '''
        Run the doctest:  $ python3 -m doctest p.py
    
        #>>> import imp
        #>>> import your_fun_game as p
        #>>> imp.reload(p); b,c = p.backpack,p.classroom  # give short names to the objects
        #<module 'p' from 'p.py'>
    
    
        >>> classroom = Room(
        ...     items = [Item(*args) for args in zip('table cupboard inst'.split(), ('', 'closed', 'wtf?'))],
        ...     description = """you opened the door to    the classroom, at least theres lights    in here.    theres a table and cupboard to look    around.""",
        ...     )
        ... 
        >>> backpack = Room(
        ...     "Granpa's moth eaten 1958 daypack.  Better mend it or you'll lose lunch.",
        ...     )
        ... 
        >>> b, c = backpack, classroom  # shorten names
        >>> print(b)                # backpack doesn't store anything.
        Granpa's moth eaten 1958
        daypack.  Better mend it or
        you'll lose lunch.
        >>> print(c)                # classroom contains an inst
        you opened the door to    the
        classroom, at least theres
        lights    in here.    theres a
        table and cupboard to look
        around. You see a  table, a
        closed cupboard, a wtf? inst.
        >>> b[0] = c['inst']        # transfer the inst to the backpack
        >>> print(b)                # backpack now has it
        Granpa's moth eaten 1958
        daypack.  Better mend it or
        you'll lose lunch. You see a
        wtf? inst.
        >>> print(c)                # and the room does not!
        you opened the door to    the
        classroom, at least theres
        lights    in here.    theres a
        table and cupboard to look
        around. You see a  table, a
        closed cupboard.
        >>> b.transfer('inst', c)   # inspiring the great transfer method
        >>> print(b)                # out of backpack
        Granpa's moth eaten 1958
        daypack.  Better mend it or
        you'll lose lunch.
        >>> print(c)                # into classroom
        you opened the door to    the
        classroom, at least theres
        lights    in here.    theres a
        table and cupboard to look
        around. You see a  table, a
        closed cupboard, a wtf? inst.
        >>> c.transfer('clock', b)  # example of handled error condition
        Source does not contain clock.
        >>>
    '''
    
    import textwrap
    
    class Item:
    
        def __init__(self, name, state = None):
            self.name = name
            self.state = state
    
        def __eq__(self, name):
            return self.name == name
    
        def __str__(self):
            return '{} {}'.format((self.state or ''), self.name)
    
        def change_state(self, newstate):
            self.state = newstate
    
    class Room:
    
        def __init__(self, description, items = []):
            self.description = description
            self.items = items
    
        def you_see(self):
            result = ''
            if self.items:
                result = 'You see a ' + ', a '.join(str(item) for item in self.items) + '.'
            return result
    
        def __str__(self):
            return textwrap.fill(
                self.description + '\n' + self.you_see(),
                width=32)
    
        def __getitem__(self, item):
            '''
                item is an item name
            '''
            items = self.items
            i = items.index(item)
            return_value = items[i]
            del items[i]
            return return_value
    
        def __setitem__(self, item, value):
            '''
                value is an Item.  item is ignored.
            '''
            assert isinstance(value, Item)
            self.items.append(value)
    
        def transfer(self, name, other):
            try:
                item = self[name]
            except:
                print('Source does not contain {}.'.format(name))
                return
            try:
                other[0] = item
            except:
                print('Sink cannot contain {}.'.format(name))
                #if random < 0.2: print('you dropped it---forever lost.') else:
                self[0] = item
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo