首页 > 解决方案 > 我在返回循环时遇到问题,我将其用作基于文本的 RPG 的各种地图,但不知道如何解决它。(PYTHON)

问题描述

我正在使用字典 playerCharacter 来存储一个位置,然后我想使用该位置来触发特定的循环,但我不确定如何让它通过一个已经通过的循环而不是第二次输入它,这似乎很高效的。它处于非常早期的阶段,我不是一个作家,而且我对编码还很陌生。任何帮助将不胜感激!

import random

#variables
startDec = 'w'
playerInput = 'w'
RNG = 0
fist = 1
ironSword = 3
playerCharacter = {'Level': 1, 'Health': 10, 'Weapon': fist, 'Xp': 0, 'Name': '', 'Gold': 100, 'Rations': 1, 'Location': 'I',
 'Lemphyll Rep': 0}

#functions
#Rep
def repGain(totalRep,townName):
    if townName == 'lemphyll':
        playerCharacter['Lemphyll Rep']+= totalRep
        repGained = str(totalRep)
        print("You gained " + repGained + "reputaion with Lemphyll!")

def repLoss(totalRep,townName):
    if townName == 'Lemphyll':
        playerCharacter['Lemphyll Rep']-= totalRep
        repLoss = str(totalRep)
        print("You lost " + repLoss + " reputation with Lemphyll")
        
#Experience
def gainXp(totalXp):
    playerCharacter['Xp']+= totalXp
    expGained = str(totalXp)
    print("You Gained " + expGained + " experience from that encounter!")

def levelUp():
    if playerCharacter['Xp']== 10:
        playerCharacter['Level']+= 1
        playerCharacter['Health']+= 5
        print("You have reached Level " + playerCharacter['Level'] + ". Well Done!")
#Shop
def buyRations():
    if playerCharacter['Gold']>= 10:
        playerCharacter['Gold']-= 10
        playerCharacter['Rations']+= 1
        print("One Ration has been added to your inventory!")
    else:
        print("'Sorry doesn't seem like you have enough gold!'")

def buyIronSword():
    if playerCharacter['Gold']>= 50:
        playerCharacter['Gold']-= 50
        playerCharacter['Weapon']= ironSword
    else:
        print("'Sory doesn't seem like you have enough gold!'")

#Intro
def introduction():
    global startDec
    global playerCharacter
    print("Hello adventurer and welcome to the realm of Illandria.\n")
    while playerCharacter['Location']== 'I':
        playerCharacter['Name'] = str(input("Whats your name?\n"))
        startDec = str(input("If you are ready to begin this quest, " + playerCharacter['Name']+ ", then just let me know\n(Y)es or (N)o\n"))
        if startDec == 'N':
            print("What else is there left to do?")
        elif startDec == 'Y':
            print("Great! Exciting things await on your journey's\n")
            playerCharacter['Location']= 'BT'

#Central Towns
def town_1():
    global playerInput
    global playerCharacter
    print("You ride into a small town called, Lemphyll, you notice a board that reads General Store to the east,\nChurch to the West, The Pitted Fall straight along.")
    playerCharacter['Location']= 'LCR'
    if playerCharacter['Location'] == 'LCR':
        playerCharacter['Location']= input("Would you like to go to the (G)eneral Store, or maybe the (C)hurch, or the (P)itted Fall:\n")
    #General Store Story Tree
    if playerCharacter['Location'] == 'G':
        print("\nYou walk into a small but well kept General Store. After a walk through you see ordinary supplies light weaponry and food.\nThere is a chubby clean shaven man behind the counter with an eerie grin. He waves at you to come over to him.")
        playerInput = input("Would you like to go up to the (S)hop Keep, or (L)eave Abruptly\n")
        if playerInput == 'S':
            print("\n'Well that's a face I've never seen' say's the Shop Keep.\n'My name is Charlain, but most people call me Charlie. What's your name?'\n")
            playerInput = input("Do you want to tell him your (N)ame, or (A)sk about supplies?\n")
            if playerInput == 'N':
                playerInput = input("\n'My name is " + playerCharacter['Name'] + " I'm just coming through looking to see if you had any (R)umors?\nor looking to see what you're (S)elling?'\n")
                if playerInput == 'R':
                    print("\n'Well I don't know about any rumors but I have heard some people talking about a strange man who just moved in above the bar, The Pitted Fall\nHe says he was contacted by something while walking in the woods'\n")
                    playerInput = input("Would you like to (T)hank him, or (A)sk about supplies")
                    if playerInput == 'T':
                        print("'Thanks for the warm welcome Charlie' you say as you head out the door.\n")
                        repGain(1,'Lemphyll')
                        gainXp(1)
                        playerCharacter['Location']= 'LCR'
                    elif playerInput == 'A':
                        playerInput = input("\nCharlie says 'I have (R)ations for 10G and I have an (I)ron sword for 50G or buy (N)othing\n")
                        if playerInput == 'R':
                            buyRations()
                            print("'Theres your rations' says Charlie")
                            playerInput = input("Would you like to (L)eave, or buy (I)ron sword?")
                            if playerInput == 'I':
                                buyIronSword()
                                playerInput = input("Would you like to (L)eave or (A)sk about rumors")
                                if playerInput == 'L':
                                    print("'Thanks for the warm welcome Charlie' you say as you head out the door.\n")
                                    repGain(1,'Lemphyll')
                                    gainXp(3)
                                    playerCharacter['Location']= 'LCR' 
                        elif playerInput == 'I':
                            buyIronSword()
                            playerInput = input("\nWould you like to ask about the (R)ations, or (")
                        elif playerInput == 'N':
                            print("Thank the Shop Keep and leave quickly")
                            playerCharacter['Location']= 'LCR'
        elif playerInput == 'L':           
            print("'What's the rush stranger I was just trying to have a chat!\nYou leave abruptly and are back at the crossroads of the town.")
            repLoss(1,'Lemphyll')
            gainXp(1)
            playerCharacter['Location']= 'LCR'

def beginingTownRng():
    global RNG
    RNG = random.randint(1,1)
    if RNG == 1:
        town_1()

if playerCharacter['Location']== 'I':
    introduction()

if playerCharacter['Location']== 'BT':
    beginingTownRng()

标签: pythonloopsdictionarygame-development

解决方案


我想出了一种解决方法,方法是向字典中添加一个新变量,并将通用商店 if 循环嵌套在城镇 while 循环中

澄清代码

playerCharacter = {'Level': 1, 'Health': 10, 'Weapon': fist, 'Xp': 0, 'Name': '', 'Gold': 100, 'Rations': 1, 'General Location': 'I', 'Town Location': '','Lemphyll Rep': 0}
def town_1():
    global playerInput
    global playerCharacter
    print("You ride into a small town called, Lemphyll, you notice a board that reads General Store to the east,Church to the West, The Pitted Fall straight along.")
    playerCharacter['General Location']= 'L'
    while playerCharacter['General Location'] == 'L':
        playerCharacter['Town Location']= input("Would you like to go to the (G)eneral Store, or maybe the (C)hurch, or the (P)itted Fall:")
        #General Store Story Tree
        if playerCharacter['Town Location'] == 'G':
            print("You walk into a small but well kept General Store. After a walk through you see ordinary supplies light weaponry and food.There is a chubby clean shaven man behind the counter with an eerie grin. He waves at you to come over to him.")
            playerInput = input("Would you like to go up to the (S)hop Keep, or (L)eave Abruptly")
            if playerInput == 'S':
                print("'Well that's a face I've never seen' say's the Shop Keep.'My name is Charlain, but most people call me Charlie. What's your name?'")
                playerInput = input("Do you want to tell him your (N)ame, or (A)sk about supplies?\n")
                if playerInput == 'N':
                    playerInput = input("'My name is " + playerCharacter['Name'] + " I'm just coming through looking to see if you had any (R)umors?\nor looking to see what you're (S)elling?'")
                    if playerInput == 'R':
                        print("'Well I don't know about any rumors but I have heard some people talking about a strange man who just moved in above the bar, The Pitted FallHe says he was contacted by something while walking in the woods'")
                        gainXp(3)
                        repGain(1,'Lemphyll')
                        playerInput = input("Would you like to (T)hank him, or (A)sk about supplies")
                        if playerInput == 'T':
                            print("'Thanks for the warm welcome Charlie' you say as you head out the door.")
                        elif playerInput == 'A':
                            playerInput = input("Charlie says 'I have (R)ations for 10G and I have an (I)ron sword for 50G or buy (N)othing\
                            if playerInput == 'R':
                                buyRations()
                                print("'Theres your rations' says Charlie")
                                playerInput = input("Would you like to (L)eave, or buy (I)ron sword?")
                                if playerInput == 'I':
                                    buyIronSword()
                                    playerInput = input("Would you like to (L)eave or (A)sk about rumors")
                                    if playerInput == 'L':
                                        print("'Thanks for the warm welcome Charlie' you say as you head out the door.\n")
                                        repGain(1,'Lemphyll')
                                        gainXp(3) 
                                elif playerInput == 'L':
                                    print("'Thanks Charlie' you say as you leave")    
                            elif playerInput == 'I':
                                buyIronSword()
                                playerInput = input("Would you like to ask about the (R)ations, or (L)eave")
                            elif playerInput == 'N':
                                print("You thank the Shop Keep and leave quickly")
            elif playerInput == 'L':           
                print("'What's the rush stranger I was just trying to have a chat!\nYou leave abruptly and are back at the crossroads of the town.")
                repLoss(1,'Lemphyll')
                gainXp(1)

推荐阅读