I was creating a text-based adventure game for practice with Python and here is my code.
#game.py
import time
import encounter
#Hp at start of game
hp = 100
#Function to display the current hp of the current player
def currenthp(hp):
if hp < 100:
print "Your hp is now at %d" % hp
elif hp <= 0:
dead()
else:
print "You are still healthy, good job!"
print "You start your regular trail."
print "It will be just a little different this time though ;)"
#Start of game
time.sleep(3)
print "You are walking along when suddenly."
time.sleep(1)
print "..."
time.sleep(2)
#Start of first encounter
print "Wild bear appears!."
print "What do you do?"
print "Stand your ground, Run away, be agressive in an attempt to scare the bear"
#first encounter
encounter.bear(hp)
I put all of my encounters in a separate script for neatness. This is the encounter script.
import time
#Bear encounter
def bear(hp):
import game
choice = raw_input("> ")
if "stand" in choice:
print "The bear walks off, and you continue on your way"
elif "run" in choice:
print "..."
time.sleep(2)
print "The bear chases you and your face gets mauled."
print "You barely make it out alive, however you have sustained serious damage"
hp = hp-60
game.currenthp(hp)
elif "agressive" in choice:
print "..."
time.sleep(2)
print "The bear sees you as a threat and attacks you."
print "The bear nearly kills you and you are almost dead"
hp = hp-90
game.currenthp(hp)
else:
print "Well do something!"
Well this all works handy-dandy, except for one thing. When my program gets to the part where it asks for an answer for what the player would like to do in response to the bear in the encounter script, the whole game script restarts. However, this time, the program will work normally. Is there a reason for this or will I just have to deal with it?