How to create questions buttons so the programm knows from the position which answer is correct. Also don’t know how to set up the question class correctly. Second big problem: how to shrink the code.
Not very familiar with processing, having to do a project for university.
Every help will be greatly appreciated.
Main
Preformatted text`Button [] buttons = new Button[3];
Question [] questions = new Question [ 3 ];
int indexQuestions = 0;
final int StartScreen=0;
final int play=1;
final int gameOver=2;
int status = StartScreen;
void setup() {
size(1280, 800);
questions[0] = new Question( "Q1? ", "Answer1", "Answer2", "Answer3", "Answer4", 2, 640, 100 );
questions[1] = new Question( "Q1? ", "Answer1", "Answer2", "Answer3", "Answer4", 3, 640, 100 );
questions[2] = new Question( "Q1? ", "Answer1", "Answer2", "Answer3", "Answer4", 3, 640, 100 );
buttons[0] = new Button("START", 1050, 480, 200, 80); //Start
buttons[1] = new Button("HOW TO", 800, 480, 200, 80); // How To
buttons[2] = new Button("IMPRESSUM", 1000, 700, 250, 80); // Impressum
}
void draw() {
background(96, 154, 166);
PImage img;
img = loadImage("trump_face.png");
image(img, 100, 120);
switch (status) {
case StartScreen:
buttons[0].Draw();
buttons[1].Draw();
buttons[2].Draw();
if (buttons[1].MouseIsOver()) {
img = loadImage("impressum.jpg");
image(img, 0, 0);
}
if (buttons[2].MouseIsOver()) {
img = loadImage("impressum.jpg");
image(img, 0, 0);
}
break;
case play:
img = loadImage("game.png");
image(img, 0, 0);
questions[indexQuestions].display();
break;
case gameOver:
text ( "The END.", 200, 200 );
text ( "Click in the lowest right corner .", 200, 400 );
break;
}
}
void mousePressed() {
switch (status) {
case StartScreen:
if (buttons[0].MouseIsOver()) {
status = play;
break;
}
case play:
if (buttons[1].MouseIsOver())
{
if (questions[indexQuestions].check(key)) {
println ("Good");
} else {
println ("No");
}
indexQuestions++;
if (indexQuestions>questions.length-1)
{
status = gameOver;
} // if
} // if
break;
case gameOver:
if (buttons[2].MouseIsOver()) {
// restart
indexQuestions=0;
status=StartScreen;
}
break;
}
}
Buttons
class Button {
String myText;
float myX;
float myY;
float myWidth;
float myHeight;
Button( String tempText, float tempX, float tempY, float tempWidth, float tempHeight ) {
myText = tempText;
myX = tempX;
myY = tempY;
myWidth = tempWidth;
myHeight = tempHeight;
}
void Draw() {
//buttonLook
noStroke();
fill(255,0,0);
rect(myX,myY,myWidth,myHeight);
textSize(34);
fill(255);
textAlign(CENTER, CENTER);
text(myText, myX + ( myWidth / 2 ), myY + ( myHeight / 2 ) );
}
boolean MouseIsOver() {
if (mouseX > myX && mouseX < myX+myWidth && mouseY > myY && mouseY < myY+myHeight) {
return true;
}
return false;
}
}
Class questions
class Question {
String quest;
String answer1, answer2, answer3, answer4 ;
int correctAnswerNumber;
int posX, posY;
Question (String que, String answer1temp, String answer2temp, String answer3temp, String answer4temp, int correctAnswerNumberTemp, int x, int y) {
this.quest = que;
answer1=answer1temp;
answer2=answer2temp;
answer3=answer3temp;
answer4=answer4temp;
correctAnswerNumber = correctAnswerNumberTemp;
posX=x;
posY=y;
}
void position(int x, int y) {
this.posX = x;
this.posY = y;
}
void display() {
textSize(34);
text(quest, posX, posY);
text(answer1, posX, posY+50);
text(answer2, posX, posY+150);
text(answer3, posX, posY+200);
text(answer3, posX, posY+250);
} // method
boolean check( char keyToTest ) {
if (keyToTest=='1' && correctAnswerNumber==1) return true;
if (keyToTest=='2' && correctAnswerNumber==1) return true;
if (keyToTest=='3' && correctAnswerNumber==1) return true;
if (keyToTest=='3' && correctAnswerNumber==1) return true;
return false;
}
}