首页 > 解决方案 > 为什么我的基于文本的冒险游戏“循环”通过 if 语句而不是做出非此即彼的决定?

问题描述

编辑:看起来我的主要问题是,无论出于何种原因,在我的选择中使用布尔运算符“或”会默认用户同时选择两种选择,或者是肯定的选择而不是否定的选择。如果有人能解释原因,我将不胜感激。

我是 Python 3 的新手,这可能很明显,所以希望这是一个简单的修复。我真的很想完成这个游戏,因为它是我发现的唯一一种“有趣”的学习方式,可以让我不断地专注(和沮丧)。我最终还想添加更复杂的功能,例如用户清单。我一直在关注一些基于功能的 youtube 指南,并且在我的游戏工作中基本上有一些路径(北>>西>>战斗),而有些则没有(北>>东>>否)。在后一种情况下,我的程序每次玩家做出第三次选择时都会选择“Y”,即使玩家写的是“N”。该问题在这段代码中多次出现。在这里,我将向您展示(如果您发现我的代码有其他根本性错误,请告诉我):

#Author: Joseph C 2021
# Cyoa (Kyoa) is a text-based fantasy game set in the realm of Kyoa. The Traveller awakens empty-handed and
# without any inkling of memory about who s/he is or what this strange medieval motor-punk world has to offer...
import random
import time

def playagain():
    answer = input("Do you want to play again? Y or N")
    if answer == "Y":
        print("OK.")
        chapter1()
    elif answer == "N":
        print("Goodbye.")

def Intro():
    print("Hey there...")
    time.sleep(.5)
    print("Hello?!")
    time.sleep(1.5)
    print("WAKE UP!")
    time.sleep(1)
    print("You awaken under a bodhi tree, to the annoying chirp of your Lilliputian fairy ally, Pyxys. She soon figures")
    print("that you've lost your memory due to the encounter up north, and respawned in the middle of a hilly field.")
    print("As a blank slate and denizen of the uncharted realm of Kyoa, you and Pyxys decide to explore once more.")

def chapter1():
    choice = input("Do you choose to go north, south, east, or west? \n")
    if choice == "north":
        print("You move forward in this direction. You witness a murder of winged orca whales in the blue, star"
              "covered yet daylit sky.")
        chapter1north()
    if choice == "east":
        chapter1east()
    if choice == "south":
        print("You walk a lot, pass by a lake with a distant background mountain range, and finally")
        print("come across an abandoned town. You loot a house and find a sack of gold! But suddenly,")
        print("a cohort of Vector Vipers and Glue Goblins gang up on you! You have died.")
        playagain()
    if choice == "west":
        print("You walk a lonely, boring path that twists and wanes but ultimately points west.")
        print("Before you know it, you stand before a tall evergreen forest, densely packed with")
        print("rocks and trees.")
        chapter2SF()

def chapter1north():
    time.sleep(.5)
    print("You continue walking north...")
    time.sleep(1)
    print("You come across a dense carniferous forest on the horizon to the east, and more spacious hillsides to"
          "the west.")
    choice = input("Will you go west toward the hilly plains, or east toward the dense woods? \n")
    if choice == "west":
        print("As you tread upon the hills towards the northwest, you encounter a spinfoam slime!")
        decision = input("Fight or flee?")
        if decision == "fight":
            print("You stomp on the paltry slime multiple times until it dissociates, bubbling into gas!"
                  "Congratulations! You gained 2 XP!")
            chapter2()
        if decision == "flee":
            print("You evade the paltry slime, yet you hear something from a distance...")
            time.sleep(1)
            print("The spinfoam slime sends a torrent of electricity towards you, burning you to a crisp!"
                  "You have died.")
            playagain()


    if choice == "east":
            print("You approach the thick forest, which you find is gated by barbed wire.")
            time.sleep(1) #at this point the code starts to bug. Maybe I need to make this in a new function
            InsectForestEntrance()

def InsectForestEntrance():
    decision2 = input("You decide to walk amid the perimeter of the barbed forest, heading in the"
                      "east and southeast directions...You manage to find an entrance."
                      " Will you enter? (Y or N)\n")
    if decision2 == "yes" or "Y":
        print("You enter the Insect Forest...")
        time.sleep(1)
        chapter1insectforest()
    elif decision2 == "no" or "N":
        print("You decide to travel south, as the forest expands throughout the entirety of the north.")
        chapter1east()
def chapter1insectforest():
    print("Immediately, the forest forks into two paths: one going northbound, the other southeastern.")
    decision2a = input("Which one will you take? north or southeast?")
    if decision2a == "north":
        pass
    if decision2a == "southeast":
        print("You go")
def chapter2():
    pass
def chapter3():
    pass
def chapter4():
    pass

Intro()
chapter1()

标签: pythontext-basedadventure

解决方案


推荐阅读