我的代码出现问题。我的代码应该(我想要它做的)是在游戏结束后出现一个spritefont,允许你输入文本,一旦输入该文本,它就会将该分数写入文本文件。但当然不会这样做。如果有人能提供帮助,那就太棒了。
修改 这是我在游戏结束后尝试输入文字的方法(游戏是太空入侵者)也许这里也有问题。
SpriteFont Label;
string txt = ".";
Keys PrevKey;
string[] HighScores = new string[5];
int count = 0;
KeyboardState ks = Keyboard.GetState();
Keys[] k = ks.GetPressedKeys();
Keys tempKey = Keys.None;
string keyChar = "";
foreach (Keys q in k)
{
Keys currentKey = q;
if (ks.IsKeyUp(PrevKey))
{
if (!(q == Keys.None))
{
switch (q)
{
case Keys.Space: keyChar = " "; break;
case Keys.Delete: txt = ""; break;
case Keys.Back: txt = txt.Remove(txt.Length - 1); break;
case Keys.Enter: HighScores[count] = txt; count++; txt = "";
if (count == 5) count = 0; break;
case Keys.End: WriteScores(); break;
case Keys.Home: ReadScores(); break;
default: keyChar = q.ToString(); break;
}
txt += keyChar;
}
}
if (currentKey != Keys.None && ks.IsKeyDown(currentKey))
{
tempKey = currentKey;
}
}
PrevKey = tempKey;
base.Update(gameTime);
}
}
}
}
public void WriteScores()
{
StreamWriter Rite = new StreamWriter(@"C:\TheScores.txt");
for (int i = 0; i < HighScores.Length; i++)
{
Rite.WriteLine(HighScores[i]);
}
Rite.Close();
}
public void ReadScores()
{
if (File.Exists(@"C:\TheHighScores.txt"))
{
StreamReader Reed = new StreamReader(@"C:\TheScores.txt");
for (int i = 0; i < HighScores.Length; i++)
{
txt += Reed.ReadLine() + "\n";
}
Reed.Close();
}
}
答案 0 :(得分:0)
将路径更改为Documents或AppData等用户文件夹。您的代码将因为权限不足而抛出访问冲突,除非您以管理员身份运行程序,因为C:\的根目录只能在提升的上下文中使用。