Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help with game
#1
i am making a choose your own adventure game and need help with the coding. i am mid way throught, and tried to play it so far but some of the outcomes dont work. i would like someone to help me fix the coding rather than tell me whats wrong. here is the coding:


#this is a choose your own adventure game

import time#makes it possible to have time delay
def wait():
    time.sleep(1)

def intro():
    
    print ('Hello!')
    wait()#makes a time delay
    print ('Ok')
    wait()
    adventure = input('Do you want to start the adventure???')
    print ('')
    if adventure == 'yes' or adventure == 'Yes' or adventure == 'yeah' or adventure == 'Yeah':    
        print ('Ok, lets get started!')
    elif adventure != 'yes' or adventure != 'Yes' or adventure != 'yeah' or adventure != 'Yeah':
        print ("That's lame. I'll just pretend you said yes...")#starts the game
    wait()
    print('')
    name = input('First, what is your name?')
    print('')
    wait()
    print ('Ok lets get started ' + name)
    wait()
    print('')
    print ('You have been sent by your kingdom to slay a dragon!  If you come home victorious you will be crowned king! This, however, is a challenge no one has ever come home from...')
    wait()
    print('')
    print('You must either bring back the dragons head or some of the dragons treasure.')
    wait()
    print('')
    print ('You will have to make many decisions that will affect your adventure throughout the game. Some may be good and some may be bad...')
    wait()
    print('')
    print ('You start your adventure in your kingdom...')#finishes intro and starts the adventure
    wait()
    travel()

def travel():
    print('')
    print ('You have been sent on your quest and first have to get to the cave.')
    print('')
    choice1 = input("you have 2 options: enter 'a' for going via a boat or enter 'b' for going via horse.")#this decided how to travel to the cave
    print('')
    if choice1 != ('a') and choice1 != ('b'):
        print ('this is not a valid option try again')#invalid
        travel()
    elif choice1 == ('a') or choice1 == ('b'):
        print('')
        print ('Ok!')
        tunnel()

def tunnel():
    print('')
    wait()
    choice2 = input("You have arrived outside the cave... you walk inside and see two tunnels. Enter 'a' for the right tunnel or Enter 'b' for the left tunnel.")
    print('')
    if choice2 != ('a') and choice2 != ('b'): #second choice
        print ('this is not a valid option try again')
        tunnel()
    elif choice2 == ('a') or choice2 == ('b'):
        print ('Ok!')
        noexit()

def noexit():
    print('')
    wait()
    choice3 = input("You enter the tunnel...BANG!!!...A metal door closes behind you. Enter 'a' to continue walking or Enter 'b' to try to find a way to open the door.")
    print('')
    if choice3 != ('a') and choice3 != ('b'): 
        print ('this is not a valid option try again')
        noexit()
    if choice3 == ('b'):
        noexit1()
    if choice3 == ('a'):
        print ('ok')
        torch()

def noexit1():
    if choice3 == ('b') and choice2 == ('a'):
        print ('You start to search around the door looking for a switch, when you hear a very loud roar...you turn around and realise you are facing the dragon... you start to raise your sword, but its too late, the dragon kills you :-(.')
        restart()
    elif choice3 == ('b') and choice2 == ('b'):
        print ('You start to search around the door looking for a switch, when you feel a button.')
        print('')
        wait()
        choice5 = input("Do you press it? Enter 'a' to press it or Enter 'b' to leave it and continue looking.")
    if choice5 != ('a') and choice5 != ('b'):
        print ('this is not a valid option')
        noexit()
    if choice5 == ('a'):
       button()
    if choice5 == ('b'):
        buttonorswitch()

def button():
     print ('You press the button and...nothing happened! You stand up and look around - sundenly the floor opens up beneath you and you fall to your death')
     restart()

def buttonorswitch():
    choice6 = ("You continue looking and find a switch. You decide to either press the button or the switch. Enter 'a' to press the button or Enter 'b' to flick the switch")
    if choice6 != 'a' and choice6 != 'b':
        print ('this is not a valid option')
        buttonorswitch()
    elif choice6 == 'a':
        button()
    elif choice6 == 'b':
        switch()

def switch():
    print ('You flick the switch and...the door opened! But thats not it...another door openes which reveals loads of treasure!')
    treasurehome()
    
def torch():
    print ('')
    wait()
    print ('as you continue, you notice the way ahead is darker.')
    choice4 = input("Do you pick up a torch? Enter 'a' for yes and Enter 'b' for no.")
    if choice4 != ("a") and choice4 != ("b"):
        print('This is not a valid option try again')
        torch()
   

def restart():
    startover = input('do you want to try again?')
    if startover == ('yes') or startover == ('y') or startover == ('ok'):
        intro()
    elif startover == ('no') or startover == ('nope') or startover == ('n'):
        quit()
    else:
        print ('that is not a valid option try again')
        restart()
def treasurehome():
    print ('You return home with loads of treasure and become the king!')
    restart()
intro()
Reply
#2
The first thing that comes to mind is your if statements. Take this:

if adventure == 'yes' or adventure == 'Yes' or adventure == 'yeah' or adventure == 'Yeah':
An easier way to do this is:

if adventure in ('yes', 'Yes', 'yeah', 'Yeah'):
But notice that you are searching for the same things, upper and lower case. The standard way to deal with this is to test against a lowercase version:

if adventure.lower() in ('yes', 'yeah'):
Then you have:

elif adventure != 'yes' or adventure != 'Yes' or adventure != 'yeah' or adventure != 'Yeah':
First of all, this is not necessary. Since this is the elif, it only triggers if the preceding if didn't trigger. So we already know that adventure is none of those values. But say this was an if statement, so we didn't already know it wasn't any of these values. It would be true if adventure == 'yes'. The first check against 'yes' would return False, but the checks against 'Yes', 'yeah', and 'Yeah' would all return True. With the or operator, if any of them are True, the whole expression is true. You want the and operator, or using my version:

if adventure.lower() not in ('yes', 'yeah'):
Again, we don't need this in your case, because the preceding if statement already showed us that it's none of those values. In your code, this could be replaced with a plain else.

Also, check out this tutorial on text adventures.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
i understand that. however surely that is just tiding up my code. please help me with the errors :)
Reply
#4
(Feb-11-2017, 12:50 PM)Darbandiman123 Wrote: please help me with the errors :)
You never posted any errors. And we are too busy to run through your program to sniff them out.
https://python-forum.io/misc.php?action=help&hid=20

And yes it is tiding up your code. However the more drawn out it is the less people want to help you. So you might want to consider tiding up your code more important, as the less code we have to look at the better chance we respond. This is such a common issue that we made a tutorial post about it.
Recommended Tutorials:
Reply
#5
Oh, I misread your question. But your question isn't very clear. I ran through the game, got to the cave, went in the tunnel, picked up the torch, and then the game stopped. It didn't give me an error, but I don't really know what you expected it to do after that, so I can't tell you how to fix it. I guess the torch function should call some other function, but I'm not sure which one that should be.

I think this is partly because of the structure of your program. The structure of your map and your choices is embedded in the structure of your program. So the question of what to do after picking up the torch becomes a programming issue. The tutorial I linked to shows you how to separate the structure of your game from the structure of your program. This makes problems in either one easier to detect and repair.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
yes i see what you mean. basically i havent finished the game but for example if you get to the cave, go into the right tunnel and look for a switch you run into a problem
Reply
#7
You didn't follow metulburr's link did you? Because if you had, you would have known to post the full traceback of the error you are getting. That way, I wouldn't have had to run the program and find it myself.

The problem is that choice3 isn't defined in noexit1, it's defined in noexit. One function does not have access to another's variables, unless you pass them as parameters or return them with the return statement.

This is a symptom of what I was talking about. Because your game structure is in your program structure, you have to keep repeating the same program structure over and over again. This is an invitation to bugs. Also note that the way you are programming this means that the game can only move forward, it can't move back. If your player goes forward after the door slams behind him and gets the torch, they can't then go back and try and open the door. Games generally operate on a main loop. That loop waits for the player's action, processes the result of that action, and then waits for another action. Games based on an if/else structure either have to be very short or or become a huge mess very quickly.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020