【问题标题】:Camel Game, trouble with native distance values骆驼游戏,原生距离值的问题
【发布时间】:2023-03-11 06:55:02
【问题描述】:

我正在尝试使用函数而不是这么多嵌套的 if 语句来编写 Camel 游戏。我认为我做错了什么,我不得不对原生距离部分进行很多修改,因为我不断地进入它们只会变得更远而不是更近的部分。但是现在在尝试更改 randomint 值之后,我永远无法逃脱它们。非常感谢任何改进建议! 这是我的代码:

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()

【问题讨论】:

    标签: python python-3.x function


    【解决方案1】:

    游戏在distanceNativesTraveled &gt;= 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多