Hi all, I am new to both coding and this forum so sorry in advance for whatever errors I am probably already making. For context, for this project, we were provided with a code skeleton. Right now I am having trouble figuring out how to move from one question to the next. The pieces are there, I just don’t know how to put them together and it’s driving me a little crazy. Should I be using cases or I have a feeling some kind of loop would probably be best? Not sure how much code is necessary but below are the two main tabs. I’d truly appreciate any help you all could provide.
ArrayList<Question> questions;
ArrayList<Answer> answers;
int state = 0; //game state
int activeQuestion = 0; //which question id is active
int score = 0;
int titleH;
boolean answered = false;
PFont ubu;
color c1 = #4EEE94;
color c2 = #68C7D4;
color c3 = #FFCC00;
XML data;
void setup(){
size(640, 640);
titleH = height/2 - 100;
loadData();
ubu = createFont("Ubuntu-Medium.ttf", 30);
}
void draw(){
background(c2);
switch(state){
case 0:
startScreen();
break;
case 1:
game();
break;
case 2:
endScreen();
break;
}
}
//Create array lists of objects and load external data
void loadData(){
data = loadXML("QuizGame.xml");
questions = new ArrayList();
answers = new ArrayList();
XML[] qnas = data.getChildren("qna");
for(int i = 0; i < qnas.length; i++){
String qt = qnas[i].getChild("qText").getContent();
String at = qnas[i].getChild("aText").getContent();
int id = qnas[i].getInt("id");
int score = qnas[i].getChild("aText").getInt("score");
String possAnsS = qnas[i].getChild("possAns").getString("pA");
String[] possAns = split(possAnsS, ',');
String cA = qnas[i].getChild("aText").getString("cA");
PImage img = loadImage(qnas[i].getChild("qText").getString("img"));
Question q = new Question(qt, id, score, img);
Answer a = new Answer(at, id, score, img, possAns, cA);
questions.add(q);
answers.add(a);
}
}
void mouseClicked(){
if(overStart){
state = 1; //move to the game state
//check the multiple choice buttons for clicks
for(int i = 0; i < overMulti.length; i++){
if(overMulti[i]){
questions.get(activeQuestion).guess = answers.get(activeQuestion).possAns[i];
answered = true;
//THIS IS WHERE THE USER ANSWERS
}
}
}
}
boolean overStart = false;
boolean overCont = false;
boolean overSubmit = false;
boolean[] overMulti = {false, false, false, false};
void startScreen(){
//Display title
textAlign(CENTER);
textSize(36);
textFont(ubu);
fill(255);
text("Fun Fact Trivia!", width/2, titleH);
//Start button
noStroke();
overStart = button(width/2, height/2, 150, 75, "START");
if((mouseX>width/2) && (mouseX<width/2 + 150) && (mouseY>height/2) && (mouseY<height/2+75)) {
//fill(c1);
println("fill");
} else {
//fill(c3);
println("fill2");
}
//Score and Q# displayed
scoreAndQN();
//if(.correctAns == true score ++);
//else()
}
void game(){
//Active question and answer objects
Question q = questions.get(activeQuestion);
Answer a = answers.get(activeQuestion);
//Display img
q.displayImage(width/4,height/4);
//Display text
q.displayText();
//Display multiple choice buttons
for(int i = 0; i < a.possAns.length; i++){
//fill(c1);
overMulti[i] = button(width/2, height/2+90*i, 150, 75, a.possAns[i]);
if((mouseX>width/2) && (mouseX<width/2 + 150) && (mouseY>height/2+90*i) && (mouseY<height/2+90*i+75)) {
fill(c1);
//println("yes");
} else {
fill(c3);
//println("unlucky");
}
}
if(answered){
q.checkAnswer();
}
//Score and Q# displayed
scoreAndQN();
}
void feedbackScreen() {
overCont = button(550, 550, 150, 75, "Continue");
fill(0);
//if correct play this sound, if not, play this sound or show check or x
}
void endScreen(){
//display score somehow
}
void scoreAndQN(){
textSize(28);
fill(255);
text("Score: "+score, width-100, 40);
text("Question: "+activeQuestion, 100, 40);
}
boolean button(int x, int y, int w, int h, String s){
rect(x-w/2,y-h/2,w,h);
textSize(28);
text(s, x, y+10);
if((mouseX>x-w/2) && (mouseX<x-w/2 + w) && (mouseY>y-h/2) && (mouseY<y-h/2+y)) {
//println("over");
return true;
} else {
//println("not over");
return false;
}
}