3
\$\begingroup\$

I have two lists which work with an XML file which holds questions and answers. At the moment the project displays all the questions at the same time so they "flicker" on the screen and iterate between each item in the list.

Whats the easiest way (coding) to pause the random generated list on any particular questions?

Question class:

class question
{
    public string questionString;
    public string apple;
    public string pear;
    public string orange;

    //the ? indicate the int can be nullable so it can accept the string item
    int? correctAnswer;

    public question(string newquestionString)
    {
        parseQuestion(newquestionString);
    }

    public void parseQuestion(string newquestionString)
    {
        List<string> questionComponents = newquestionString.Split('|').ToList<string>();

        questionString = questionComponents[0];
        apple = questionComponents[1];
        pear = questionComponents[2];
        orange = questionComponents[3];

        correctAnswer = Int32.Parse(questionComponents[4]);

    }

In Game1:

    Random q = new Random();
    int i = q.Next(questions.Count);



    spriteBatch.Begin();
    spriteBatch.DrawString(myfont, questions[i], new Vector2(100 + 100 *i, 100), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].questionString, new Vector2(100 + 100, 100), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].apple, new Vector2(100 + 100, 200), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].orange, new Vector2(100 + 100, 300), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].pear, new Vector2(100 + 100, 400), Color.Black);
    spriteBatch.End();
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Does the code labeled "Game1" occur in the Draw(GameTime gameTime) method? If so, the problem looks as though you draw a different question each frame not that you need to draw slower. If you draw the same thing every frame, there should be no flicker; I would adjust the code like this:

// private class level variable
int currentQuestion = 0;


// in the Update(GameTime gameTime) method
if(userChoseAnswer) // check input and see if you should advance to the next question
{
    Random q = new Random();
    // might want to add code here to avoid showing 
    // questions already shown? unless that wont help your game
    currentQuestion = q.Next(questions.Count);  
}


// in the Draw(GameTime gameTime) method
spriteBatch.Begin();
spriteBatch.DrawString(myfont, questions[currentQuestion], new Vector2(100 + 100 *i, 100), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].questionString, new Vector2(100 + 100, 100), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].apple, new Vector2(100 + 100, 200), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].orange, new Vector2(100 + 100, 300), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].pear, new Vector2(100 + 100, 400), Color.Black);
spriteBatch.End();
\$\endgroup\$
6
  • \$\begingroup\$ +1 This. A question and answer game shouldn't worry about timing unless they're timed questions, but the primary sentinel value should be checking whether a user has answered in the first place. \$\endgroup\$
    – Ray Dey
    Commented Apr 21, 2011 at 21:42
  • \$\begingroup\$ I think your answer is better than mine :). Small addendum A lot of newbies have problems with detecting a keypress (because its hard to hold a button for less than 15ms. So here is the code that to detect a key press: KeyBoardState oldState; if(Keyboard.GetState.IsKeyDown(Keys.A) && !oldState.IsKeyDown(Keys.A)){ userChoseAnswer = true; } oldState = Keyboard.GetState(); \$\endgroup\$
    – Roy T.
    Commented Apr 22, 2011 at 7:51
  • \$\begingroup\$ @Roy a good point on keypress. \$\endgroup\$
    – Nate
    Commented Apr 22, 2011 at 17:35
  • \$\begingroup\$ I've tried using this but I've got problems with the keyboardstate method that Roy T has given. Its giving an error for the line "GetState" saying its a method used incorrectly. I tried making a variable for the Update method for currentState but that hasnt worked either... :( \$\endgroup\$
    – Tom
    Commented Apr 25, 2011 at 13:01
  • \$\begingroup\$ Edit: I got it working, I made a local variable within the Update method but I forgot to put "= new KeyBoardState" which has made it work. Now when the game runs, it still displays all the questions one after another and doesnt stop.. \$\endgroup\$
    – Tom
    Commented Apr 25, 2011 at 13:06
4
\$\begingroup\$

The update and draw loops run roughly 60 times per second (30 on WPF, and it also depends on how much your computer is doing in each loop and how fast the computer is). To update the questions slower you need to keep track of the time passed. You can do that as following

float elapsedSeconds = 0; 
public void Update(GameTime gameTime){
     elapsedSeconds += (float)gameTime.ElapsedGameTime.TotalSeconds;
     if(elapsedSeconds > TimeIWantAQuestionToDisplay)
     {
       elapsedSeconds = 0;
       question = q.Next(questions.Count);
     }  
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ This would work for showing a question for a given length of time, but I suspect the question should remain visible until the player chooses an answer. \$\endgroup\$
    – Nate
    Commented Apr 21, 2011 at 19:20
  • \$\begingroup\$ Ah yes, that is probably what Tom meant he was a bit unclear in his wording though. +1 for you sir :) \$\endgroup\$
    – Roy T.
    Commented Apr 22, 2011 at 7:53
  • \$\begingroup\$ I apologize if my wording was not clear, yes the answers to the questions signify an object within a game. Each question answer (three different fruit) need to be assigned for the answers \$\endgroup\$
    – Tom
    Commented Apr 25, 2011 at 12:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .