True or False game / If mousepressed correct answer, show next question randomly

Hi guys, I’d like to make a quiz game.
basically, the question will show randomly and if there are 2 buttons,
one is wrong and one has to be correct.
And also If the user clicked correct answer, I want the question will show randomly.
Help!

Thanks

String[] Texts = {
“A”,
“B”,
“C”,
“D”,
“E”,
“F”,
“G”};

int n = int (random(0, 6));

String myText;
int totalWrong = 0;
int totalCorrect = 0;
int question = 0;
String correctAnswer;
String WrongAnswer;
int True= totalCorrect++;
int False= totalWrong++;

void setup() {
size(800,400);
background(255);
smooth();
ellipseMode(CENTER);
}

void draw() {
noStroke();
fill(255,20,40);
ellipse(520,100,20,20);
ellipse(620,100,20,20);

fill(0);
rect(width/2,height/2,300,100);
textSize(20);
text(“Texts”,10,30);
fill(255);

text("", 0, 300, 800, 700);
text(question, 20, 80);
text(“Correct:”, 600, 40);
text(totalCorrect, 620, 80);
text(“Incorrect:”, 600, 140);
text(totalWrong, 620, 180);
}

void Button(){
ellipse(520,200,20,20);
}

void mousePressed(){
Button();
}

1 Like

Hello Ellen,

Could you please edit your post and format your code using the </> button ?

You haven’t told us what is the issue. What have you tried, where are you stuck?

Hello!

Welcome to the forum !

Great to have you here!

Please note that you have int n = int (random(0, 6)); at start of the Sketch.
This is ran only once. It isn’t repeated throughout. So you have to repeat the line in your Sketch where you need it.

the same is true for these lines:

int True= totalCorrect++;
int False= totalWrong++;

You can’t declare them at the start and then hope they are executed again and again. Instead repeat them where needed in your Sketch.

Remark

You set n as number of current question but you don’t use it.

Try text(Texts[n], 10, 30);

Remark

You also need the knowledge whether a text/question has Yes or no as a correct answer. Example:

String[] RightAnswer = {
  "Yes", 
  "No", 
  "Yes", 
  "No", 
  "No", 
  "Yes", 
  "No"
};

Note that both arrays belong together. So the 3rd question belongs to the 3rd answer. Both use n as index!

The mouse

When the mouse is pressed you need to check whether button1(Yes) or button2(No) has been pressed (dist() command gives distance of the mouse to the button 1 and 2).

Then compare button Yes/No to the correct answer for this question (from array RightAnswer at position n this is). Set totalWrong and totalCorrect accordingly.

Go on to next question, which means to set n again.

Warm regards,

Chrisir

Example

String[] Texts = {
  "Question A", 
  "Question B", 
  "Question C", 
  "Question D", 
  "Question E", 
  "Question F", 
  "Question G"
};

String[] RightAnswer = {
  "Yes", 
  "No", 
  "Yes", 
  "No", 
  "No", 
  "Yes", 
  "No"
};

int n = int (random(0, 6));

//String myText;
int totalWrong = 0;
int totalCorrect = 0;
//int question = 0;
//String correctAnswer;
//String WrongAnswer;
//int True= 0;
//int False= 0;

void setup() {
  size(800, 400);
  background(255);
  smooth();
  ellipseMode(CENTER);
}

void draw() {
  // draw() runs 60 times per second again and again

  // we clear the screen to WHITE  
  background(255);

  // we show stuff 
  noStroke();

  //Button 1
  fill(255, 20, 40);
  if (button1()) 
    fill(0, 255, 0); 
  ellipse(320, 100, 20, 20);
  text("YES", 320+22, 100);

  //Button 2
  fill(255, 20, 40);
  if (button2()) 
    fill(0, 255, 0); 
  ellipse(420, 100, 20, 20);
  text("NO", 420+22, 100); 

  fill(0);
  rect(width/2, height/2, 300, 100);
  textSize(20);
  text(Texts[n], 10, 30);
  text(RightAnswer[n], 10, 30+22); // for testing what happens!!! 

  fill(255);

  fill(0); 
  text("", 0, 300, 800, 700);
  //text(question, 20, 80);
  text("Correct? ", 20+110+55, 80+30);
  text("Correct:", 600, 40);
  text(totalCorrect, 620, 80);
  text("Incorrect:", 600, 140);
  text(totalWrong, 620, 180);
}

void mousePressed() {
  // CHECK Buttons
  if (button1()) {
    // button 1: Yes
    println("button1");
    if (RightAnswer[n].equals("Yes")) {
      totalCorrect++;
    } else {
      totalWrong++;
    }
    goOn();
  } else if (button2()) {
    // button 2: No
    println("button2");
    if (RightAnswer[n].equals("No")) {
      totalCorrect++;
    } else {
      totalWrong++;
    }
    goOn();
  }//else if
}

void goOn() {
  n = int (random(0, 6));
} 

boolean button1() {
  return
    dist(mouseX, mouseY, 320, 100) < 70;
} 

boolean button2() {
  return
    dist(mouseX, mouseY, 480, 100) < 70;
} 
//

1 Like