Login | Register

Nerd Paradise

It's Omni's fault.
Open
Original issue
I am writing a Text-Based Game in Python. Nothing big, considering my being new to the language. However, I don't currently have the time to resolve this one issue:
x = 5
y = 5
HP = 10.0
com = '$HELP'
ongoing = True

def move_x(x, com, e, w):
    if e and com == 'e':
        x += 1
        return x
    elif w and com == 'w':
        x -= 1
        return x
    else:
        return 'error'

def move_y(y, com, n, s):
    if n and com == 'n':
        y += 1
        return y
    elif s and com == 's':
        y -= 1
        return y
    else:
        return 'error'

print 'Welcome to a random TBG prototype.'
print 'v.0.0.0'

while ongoing:
    n = False
    s = False
    e = False
    w = False
    if com == '$HELP':
        print 'command list'

        
    if com == 'location':
        print '[' + str(x) + ',' + str(y) + ']'

    if com == 'HP':
        print HP

    if x == 5 and y == 5:
        print 'You are in the STARTING ROOM. To the EAST is the EASTERN TESTING ROOM and to the NORTH is the NORTHERN TESTING ROOM.'
        e = True
        n = True
    elif x == 6 and y == 5:
        print 'you are in the EASTERN TESTING ROOM. To the WEST is the STARTING ROOM and to the EAST is the INSTANT DEATH ROOM.'
        e = True
        w = True
    elif x == 5 and y == 6:
        print 'You are in the NORTHERN TESTING ROOM. To the SOUTH is the STARTING ROOM.'
        s = True
    elif x == 7 and y == 5:
        print 'Welcome to the INSTANT DEATH ROOM. Are you sure you want to die? [y/n]'
        ans = 0
        while ans != 'y' or 'n':
            ans = raw_input()
            if ans == 'y':
                HP -= 10
                break
            elif ans == 'n':
                print 'To the WEST is the EASTERN TESTING ROOM.'
                w = True
                break
            else:
                print 'please answer with "y" or "n".'
    else:
        print 'you somehow moved off the map. You are now in the STARTING ROOM ([5,5]).'
        x = 5
        y = 5
        e = True
        n = True
    com = raw_input()
    while not (com == '$HELP' or com == 'location' or com == 'n' or com == 's' or com == 'e' or com == 'w' or com == 'quit' or com == 'HP'):
        print 'Unknown command. Please try again.'
        com = raw_input()
    if (move_x(x, com, e, w) == 'error') and (move_y(y, com, n, s) == 'error'):
        if not(com == '$HELP' or com == 'location' or com == 'quit' or com == 'HP'):
            print 'you cannot go there.'
    else:
        x = move_x(x, com, e, w)
        y = move_y(y, com, n, s)
    if HP<=0 or com == 'quit':
        ongoing = False
        if HP <=0:
            print 'GAME OVER'

I put up the whole thing, because I might have made a mistake somewhere that I don't know about. Anyhow, here's the problem:
Welcome to a random TBG prototype.
v.0.0.0
command list
You are in the STARTING ROOM. To the EAST is the EASTERN TESTING ROOM and to the NORTH is the NORTHERN TESTING ROOM.
e
you somehow moved off the map. You are now in the STARTING ROOM ([5,5]).
n
you somehow moved off the map. You are now in the STARTING ROOM ([5,5]).

EDIT: I will cut the code down when I have time again.
[Quote] [Link]
Closed
Solution
Look at your move functions.

Suppose you want to move east. 'e'.
if e is True, then x += 1. <-- This is the condition that gets hit.
elif w is True, then x -= 1.
otherwise, return 'error'

That's fine.

Now consider y_move. Remember, we're moving EAST.
If n is True, then y -= 1 (Nope)
Elif s is True, then y += 1 (Nope)
Return "error" (This is the final result)

Aside from that, if you want to efficiently write one of these, it's not a good idea to put game content in code. You'll want to put all the rooms in a lookup table of some sort. Otherwise you'll end up with a monstrosity of a deeply nested if/elif/else block.

Here's roughly how I would approach the problem. This is just a rough sketch to give you an idea. Don't actually try to run this.


# First start with a lookup table like this. The key would be the room location and 
# the value would be whatever information you would need for your engine to interpret
# the room correctly and display the information and interactivity that you want.
# In this case, I just have the name of the room and a special behavior handler. The
# Behavior handler can be None if the room does nothing. You can add more complexity 
# to this table if needed. But the key philosophy is if this table were hidden from
# someone reading the code, they would have no idea about specifics of your game plot/map.  
rooms = {
{ '5-5' : ('STARTING ROOM', None) },
{ '7-5' : ('INSTANT DEATH ROOM', prompt_instant_death) }
...
}

# Behavior handler
def prompt_instant_death():
  print "Are you sure you want to die? [y/n]"
  choice = raw_input()
  ...etc...

def get_room_info(x, y):
  global rooms
  key = str(x) + '-' + str(y)
  return rooms.get(key)

def does_room_exist(x, y):
  return get_room_info(x, y) == None

def can_go_east_from(x, y):
  return does_room_exist(x + 1, y)

def main_loop():
  x = 5
  y = 5
  ongoing = True
  while ongoing:
    current = get_room_info(x, y)
    name = current[0]
    handler = current[1]
    print "You are in " + name + "."
    if can_go_east_from(x, y):
      east_room = get_room_info(x + 1, y)
      print "To the east is " + east_room[0]
    if can_go_west_from(x, y):
      ...
    # north, south, etc.
    
    choice = raw_input()
    if choice == 'e':  x += 1
    elif choice == 'w': x -= 1
    elif choice == 's':  y += 1
    elif choice == 'n': y -= 1
    elif choice == '$HELP': ...
    elif choice == 'etc...
[Quote] [Link]
Thank you! I am new to python, so chipping off the little errors in your code took me a while, as I have never had the chance to use dictionaries before, but I will take your advice and do that.
[Quote] [Link]
If you want, you can take it a step further and take the datastructure out of the code file altoghether and put it in a separate file in a different format like XML. I believe gws wrote something very similar to this and posted it on a previous NP.

Ah, yes, here it is
[Quote] [Link]
Actually writing runnable code to make that idea work is something I keep remembering that I should really ought to do some day.
[Quote] [Link]
new problem here:
I have tried your method, Blake, and it has done wonderful things, but I do have a problem with the behavior. More precisely, I intend on creating an easter egg in MAIN HALL, where inputting 'pet dog' prints a message. Here's the code:
#defining special functions, for within rooms
def pet_dog(com):
  if com == 'pet dog':
    return 'You pet the dog. He looks happy!'
  return 'error'

#room dictionary
#legend: x,y : ([0]'name', [1]'general description', [2]'detailed description', [3](gp, other, items), [4]'easter eggs' and other behavior),

rooms = {'5,6' : ['MAIN HALL','You are in a long hall, with doors on each end and each side.',
         'The walls are of wood, and on a wardrobe there are some board games. To the South of you you see a dog.',[0],
         pet_dog, None]}



#defining game variables:
com = 'e'
later:

    com = raw_input('command: ')
    if this_room[4] != None and this_room[4] != error: print this_room[4]

I know am pretty sure that this would work if raw_input was above the dictionary, and I used pet_dog(com) in this_room[4].
(this_room is the what I got in get_room_info)
From what I can see, I need the function above the dict, so I can use it in this_room[4], and I need the dict above the game engine, so I can use the rooms in it, but I also need the game engine above the dict, or, more precisely, the raw_input.
[Quote] [Link]
A few restructuring moves may be in order to accomodate that. Pastebin your full code and I'll take a look at it.
[Quote] [Link]
here
EDIT: newer v. with help from gws here
[Quote] [Link]
Current Date: 13 Ineo 10:2Current Time: 11.64.39Join us in IRC...
Server: irc.esper.net
Channel: #nerdparadise
Your IP: 54.234.126.92Browser: UnknownBrowser Version: 0