我使用Python創建了一個基於文本的冒險遊戲,這裏是我的代碼。爲什麼我的模塊在Python中運行兩次
#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)
我把我所有的遭遇都放在一個單獨的腳本中,以保持整潔。這是遭遇劇本。
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!"
好吧,這一切工作方便,丹迪,除了一件事。 當我的程序到達要求回答玩家想要做什麼以迴應遇到腳本中的熊的部分時,整個遊戲腳本重新啓動。但是,這一次,該程序將正常工作。有沒有這個原因,還是我只需要處理它呢?
我實際上是在函數中導入了遊戲,因爲它阻止了頂級的相互導入,因爲這會給我一個屬性錯誤,並在另一個帖子中看到了這一點,頂級投票答案表示這將是解決問題的方法。 –