Question does not show on screen

Hi everyone,
I am working on a quiz game in processing. The first screen will display just a welcoming text and you should press space in order to move to the next screen. I am using states to control when each screen should display. The first page works, but when I press space, the question does not show on screen. Here is my code:

Question [] questions; 
Screens sc;
Answers ans;
PImage [] answ;
float mx,my;
final int stateFirst=0; //cannot be changed
final int stateQuestion=1;
final int stateGameOver=2;
final int stateq1=3;
final int stateq2=4;
final int stateq3=5;
final int stateq4=6;
final int stateq5=7;

int currentState=stateFirst; 


void setup(){
  size(600,600);
 sc=new Screens();
//-------------------------------------------    
  String [] text=loadStrings("questions.txt");//load the file
  questions=new Question[text.length]; 
  
  for(int i=0;i<questions.length;i++){
    String [] q=split(text[i],"?"); //split the text file whenever we hit a question mark
    questions[i]=new Question(q);
  }
 
//-------------------------------------------  
  
}

void draw(){
    sc.showState();
    for(int i=0;i<questions.length;i++){
    
    }
    
    
}

void keyPressed(){ //if spacebar is pressed, we move to the question screen
  if(key==' '){
    currentState=stateQuestion; 
  }
}

void mousePressed(){
  mx=mouseX;
  my=mouseY;
  
  
  
}
class Question {
  String [] options;
  int qnumber;
  color qcolor;
  
Question(String [] options_){
  options=options_;
  qnumber=1;
  qcolor=color(0,0,0);
}
  void showQuestion(){
    for(int i=0;i<options.length;i++){
      textAlign(CENTER);
      textSize(30);
      fill(qcolor);
      text(options[i],300,100);
    }
  }
  void showQuestionState(){
    for(int i=0;i<questions.length;i++){
    switch(currentState){
      case stateq1:
      questions[0].showQuestion();
      break;
      case stateq2:
      questions[1].showQuestion();
      break;
      case stateq3:
      questions[2].showQuestion();
      break;
      case stateq4:
      questions[3].showQuestion();
      break;
      case stateq5:
      questions[4].showQuestion();
      break;
    }
    }
    
  
  }
  
  
}
class Screens {
int score;
color questionscreenbg;
color lastsc1;
color lastsc2;

  Screens(){
   questionscreenbg=color(183,132,167); //opera mauve
   lastsc1=color(100,41,37); //red
   lastsc2=color(2,78,36); //green
    score=0;
  }
  void showState(){
    switch(currentState){
      case stateFirst:
      firstScreen();
      break;
      case stateGameOver:
      lastScreen();
      break;
      case stateQuestion:
      questionScreen();
    }
  }
  void firstScreen(){
    background(0,0,150);
    textAlign(CENTER);
    textSize(50);
    text("WELCOME TO TRIVIA",300,200);
    textSize(30);
    text("Press space to start playing",300,400);
  }
  void lastScreen(){
    if(score>5){
      background(lastsc2);
      
      textAlign(CENTER);
      textSize(30);
      text("CONGRATS!",300,200);
      text("YOUR SCORE IS:"+score,300,300);
    }
    else{
      background(lastsc1);
       
      textAlign(CENTER);
      textSize(30);
      text("SORRY, YOU LOST!",300,200);
      text("YOUR SCORE IS:"+score,300,300);
    }
    
  }
  void questionScreen(){
    background(questionscreenbg);
    noFill();
    rect(50,200,250,400);
    rect(300,200,250,400);
    
  }
  }

I have been looking at this for a while now and I still cannot figure it out. Any type of help would be greatly appreciated!

1 Like
  1. ‘loadStrings’ creates a string array if you place each question on a separate line. If you change the name of the array to ‘questions’ I’m not sure you need the separate questions array that you try to create by splitting the text array at the question marks.
  2. Function ‘questionScreen’ is called, but you don’t tell it which question to print. It won’t print a question if you don’t tell it which one to print.
1 Like

Is not called (needs to be called from questionScreen())

From an architectural perspective a few things are to be criticized if you don’t mind me saying.

2 Likes

For example, this is an array. You should use it as such, so use it with an index:
questions [ currentQuestion ]

Then you don’t need stateq1, stateq2… and don’t need the long switch section in showQuestionState().

you don’t need the for loop in showQuestionState()

  • showQuestionState() should also not be inside the class since it contains Sketch logic, and not class logic imho
3 Likes

Thanks to all of you for your suggestions! I managed to fix my issue and also reshape the structure of the code. It sure does look better and it’s easier to work with. Im a beginner so any form of criticism helps a lot.

1 Like