C#在Main中使用其他类方法变量



我尝试制作一个简单的基于文本的游戏,刚刚完成了在Main中编写角色创建菜单的代码。现在我决定用单独的类Player制作库,并用这段代码制作CharacterCreation方法。问题是,我不知道如何制作Player。CharCreation方法变量(如玩家名称和属性(现在可以在主程序中使用。

public class Player
{
public static void CharCreation()
{
Console.WriteLine("t=== Character creation menu ===n");
Console.Write("Write the name and press Enter: ");
string name = Console.ReadLine();
string Again;
string RaceChoise;
Console.Write("nChoose your race(print number and press Enter):n 1.Humann 2.Dwarfn 3.Elfn");
// Race choise
do
{
Again = "No";
RaceChoise = Console.ReadLine();
switch (RaceChoise)
{
case "1":
Console.WriteLine("nYou was born as human (Charisma +1)n");
RaceChoise = "Human";
break;
case "2":
Console.WriteLine("nYou was born as dwarf(Constitution +1)n");
RaceChoise = "Dwarf";
break;
case "3":
Console.WriteLine("nYou was born as elf (Agility +1)n");
RaceChoise = "Elf";
break;
default:
Console.WriteLine("Wrong number");
Again = RaceChoise;
break;
}
} while (Again != "No");
// Base attributes according to the race choise
Console.WriteLine("Character attributesn");
int Str = 1;
int Agi = 1;
int Int = 1;
int Con = 1;
int Cha = 1;
Agi = RaceChoise == "Elf" ? (Agi + 1) : (Agi);
Con = RaceChoise == "Dwarf" ? (Con + 1) : (Con);
Cha = RaceChoise == "Human" ? (Cha + 1) : (Cha);
Console.Write($"Strengh: {Str}nAgility: {Agi}nIntelligence: {Int}nConstitution: {Con}nCharisma: {Cha}nn");
Console.WriteLine("Above is your character base attributes. Now distribute 5 points more");
// Attributes points destribution
int Counter = 5;
for (int i = 0; i < 1; i++)
{
Console.Write("To strengh: ");
int StrChange = Convert.ToInt32(Console.ReadLine());
if (StrChange > Counter) { StrChange = Counter; }
Counter = Counter - StrChange;
Str = Str + StrChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To agility: ");
int AgiChange = Convert.ToInt32(Console.ReadLine());
if (AgiChange > Counter) { AgiChange = Counter; }
Counter = Counter - AgiChange;
Agi = Agi + AgiChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To intelligence: ");
int IntChange = Convert.ToInt32(Console.ReadLine());
if (IntChange > Counter) { IntChange = Counter; }
Counter = Counter - IntChange;
Int = Int + IntChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To constitution: ");
int ConChange = Convert.ToInt32(Console.ReadLine());
if (ConChange > Counter) { ConChange = Counter; }
Counter = Counter - ConChange;
Con = Con + ConChange;
if (Counter == 0)
{
break;
}
else
{
Console.WriteLine($"Points remains: {Counter}");
Console.Write("To charisma: ");
int ChaChange = Convert.ToInt32(Console.ReadLine());
if (ChaChange > Counter) { ChaChange = Counter; }
Cha = Cha + ChaChange;

好吧,我想它可能是某种构造函数,但尝试了几个选项,但它不起作用。我的代码看起来很笨拙,但我只学习了一个月的C#,所以不要严格判断:(

public class Player
{
public string Name { get; set; }
public string Race { get; set; }
public static Player CharCreation()
{
// do your stuff...
string name = "Player";
string race = "Human";
return new Player()
{
Name = name,
Race = race,
};
}
}
class Program
{
static void Main()
{
Player player = Player.CharCreation();
// do something with player.Name or player.Race
}
}

最新更新