【发布时间】:2019-02-21 22:41:31
【问题描述】:
我正在编写一个游戏,我正在努力删除我的火柴人,然后再画另一个。
我可以只使用Console.Clear(),但它会清除整个控制台,我只需要删除以前的火柴人。
我正在尝试使用:
private static string path = @"c:..\..\characters\";
private string file;
private int xpast = 0;
private int ypast = 0;
private int LinhasSeparacao;
public Graphics()
{
}
public Graphics(string file)
{
this.file = file;
StreamReader sr = new StreamReader(path + file);
LinhasSeparacao = 0;
do
{
LinhasSeparacao++;
}
while (sr.ReadLine() != "separar");
sr.Close();
}
public void Draw(int x, int y,bool forma = true)
{
string[] persona = File.ReadAllLines(path + file);
LimparAnterior(forma,persona,x,y);
//Console.Clear();
if (forma)
{
for (int i = 0; i < LinhasSeparacao - 1; i++)
{
Console.SetCursorPosition(x, y + i);
Console.Write(persona[i]);
}
}
else
{
int j= 0;
for (int i = LinhasSeparacao; i <persona.Length-1; i++)
{
Console.SetCursorPosition(x, y + j);
Console.Write(persona[i]);
j++;
}
}
xpast = x;
ypast = y;
}
private void LimparAnterior(bool forma,string[] persona, int xlive, int ylive)
{
int i = 0;
for (i = 0 ; i < LinhasSeparacao; i++)
{
Console.SetCursorPosition(xpast > 0? xpast -1:xpast,ypast + i);
Console.Write(" ",persona[i].Length);
}
}
这是我绘制新角色的类,它需要 x 和 y 坐标。我在绘图中使用了一个文件,并将所有线条放入一个名为 persona 的数组中。有人按下箭头让小家伙动起来后,我正在画画。
如果您需要更多信息,请说出来。这是github的链接: https://github.com/digaso/Wizardoft
【问题讨论】:
-
移到你要清空的位置
Console.Write(" ");(即写一个空格),或者,如果你想清空整行,移到你要清空的位置Console.WriteLine();是请注意,后一个调用会将您的光标定位在下一行的开头。 -
感谢您的回答,但我有多行要删除,但不是整行,只是确切的字符数。请查看 git hub 的链接并查看文件“hero.txt”,这就是我要删除的内容。随意编辑
-
这些确实是您的选择(据我所知)。如果您在位置 (x, y) 处写一个“0”和一个“|”在位置 (x, z),记住你写它们的位置。然后,当您要删除它们时,请返回您编写它们的位置并用空格覆盖它们。如果要连续删除4个字符(横向),可以写4个空格。其他人可能知道更好的方法。如果是这样,我有兴趣看到这个建议。
-
但问题是,我就是这样用的。我有变量来保存 x 和 y 位置。之后设置它们,我写“”来清除,但不会清除所有字符。如果您需要帮助,请下载项目编译后自己查看。
-
如果你写了很多主机游戏,你应该看看这个:Advanced console I/O