新的 C#er 在这里。我正在开发控制台 RPG。我创建了一个保存和加载方法,它将变量写入 txt 文件并在您在某个点键入“保存游戏”或“加载游戏”时加载它们。我试图让 LoadGame() 方法跳转到我的 Main() 中的某个点。我意识到我可以重写我的一些代码并使用一些 if 语句,或者转到并删除我的 LoadGame(),但我更愿意将我的 LoadGame() 保留在我的 Main() 之外。
这基本上是我的代码的样子:
public static void LoadGame()
{
TextReader tr = new StreamReader("SavedGame.txt");
string charnameload = tr.ReadLine();
string hpload = tr.ReadLine();
string hpmaxload = tr.ReadLine();
string charattackload = tr.ReadLine();
string charmanaload = tr.ReadLine();
string charmanamaxload = tr.ReadLine();
string charmanapowerload = tr.ReadLine();
string spellsknownload = tr.ReadLine();
string holyblastknownload = tr.ReadLine();
string greaterhealknownload = tr.ReadLine();
string goldload = tr.ReadLine();
string expload = tr.ReadLine();
string levelload = tr.ReadLine();
string inventoryWeaponload = tr.ReadLine();
string inventoryArmorload = tr.ReadLine();
string inventoryshieldload = tr.ReadLine();
string inventoryPotion1load = tr.ReadLine();
string inventoryPotion2load = tr.ReadLine();
string inventoryPotion3load = tr.ReadLine();
string inventoryKey1load = tr.ReadLine();
string inventoryKey2load = tr.ReadLine();
string inventoryKey3load = tr.ReadLine();
string inventoryKey4load = tr.ReadLine();
string inventoryKey5load = tr.ReadLine();
characterName = Convert.ToString(charnameload);
hp = Convert.ToInt32(hpload);
hpmax = Convert.ToInt32(hpmaxload);
charattack = Convert.ToInt32(charattackload);
charmana = Convert.ToInt32(charmanaload);
charmanamax = Convert.ToInt32(charmanamaxload);
charmanapower = Convert.ToInt32(charmanapowerload);
spellsknown = Convert.ToInt32(spellsknownload);
holyblastknown = Convert.ToInt32(holyblastknownload);
greaterhealknown = Convert.ToInt32(greaterhealknownload);
gold = Convert.ToInt32(goldload);
exp = Convert.ToInt32(expload);
level = Convert.ToInt32(levelload);
inventoryWeapon = Convert.ToString(inventoryWeaponload);
inventoryArmor = Convert.ToString(inventoryArmorload);
inventoryshield = Convert.ToString(inventoryshieldload);
inventoryPotion1 = Convert.ToString(inventoryPotion1load);
inventoryPotion2 = Convert.ToString(inventoryPotion2load);
inventoryPotion3 = Convert.ToString(inventoryPotion3load);
inventoryKey1 = Convert.ToString(inventoryKey1load);
inventoryKey2 = Convert.ToString(inventoryKey2load);
inventoryKey3 = Convert.ToString(inventoryKey3load);
inventoryKey4 = Convert.ToString(inventoryKey4load);
inventoryKey5 = Convert.ToString(inventoryKey5load);
tr.Close();
CheckInventory();
CheckSpell();
//need statement here to skip to a point in Main()
}
static void Main()
{
//skip to somewhere in here
}