patternpythonMinor
"Lost in the Woods" text-based game
Viewed 0 times
thelosttextgamebasedwoods
Problem
This is my first Python text-based game. Let me know if there is something I should improve on or fix in the way that my code is written because I feel that I am being very redundant in my code, but I'm just learning this stuff.
Also note that
this cabin will self destruct in.....
```
#Lost In the Woods - A simple text based game
from sys import exit
def start():
print "You are in the middle of the woods alone and scared!"
print "There is no one around but yourself."
print "You look around and wonder....."
print "What will you do?"
print "1.move forward\n2.stay still"
choice = raw_input("> ")
if "move" in choice:
bear_fight()
else:
print "Well your still at the same spot."
start()
def bear_fight():
print "Ahead of you is a huge hungry bear with razor sharp claws"
print "Hes looks angry too!"
print "Do you....."
print "1.throw rock\n2.play dead\n3.run away\n"
choice = raw_input("> ")
if "run" in choice:
print "Duhhh.... Did you think you can out run a bear?"
dead("The bear catches up and has you for dinner, Good Job")
elif "play dead" in choice:
print "The bear laughs at you and chews but off."
dead("You lost your but and lost this game")
else:
print "You deafted the bear with a rock! Hows that possible?"
print "Anyways, You contine to move forward untill......"
cabins()
def cabins():
print "You come acroos two cabins."
print "One of the left and one on the right"
print "Whcih cabin do you pick?"
print "1.left\n2.right\n"
choice = raw_input("> ")
if "left" in choice:
cabin_one()
else:
cabin_two()
def cabin_two():
print "You apprach the cabin on the right."
print "You notice a note on the cabin door."
print "Will you......"
print "1.read the note\n2.open the door"
choice = raw_input("> ")
if "read" in choice:
let
Also note that
note.txt just says:this cabin will self destruct in.....
```
#Lost In the Woods - A simple text based game
from sys import exit
def start():
print "You are in the middle of the woods alone and scared!"
print "There is no one around but yourself."
print "You look around and wonder....."
print "What will you do?"
print "1.move forward\n2.stay still"
choice = raw_input("> ")
if "move" in choice:
bear_fight()
else:
print "Well your still at the same spot."
start()
def bear_fight():
print "Ahead of you is a huge hungry bear with razor sharp claws"
print "Hes looks angry too!"
print "Do you....."
print "1.throw rock\n2.play dead\n3.run away\n"
choice = raw_input("> ")
if "run" in choice:
print "Duhhh.... Did you think you can out run a bear?"
dead("The bear catches up and has you for dinner, Good Job")
elif "play dead" in choice:
print "The bear laughs at you and chews but off."
dead("You lost your but and lost this game")
else:
print "You deafted the bear with a rock! Hows that possible?"
print "Anyways, You contine to move forward untill......"
cabins()
def cabins():
print "You come acroos two cabins."
print "One of the left and one on the right"
print "Whcih cabin do you pick?"
print "1.left\n2.right\n"
choice = raw_input("> ")
if "left" in choice:
cabin_one()
else:
cabin_two()
def cabin_two():
print "You apprach the cabin on the right."
print "You notice a note on the cabin door."
print "Will you......"
print "1.read the note\n2.open the door"
choice = raw_input("> ")
if "read" in choice:
let
Solution
Your code is indented well, and appears to have no problems in this way.
When you start the game, you should have all code that would otherwise just run defined in a method like this:
You can learn about this more here: What does
It is very good that your
You should probably run your text through a spell checker, should this be "nobody", "civilization", "Burger King" (capitalized as a proper noun), and "sweat"?
"No bobdy comes for you."
"You see civiliztion and stop at the first buger king."
"You sweet to death and die!"
There are many other misspellings in here.
I would probably create a method to print the options the user has, and you can pass the options as a tuple:
This can now be called like this:
Your game will be a little boring once the user has played it a couple times. There seems to be no element of randomness, so there is only one path to victory. The user won't want to lose, so once they realize this, they probably won't play your game. To defeat this problem, you should probably use a random number generator to randomly choose options, which can be weighted to give certain actions a higher probability of happening.
In
Your
When you open and read the note at cabin 2, I would not put that in a file by itself. It does not describe what the program is doing any more than just printing the note with
When you start the game, you should have all code that would otherwise just run defined in a method like this:
if __name__ == "__main__":
start()You can learn about this more here: What does
if __name__ == “__main__”: do?It is very good that your
main delegates all the work of starting a game to a start() method.You should probably run your text through a spell checker, should this be "nobody", "civilization", "Burger King" (capitalized as a proper noun), and "sweat"?
"No bobdy comes for you."
"You see civiliztion and stop at the first buger king."
"You sweet to death and die!"
There are many other misspellings in here.
I would probably create a method to print the options the user has, and you can pass the options as a tuple:
def print_options(options):
for i in range(0, len(options)):
print str(i + 1) + ". " + options[i]This can now be called like this:
print_options(("t1", "t2", "t3"))Your game will be a little boring once the user has played it a couple times. There seems to be no element of randomness, so there is only one path to victory. The user won't want to lose, so once they realize this, they probably won't play your game. To defeat this problem, you should probably use a random number generator to randomly choose options, which can be weighted to give certain actions a higher probability of happening.
In
start(), you recursively call start() if the user chooses to stay still. In this case, it probably is not a big deal because you don't create a bunch of variables and the user won't choose to stay still forever, but it isn't always a good idea to do this because the call will be added to the stack recursively until the stack overflows (if this point is ever reached); instead, you should use a loop to display the prompt until the user chooses to move.Your
win() and dead() methods are essentially the same. You should combine these into one method for ending the game.When you open and read the note at cabin 2, I would not put that in a file by itself. It does not describe what the program is doing any more than just printing the note with
print - in fact, I think print shows what is happening better. What it is doing is describing what the user is doing, which is very different than what the program itself is doing.Code Snippets
if __name__ == "__main__":
start()def print_options(options):
for i in range(0, len(options)):
print str(i + 1) + ". " + options[i]print_options(("t1", "t2", "t3"))Context
StackExchange Code Review Q#82752, answer score: 6
Revisions (0)
No revisions yet.