如何解决我遇到了问题,回到我的循环中,我正在将其用作基于文本的RPG的排序图,并且不确定如何解决它 蟒蛇
我正在使用字典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()
解决方法
我想出了一条出路,方法是在字典中添加一个新变量,然后将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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。