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!