#python #loops #dictionary #game-development
#python #циклы #словарь #разработка игр
Вопрос:
Я использую dictionary 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 known(Y)es or (N)on"))
if startDec == 'N':
print("What else is there left to do?")
elif startDec == 'Y':
print("Great! Exciting things await on your journey'sn")
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 Abruptlyn")
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 FallnHe 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)othingn")
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()
Ответ №1:
Я нашел один выход, добавив новую переменную в словарь и вложив цикл общего хранилища if в цикл town 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)