making a text room escape game?python 2

This is the place for queries that don't fit in any of the other categories.

making a text room escape game?python 2

Postby lfcpaul » Wed May 22, 2013 9:07 pm

This is almost the same as my post earlier but i messed up the intial question and just thought id start again
I get the error (bk is not defined)
Im doing this on android phone using py4a and sl4a.


Code: Select all

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."
lfcpaul
 
Posts: 7
Joined: Tue May 21, 2013 8:05 pm

Re: making a text room escape game?python 2

Postby metulburr » Wed May 22, 2013 9:13 pm

This is almost the same as my post earlier but i messed up the intial question and just thought id start again


What? Just post a post on the old thread then, no need to make a whole new thread beacuse of it
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: making a text room escape game?python 2

Postby Mekire » Wed May 22, 2013 11:32 pm

I also thought we covered the "bk is not defined" thing:
Mekire wrote:bk was defined local to your function. Other functions are not aware of its existence.

If you define a variable in a function other functions won't know about it unless it is returned from that function.
Personally for what you are doing I would suggest classes but there are some other ways to do it. You will either need the pertinent options returned from the function; Have the options known somewhere outside the function; Or query the user within the function. If your rooms were classes you could just ask for MyRoom.bk but such is not the case.

The example I showed you used the second option of having a dictionary of rooms known outside the function:
Code: Select all
def room1():
    print "blah blah blah"

def room2():
    print "jdjdndndk"

def hallway(room_dict):
    user = ""
    while user != "quit":
        user = raw_input("Select from [bdj,hhhh,quit]>")
        if user in room_dict:
            room_dict[user]()

if __name__ == "__main__":
    ROOMS = {"bdj":room2,"hhhh":room1}
    hallway(ROOMS)

Using cartesian coordinates (tuples) for dictionary keys is one easy way to construct a map of rooms.

-Mek
User avatar
Mekire
 
Posts: 1126
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: making a text room escape game?python 2

Postby lfcpaul » Thu May 23, 2013 1:08 am

Could you show me an example of using classes for it (if its easier) just a quick example i womt expect you to write something long lol.
I tried doing this(but the choices in the second while loop dont work it just plays the else:)

Code: Select all
import android
droid = android.Android()

print """Welcome to the room escape
     game, the aim is to find 100
     gold, once you have found it
     you will have completed the
     game.
     for help type inst."""

gold = 0

loop = 1
print """you are in the library
  there is a bookshelf,
  frontdesk and a pile of
  boxes, there is also a
  door leading to the classroom."""
while loop !=0:
    choice = raw_input("what do you want to do?:")
    if choice == "inst":
        print """to interact with objects
         type the name of the object
         or the name of the room you
         want to move to e.g. bookshelf
         to look in bookshelf, classroom
         to move to that room."""
    elif choice == "bookshelf":
        print "nothing here but some old books."
    elif choice == "frontdesk":
        print """roumaging around in the draw...
         Yes..You found 30 gold!"""
        gold = gold + 30
    elif choice == "boxes":
        print "something just grabbed your gold!"
        print "-10 gold."
        print """hes ran off in the dark you
         wont be getting that back."""
    elif choice == "classroom":
        loop = 0
    else:
        print "that didnt make sense."

l = 1
print """you open the door to the classroom...
    at least there's lights in here,
    you've got a table and cupboard to
    look in(and the door you just came through
    obviously)."""
while l != 0:
    choice == raw_input("what you going to do then?:")
    if choice == "table":
        print """nothing on here couldnt you
                 see that haha.."""
    elif choice == "cupboard":
        print """opening cupboard...
               Yes.. 80 gold aquired."""
        gold = gold + 80
    elif choice == "library":
        loop = 1
        l = 0
    else:
        print "that didnt make sense."
lfcpaul
 
Posts: 7
Joined: Tue May 21, 2013 8:05 pm

Re: making a text room escape game?python 2

Postby metulburr » Thu May 23, 2013 1:17 am

Could you show me an example of using classes for it (if its easier) just a quick example i womt expect you to write something long lol.


It would be better if you wnet back and started learning classes, then after learning the basics, go to this code and convert it to use classes
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: making a text room escape game?python 2

Postby Mekire » Thu May 23, 2013 1:55 am

You could check out Ichabod's thread here:
Interactive Fiction

However, if you are still not quite solid on the concepts regarding variable scope, you will need to first verse yourself in this. If you don't know how to return values, pass arguments, and communicate between functions, classes won't be of any help.

This page should be helpful to you regardless:
python-scopes-and-namespaces

-Mek
User avatar
Mekire
 
Posts: 1126
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests