骆驼游戏,本地距离值的问题



我正在尝试使用函数而不是许多嵌套的if语句来编写Camel游戏。不过,我觉得我做错了什么,我不得不对原生距离部分做了很多修改,因为我一直在进入它们只离得更远而不是更近的部分。但现在,在尝试改变随机值之后,我再也无法逃脱它们。如有任何改进建议,我们将不胜感激!这是我的代码:

import random
def quitGame():
print("I am guitting now.")
return True
def status(milesTraveled, thirst, camelTiredness, distanceNativesTraveled, drinks):
print(
"""
You have traveled %d miles
Your Camel Status is %d (lower is better)
You have %d drinks left in your canteen
Your thirst is %d (lower is better)
The Natives are %d miles behind you
"""%(milesTraveled,camelTiredness,drinks,thirst,distanceNativesTraveled))
def rest():
print("The camel is happy")
distanceN = random.randint(7,14)
return(distanceN)
def fullSpeed():
distanceT = random.randint(10,20)
print("You travel %d miles"%distanceT)
camelT = random.randint(1,3)
distanceN = random.randint(7,14)
return(distanceT,camelT,distanceN)
def moderateSpeed():
distanceB = random.randint(5,12)
print("You travel %d miles"%distanceB)
nativesB = random.randint(7,14)
return(distanceB,nativesB)
def thirsty(drinksLeft):
drinksL = drinksLeft - 1
return(drinksL)
def main():
choice = ""
done = False  # loop variable
#variables for game
milesTraveled = 0
thirst = 0
camelTiredness = 0
distanceNativesTraveled = -20
drinks = 5
print(
"""
Welcome to the Camel Game!
You have stolen a camel to make your way across the great Mobi desert.
The natives want their camel back and are chasing you down. Survive your
desert trek and out run the native.
"""
)

while not done:
findOasis = random.randint(1,20)
print(
"""
Here are your choices:
A - Drink from you canteen.
B - Ahead moderate speed. 
C - Ahead full speed. 
D - Stop and rest for night.
E - Status check.
Q - Quit the Game
"""
)
choice = input(" Your  choice?n")
if choice.upper() == "Q":
done = quitGame()
elif findOasis is 1 :
print("Wow! You've found an Oasis. Your thirst is quenched, canteen topped off, 
and your camel is now well rested and happy.")
drinks = 5
thirst = 0
camelTiredness = 0

elif choice.upper() == "A":
if drinks > 0:
drinks = thirsty(drinks)
thirst = 0
else:
print("Error: Uh oh! No water left.")

elif choice.upper() == "B":
milesB,nativesB = moderateSpeed()
milesTraveled += milesB
camelTiredness += 1
thirst += 1
distanceNativesTraveled += nativesB
elif choice.upper() == "C":
milesT,camelTired,nativesT= fullSpeed()
milesTraveled += milesT
camelTiredness += camelTired
distanceNativesTraveled += nativesT
thirst += 1
elif choice.upper() == "D":
distanceT = rest()
camelTiredness = 0
distanceNativesTraveled += distanceT
elif choice.upper() == "E":
statusCheck = status(milesTraveled, thirst, camelTiredness, distanceNativesTraveled, drinks)
else:
print("That was not a correct choice - Enter (A through E or Q)")
if thirst > 4 and thirst <= 6:
print("You are thirsty")

elif thirst > 6:
print("GAME OVER nYou died of thirst!")
done = True
elif camelTiredness > 5 and camelTiredness <= 8:
print("Your camel is getting tired")

elif camelTiredness > 8:
print("GAME OVER nYour camel is dead.")
done = True
elif distanceNativesTraveled >= 0:
print("GAME OVER nThe natives have captured you!")
done = True
elif distanceNativesTraveled > -15:
print("The natives are getting close!")

elif milesTraveled >= 200:
print("YOU WIN nCongrats, you made it across the desert!")
done = True
# call main
main()

游戏在distanceNativesTraveled >= 0时结束,但根本没有减少distanceNativesTraveled的代码。因此,随着每回合distanceNativesTraveled的不断增加,游戏必然会很快结束。

你真正想要的是检查distanceNativesTraveled是否超过了milesTraveled,所以改变:

elif distanceNativesTraveled >= 0:

至:

elif distanceNativesTraveled >= milesTraveled:

为了检查当地人是否越来越近,请更改:

elif distanceNativesTraveled > -15:

至:

elif distanceNativesTraveled - milesTraveled > -15:

为了正确显示当地人在你身后的英里数,你应该显示你和当地人行驶的英里数之间的差异,所以改变:

"""%(milesTraveled,camelTiredness,drinks,thirst,distanceNativesTraveled))

至:

"""%(milesTraveled,camelTiredness,drinks,thirst,milesTraveled - distanceNativesTraveled))

最新更新