-6

I am creating a game from some code which I got from another question on here and I need help creating another chapter after I kill the bear with sword. How and where do I insert the goto command to start working on another chapter. Also if you have any advice or comments that would also be helpful. Here is the code. Feel free to tweak it and show me how.

p = 30

print 'You enter a dark room with two doors. Do you want to enter door #1 or door #2?'
door = raw_input('> ')

if door == "1":
    print 'Theres a giant bear eating what appears to be a human arm, though its so damaged it\'s hard to be sure'
    print 'what do you want to do?'
    print '#1 try to take the arm'
    print '#2 scream at the bear'
    print '#3 swing sword of death upon him'

    bear = raw_input('> ')

    if bear == "1":
        print 'You approach the bear slowly, never breaking eye contact. After what feel like a thousand years you are finally face to face with the bear. \nYou grab the arm and pull. of course the bear responds by eating you face. well done!'

    elif bear == "2":
        print 'You scream at the bear, a decent sound as well. The bear however was not impressed it would seem \nas he insantly eats your face. Well done!'

    elif bear == "3":
        print ' You swing the sword with all your might chopping the bears head completely off while blood drips to the floor'

    else:
        print 'Well, doing %s is probably better. Bear runs away.' % bear

elif door == "2":
    print 'You stare into the endless abyss of Bollofumps retina.'
    print 'Oh dear, seems the insanity of Bollofumps existence had driven you quite insane.'
    print '#1. drool'
    print '#2. scream and drool'
    print '#3. Understand the fabrics of molecula toy crafting'
    insanity = raw_input('> ')

    if insanity == "1" or "2":
        print 'Your body survives, powered by pure insanity!'

    else:
        print 'your mind melts into green flavoured jello! mmm!'

else:
    print 'you think for a while but suddenly a troll weilding a terrible looking knife comes through a trap door and shanks you!'
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
jinx
  • 1
  • 1

1 Answers1

3

I think if you carry on down this route, you wont need to add many more rooms/scenarios before the code becomes unmanageable. It will also contain a lot of almost duplicated code to do with presenting the numbered options, reading the response, and presenting the outcome (mostly it seems unpleasant death.....).

if you really want to continue doing it all yourself, consider perhaps some structured data that describes the location, the questions and the next location each response takes you to. That could be a dictionary containing various elements. e.g. descripition, choices (an array of choices to allow different numbers of choices for each scenario), next_location (an array of the same size as choices providing the loction of the next scenario your adventurer arrives at based on the choice). You then need a dictionary to hold alll these dictionaries where the key is the name of the location. To try and give you an example of what I mean, here are (slightly cut down) versions of 'dark room' and 'endless abyss'

game_dict ={}
sample_location_1 = { 'description' : 'You enter a dark room with two doors. Do you want to',
      'choices' : ['enter door #1','enter door #2','dark_room_invalid'],
      'next_location' :['bear_room', 'endless_abyss', 'dark_room'] }
game_dict['dark_room'] = sample_location_1
sample_location_2 = { 'description' : 'You stare into the endless abyss of Bollofumps retina.\nOh dear, seems the insanity of Bollofumps existence had driven you quite insane. What do you want to do ?',
      'choices' : ['drool','scream and drool', 'Understand the fabrics of molecula toy crafting', 'endless_abyss_invalid'],
      'next_location' :['death', 'death', 'death_by_troll', 'death_by_witty_reposte'] }
game_dict['endless_abyss'] = sample_location_2

Then you need code which reads and acts upon the data in a single location and presents the next location after action has been taken :

current_location = 'dark_room'
while current_location != 'death':
    print(game_dict[current_location]['description'])
    for num, option in enumerate(game_dict[current_location]['choices'][:-1]):
        print option
    action = int(raw_input('> ')) -1
    if action < len(game_dict[current_location]['choices']) -1:
        current_location = game_dict[current_location]['next_location'][action]
    else:
        current_location = game_dict[current_location]['next_location'][-1]
print "you are now dead....... consider this."

This is an oversimplified code example, you'll possibly want to put error handling around getting the response from the user. possibly error habndling around accidentally specifying a 'next_location' you haven't defined yet and you're going to need a monstrous dictionary of all the locations you might want.

Coding the game with ifs and gotos will just get out of hand very quicky. Think structured data and single routines to handle that data and get you to the next location, which is then handled by the same routine....

Doing it this way, you can then investigate writing routines to help you create the data interactively (i.e. prompting you for descriptions, choices and outcomes). That daqta could then be written to disk and possibly read in from file allowing you to have many adventures based around one re-usable 'engine'.

I hope that's been of some use.

R.Sharp
  • 296
  • 1
  • 8
  • It came back with an error. Traceback (most recent call last): File "C:/Python27/game4.py", line 4, in print(game_dict[current_location]['description']) NameError: name 'game_dict' is not defined – jinx Aug 23 '16 at 19:05
  • Did you include the block above which starts with "game_dict = {}". That's the definition of game_dict. – R.Sharp Aug 24 '16 at 08:52
  • sorry - keep forgetting not to press [return] : Pasting both blocks into an interactive python terminal (except the final print line as we need the while loop to execute immediately) runs giving me the expected prompts if I input '2' to the which door (the only prompt that works at this stage). You need to copy both blocks, in order, into your program file. From the error message being on line 4, I suspect you haven't. Similarly if you were to choose door 1, you'd get a traceback saying : "KeyError: 'bear_room'" as that hasn't been defined in my example (to save space). HTH - R – R.Sharp Aug 24 '16 at 09:16