我正在编写一个基于文本的冒险游戏,其中我想从一个用于另一个方法的类中访问信息。我打算做的是在一个线程中包含字符信息,然后在游戏过程中的任何时候以与暂停菜单类似的方式将其打印到文本文档。
我将尝试编写一个我想在下面做的例子。
class Player
{
public int health;
}
public class Program
{
public static void Main(string[] args)
{
Player player = new Player();
player.health = 20;
}
public static void method2()
{
//Here I want to access the "player" in the Main method.
//Then I want to print this and other stats to a text document:
string[] lines = { "Stat 1", "Stat 2", "Stat 3" };
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
Process.Start(@"C:\Users\Public\TestFolder\WriteLines.txt");
}
}
答案 0 :(得分:2)
我建议将所有内容从主要内容中删除,然后制作包含播放器的游戏类。这样您就可以在需要时访问播放器,而不必来回传递播放器。您还可以在实例化时初始化播放器运行状况。
public class Program
{
public static void Main()
{
Game myGame = new Game();
myGame.PlayGame();
}
}
public class Game
{
public Player myPlayer = new Player();
public void PlayGame()
{
// place your loop / logic here to request input from the user and update your game state
}
public void WritePLayerStats()
{
string[] lines = { myPlayer.stat1, myPlayer.stat2 };
File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
Process.Start(@"C:\Users\Public\TestFolder\WriteLines.txt");
}
}
public class Player
{
public int health = 20;
public string stat1 = "";
public string stat2 = "";
}
答案 1 :(得分:1)
只需将Player
传递给您的method2()
。
public static void Main(string[] args)
{
Player player = new Player();
player.health = 20;
method2(player);
}
public static void method2(Player player)
{
//Here I want to access the "player" in the Main method.
//Then I want to print this and other stats to a text document:
string[] lines = { player.health, "Stat 1", "Stat 2", "Stat 3" };
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
Process.Start(@"C:\Users\Public\TestFolder\WriteLines.txt");
}
您还可以将方法移动到Player
对象中,例如:
public class Program
{
public static void Main(string[] args)
{
Player player = new Player();
player.health = 20;
player.PrintStatistics();
}
}
public class Player
{
public int health;
public void PrintStatistics()
{
//Here I want to access the "player" in the Main method.
//Then I want to print this and other stats to a text document:
string[] lines = { this.health, "Stat 1", "Stat 2", "Stat 3" };
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
Process.Start(@"C:\Users\Public\TestFolder\WriteLines.txt");
}
}
答案 2 :(得分:0)
您可以通过以下两种方式之一执行此操作:您可以通过将播放器实例作为参数调用它来将值传递给方法。
public static void method2(Player player)
{
string[] lines = { "Stat 1", "Stat 2", "Stat 3" };
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
Process.Start(@"C:\Users\Public\TestFolder\WriteLines.txt");
}
public static void Main(string[] args)
{
Player player = new Player();
player.health = 20;
method2(player);
}
或者你可以让它成为班级的静态成员,并在开始你的游戏逻辑之前使用Main设置课程
public class Program
{
private static Player player;
public static void method2()
{
string[] lines = { "Stat 1", "Stat 2", "Stat 3" };
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
Process.Start(@"C:\Users\Public\TestFolder\WriteLines.txt");
}
public static void Main(string[] args)
{
player = new Player();
player.health = 20;
method2(player);
}
}