Python 骆驼游戏,本土距离值问题
我正在尝试使用函数来编写Camel游戏,而不是使用那么多嵌套的if语句。不过我觉得我做错了什么,我不得不对本机距离部分做了很多修改,因为我一直在进入那些距离更远而不是更近的部分。但是现在,在尝试更改randomint值之后,我无法逃避它们。如有任何改进建议,我们将不胜感激! 这是我的密码:Python 骆驼游戏,本土距离值问题,python,python-3.x,function,Python,Python 3.x,Function,我正在尝试使用函数来编写Camel游戏,而不是使用那么多嵌套的if语句。不过我觉得我做错了什么,我不得不对本机距离部分做了很多修改,因为我一直在进入那些距离更远而不是更近的部分。但是现在,在尝试更改randomint值之后,我无法逃避它们。如有任何改进建议,我们将不胜感激! 这是我的密码: import random def quitGame(): print("I am guitting now.") return True def status(milesTraveled,
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()
随机导入
def quitGame():
打印(“我现在正在弹吉他。”)
返回真值
def状态(里程、口渴、骆驼疲劳、距离旅行、饮料):
印刷品(
"""
您已经行驶了%d英里
您的骆驼状态为%d(越低越好)
您的食堂还剩下%d杯饮料
您的口渴是%d(越低越好)
当地人在你后面%d英里
“%”(挤奶、骆驼疲劳、饮酒、口渴、距离旅行)
def rest():
打印(“骆驼快乐”)
距离n=random.randint(7,14)
返回(距离)
def fullSpeed():
距离=random.randint(10,20)
打印(“您行驶了%d英里”%distance)
camelT=random.randint(1,3)
距离n=random.randint(7,14)
返回(距离、骆驼、距离)
def mediatespeed():
距离b=random.randint(5,12)
打印(“您行驶了%d英里”%distance b)
nativesB=random.randint(7,14)
返回(距离B、本机B)
def口渴(饮品左侧):
drinksL=drinksLeft-1
返回(drinksL)
def main():
choice=“”
done=False#循环变量
#游戏变量
milesTraveled=0
口渴=0
疲劳度=0
DistanceNativeTraveled=-20
饮料=5
印刷品(
"""
欢迎来到骆驼游戏!
你偷了一头骆驼穿越莫比沙漠。
当地人想要他们的骆驼回来,他们正在追捕你。活下来吧
沙漠长途跋涉,超越当地人。
"""
)
虽然没有这样做:
findOasis=random.randint(1,20)
印刷品(
"""
以下是您的选择:
从你的餐厅喝点什么。
B-前进中速。
全速前进。
停下来休息一夜。
电子状态检查。
退出游戏
"""
)
选择=输入(“您的选择?\n”)
如果choice.upper()=“Q”:
完成=退出游戏()
elif findOasis是1:
打印(“哇!你找到了一片绿洲。你的口渴止住了,水壶盖好了\
你的骆驼现在休息得很好,很快乐。”)
饮料=5
口渴=0
疲劳度=0
elif choice.upper()=“A”:
如果饮料>0:
饮料=口渴(饮料)
口渴=0
其他:
打印(“错误:哦!没有水了。”)
elif choice.upper()=“B”:
milesB,nativesB=mediatespeed()
milesTraveled+=milesB
疲劳程度+=1
口渴+=1
距离NativeTraveled+=本机B
elif choice.upper()=“C”:
milesT、Camelwear、nativesT=全速()
milesTraveled+=milesT
骆驼疲劳
距离nativesT行程+=nativesT
口渴+=1
elif choice.upper()=“D”:
distanceT=rest()
疲劳度=0
DistanceNativeTraveled+=距离
elif choice.upper()=“E”:
状态检查=状态(里程、口渴、骆驼疲劳、距离旅行、饮料)
其他:
打印(“这不是一个正确的选择-输入(a到E或Q)”)
如果口渴>4和口渴6:
打印(“游戏结束\n您渴死了!”)
完成=正确
elif Cameltired>5和Cameltired 8:
打印(“游戏结束\n您的骆驼死了。”)
完成=正确
elif DistanceNativeTraveled>=0:
打印(“游戏结束\n本地人抓到你了!”)
完成=正确
elif DistanceNativeTraveled>-15:
打印(“本地人越来越近了!”)
elif milesTraveled>=200:
打印(“你赢了\n格拉茨,你穿越了沙漠!”)
完成=正确
#呼叫总管
main()
当distanceNativeTraveled>=0
时,游戏结束,但根本没有代码来减少distanceNativeTraveled
。因此,随着每次回合distanceNativeTraveled
的不断增加,游戏肯定会很快结束
您真正想在这里检查的是distanceNativeTraveled
是否超过milesTraveled
,因此请更改:
elif distanceNativesTraveled >= 0:
"""%(milesTraveled,camelTiredness,drinks,thirst,distanceNativesTraveled))
致:
为了检查当地人是否接近,请更改:
elif distanceNativesTraveled > -15:
致:
为了正确显示当地人在你身后的里程数,你应该显示你和当地人行驶的里程数之间的差异,因此改变:
elif distanceNativesTraveled >= 0:
"""%(milesTraveled,camelTiredness,drinks,thirst,distanceNativesTraveled))
致:
"""%(milesTraveled,camelTiredness,drinks,thirst,milesTraveled - distanceNativesTraveled))