XNA 问答游戏答案检查器
现在我正在创建一个对错问答游戏。 问题、问题的图像和答案存储在文本文件中。所以主程序只显示问题&从该文本文件定向的图像。当玩家输入答案时,通过单击 True 或 False 按钮,程序会将其与文本文件中该问题的答案进行检查。
文本文件是这样的:
question 1, TRUE, image1
question 2, FALSE, image2
question 3, FALSE, image3
question 4, TRUE, image4
question 5, TRUE, image5
question 6, TRUE, image6
question 7, FALSE, image7
question 8, TRUE, image8
question 9, FALSE, image9
question 10, FALSE, image10
这就是我从文本文件读取数据的方式:
StreamReader sr = new StreamReader("data.txt");
string line;
while ((line = sr.ReadLine()) != null)
{
string[] parts = line.Split(',');
Soal datasoalnya = new Soal();
datasoalnya.soal = parts[0];
if (parts[1] == "TRUE")
{
datasoalnya.jawaban = true;
}
else if (parts[1] == "FALSE")
{
datasoalnya.jawaban = false;
}
datasoalnya.tempatImage = parts[2];
soalList.Add(datasoalnya);
}
这就是我检查答案的方式:
if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
(new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
{
((Game1)Game).SoundBank.PlayCue("1_btn_click02");
if (datasoalnya.jawaban = true)
{
score += 100;
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
score = 0;
}
}
else if (datasoalnya.jawaban = false)
{
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
}
}
}
else if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
(new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
{
((Game1)Game).SoundBank.PlayCue("1_btn_click02");
if (datasoalnya.jawaban = true)
{
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
score = 0;
}
}
else if (datasoalnya.jawaban = false)
{
score += 100;
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
}
}
}
问题是每当玩家输入答案时,程序都不会检查该输入的条件,无论是是真是假。就像每当玩家点击正确按钮时,无论文本文件中的真假,分数总是乘以 100。
我怎样才能让程序可以检查答案......?
right now I'm creating a true-false quiz game.
the question, image for the question, and answer are stored in text file. so the main program only display the question & images directed from that text file. and when player input the answer, by clicking True or False button, the program will check it with the answer for that question in the text file.
the text file is something like this :
question 1, TRUE, image1
question 2, FALSE, image2
question 3, FALSE, image3
question 4, TRUE, image4
question 5, TRUE, image5
question 6, TRUE, image6
question 7, FALSE, image7
question 8, TRUE, image8
question 9, FALSE, image9
question 10, FALSE, image10
and this is how I read the datas from text file :
StreamReader sr = new StreamReader("data.txt");
string line;
while ((line = sr.ReadLine()) != null)
{
string[] parts = line.Split(',');
Soal datasoalnya = new Soal();
datasoalnya.soal = parts[0];
if (parts[1] == "TRUE")
{
datasoalnya.jawaban = true;
}
else if (parts[1] == "FALSE")
{
datasoalnya.jawaban = false;
}
datasoalnya.tempatImage = parts[2];
soalList.Add(datasoalnya);
}
and this is how I check the answer:
if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
(new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
{
((Game1)Game).SoundBank.PlayCue("1_btn_click02");
if (datasoalnya.jawaban = true)
{
score += 100;
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
score = 0;
}
}
else if (datasoalnya.jawaban = false)
{
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
}
}
}
else if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
(new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
{
((Game1)Game).SoundBank.PlayCue("1_btn_click02");
if (datasoalnya.jawaban = true)
{
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
score = 0;
}
}
else if (datasoalnya.jawaban = false)
{
score += 100;
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
}
}
}
the problem is whenever player input the answer, the program not checked the condition of that input, wheter it true or false. like whenever player click correct button, the score always multiplied by 100 wheter it true or false in the text file.
how I can make it so the program can check the answer...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的答案检查中很可能存在问题,但是当您阅读文件时,您肯定会遇到前导/尾随空格问题。您应该
Trim()
分割的部分向上。所以这可能只是您需要的答案的一半。您的零件数组目前看起来像这样。
将您的代码交换为此会有所帮助。
作为旁注,我建议使用
bool.TryParse ()
每当您将字符串转换为布尔值时。它将使您的代码更干净并且更少出错。这就是尝试解析的样子。There's an issue in your answer checking too probably, but you are definately having leading/trailing spacing issues when you are reading in the file. You should
Trim()
your split parts up. So this is probably only half of the answer that you need.You're parts array currently looks like this.
Swapping your code to this will help that.
As a side note, I would recommend using
bool.TryParse()
whenever you're converting a string to a bool. It will make your code cleaner and less error approne. This is what it would look like with a try parse.您可能希望将
=
更改为==
,编译器也建议将其作为警告,并在 IDE 中用绿色下划线表示。You might want to change that
=
to==
, which is also suggested as a warning by the compiler and underlined green in the IDE.试试这个:
Try this: