How to make matching answer appear with questionstring?

94 views Asked by At

I am trying to create a simple game where players have to answer questions. So far I can only display the question, but I am trying to display the 3 possible answers that are matched to the question.

The problem is that although I can display the questions, all questions will quickly be shown. What I want is when I start the game, it should start with the first question, then wait 1 minute, then display the second question, and so on. The questions have to be given at random. I also want the 3 answers to be displayed.

I have a questions list where I store all my questions and answers. The int is the correctanswer.

This is my code:

private SpriteFont _verdana;
private List<Question> questions = new List<Question>();

public class Question
{
    public string questionString;
    public List<string> answers = new List<string>();
    public int correctAnswer = 0;
    public bool answered = false;

    public Question(string question, string answer1, string answer2, string answer3, int correctAnswer)
    {
        questionString = question;
        answers.Add(answer1);
        answers.Add(answer2);
        answers.Add(answer3);
        this.correctAnswer = correctAnswer;
    }
}

public Level(Game1 game)
{
    _verdana = game.Content.Load<SpriteFont>("Verdana");

        Question q = new Question("Question1", 
            "Answer1", 
            "Answer2", 
            "Answer3", 
            0);
        questions.Add(q);
        q = new Question("Question2?", 
            "Answer1.", 
            "Answer2.", 
            "Answer3.", 
            0);
}

Random r = new Random();

public void Draw(SpriteBatch spriteBatch)
{
            spriteBatch.DrawString(_verdana, questions[r.Next(0, questions.Count - 1)].questionString, new Vector2(80, 325), Color.White);

}
1

There are 1 answers

0
Arnaud Develay On

Monogame framework provides the game loop logic for you. This means that 2 methods of your game class will be called repeatedly for each frame. The Update method which is used to update variables needed for your game such as user inputs on keyboard or mouse. The Draw method is used for displaying your game entities.

Each time the Draw method is called, you generate a new random number that can change the question. This explains why your questions change so quickly. To fix this, you can create a field in your class to track the currently displayed question and use this field in the Draw method instead of the random number. Then use the Update method to update the currently displayed question. But, again if you don't want to see your question change at each frames, you must check the elapsed time before updating your field. You can do this with the GameTime parameter of the Update method or with a timer.

For displaying the answers under your question, you must call the DrawString method for each of your answers. So you will have a Draw method with 4 DrawString calls: 1 for the question and the 3 others for the answers. Don't forget to update the position (the Vector2 parameter) for each call to DrawString