XNA 问答游戏答案检查器

发布于 2024-12-17 06:53:19 字数 3021 浏览 4 评论 0原文

现在我正在创建一个对错问答游戏。 问题、问题的图像和答案存储在文本文件中。所以主程序只显示问题&从该文本文件定向的图像。当玩家输入答案时,通过单击 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我们的影子 2024-12-24 06:53:19

您的答案检查中很可能存在问题,但是当您阅读文件时,您肯定会遇到前导/尾随空格问题。您应该 Trim() 分割的部分向上。所以这可能只是您需要的答案的一半。

您的零件数组目前看起来像这样。

parts[0] = "question 1"
parts[1] = " TRUE"
parts[2] = " image1"

将您的代码交换为此会有所帮助。

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].Trim();

  string correctAnswer = parts[1].Trim()
  if (correctAnswer  == "TRUE")
  {
    datasoalnya.jawaban = true;
  }
  else if (correctAnswer  == "FALSE")
  {
    datasoalnya.jawaban = false;
  }

  datasoalnya.tempatImage = parts[2].Trim();
  soalList.Add(datasoalnya);
}

作为旁注,我建议使用 bool.TryParse () 每当您将字符串转换为布尔值时。它将使您的代码更干净并且更少出错。这就是尝试解析的样子。

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].Trim();

  bool isCorrectAnswer;
  if(bool.TryParse(parts[1], out isCorrectAnswer)
  {
    datasoalnya.jawaban = isCorrect;
  }
  else
  {
    // unable to parse, add logic to recover or error out
  }

  datasoalnya.tempatImage = parts[2].Trim();
  soalList.Add(datasoalnya);
}

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.

parts[0] = "question 1"
parts[1] = " TRUE"
parts[2] = " image1"

Swapping your code to this will help that.

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].Trim();

  string correctAnswer = parts[1].Trim()
  if (correctAnswer  == "TRUE")
  {
    datasoalnya.jawaban = true;
  }
  else if (correctAnswer  == "FALSE")
  {
    datasoalnya.jawaban = false;
  }

  datasoalnya.tempatImage = parts[2].Trim();
  soalList.Add(datasoalnya);
}

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.

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].Trim();

  bool isCorrectAnswer;
  if(bool.TryParse(parts[1], out isCorrectAnswer)
  {
    datasoalnya.jawaban = isCorrect;
  }
  else
  {
    // unable to parse, add logic to recover or error out
  }

  datasoalnya.tempatImage = parts[2].Trim();
  soalList.Add(datasoalnya);
}
長街聽風 2024-12-24 06:53:19
if (datasoalnya.jawaban = true)
...
else if (datasoalnya.jawaban = false)

您可能希望将 = 更改为 ==,编译器也建议将其作为警告,并在 IDE 中用绿色下划线表示。

if (datasoalnya.jawaban = true)
...
else if (datasoalnya.jawaban = false)

You might want to change that = to ==, which is also suggested as a warning by the compiler and underlined green in the IDE.

眼眸里的那抹悲凉 2024-12-24 06:53:19

试试这个:

bool? button = null;
if (
   (mouseState.LeftButton == ButtonState.Pressed && 
    prevMouseState.LeftButton == ButtonState.Released) &&
    (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
        button = true;
else
if (
   (mouseState.LeftButton == ButtonState.Pressed && 
    prevMouseState.LeftButton == ButtonState.Released) &&
    (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
        button = false;
if (button != null)
{
    ((Game1)Game).SoundBank.PlayCue("1_btn_click02");
    if (datasoalnya.jawaban == button.Value) score += 100;
    noSoal++;
    if (noSoal == 11)
    {
        mouseDiBenar = true;
        noSoal = 1;
        if (datasoalnya.jawaban == button.Value) score = 0;
    }
}

Try this:

bool? button = null;
if (
   (mouseState.LeftButton == ButtonState.Pressed && 
    prevMouseState.LeftButton == ButtonState.Released) &&
    (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
        button = true;
else
if (
   (mouseState.LeftButton == ButtonState.Pressed && 
    prevMouseState.LeftButton == ButtonState.Released) &&
    (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
        button = false;
if (button != null)
{
    ((Game1)Game).SoundBank.PlayCue("1_btn_click02");
    if (datasoalnya.jawaban == button.Value) score += 100;
    noSoal++;
    if (noSoal == 11)
    {
        mouseDiBenar = true;
        noSoal = 1;
        if (datasoalnya.jawaban == button.Value) score = 0;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文