game doesnt resume after answering quiz question

edited December 2017 in Questions about Code

Hallo guys iam currently programming a game for my project and dont know hot to solve my problem. When the figur enters a sprecific space defined in the method enterRect() a new quiz screen is called. when ur answer is correct the method correctAnswer will be called and when u answer false the method falseAnswer() will be called.My problem is after i the answer question the display doesnt change from correct/false to the game. my variable correctAnswer/falseAnswer is always on 1 after answering the question.

Edit the methods correctAnswer and falseAnswer() are in the Question class.

Code:

my question variables and methods for quiz screen:

    public int falseAnswer=0;
    public int correctAnswer=0;

void setup() {
  frameRate(60);
  questions= new Question[7];

  ball= new Ball(50);
  s= new Shape();
  s.createRect(100, 80);
  s.createRect(100, 500);

  s.createRect(800, 80);
  s.createRect(800, 500);
}

void draw() {

  if (gameScreen==0) {
    initScreen();
  } else if (gameScreen==1) {
    gameStartScreen();
  } else if (gameScreen==2) {
    gameOverScreen();
  } else if (gameScreen==3) {
    quizScreen();
  }
  //print("K "+correctAnswer+" F "+falseAnswer);
}


public void quizScreen() {
  questions[0]=new Question("Wer", "Wer", "Wer", "Wer", "Wer");
  question=questions[0];
  if (correctAnswer==1) {
    question.correctAnswer();
  } else if (falseAnswer==1) {
    question.falseAnswer();
  }
}


void startGame() {
  gameScreen=1;
}
void gameOver() {
  gameScreen=2;
}
public void startQuiz() {
  gameScreen=3;
}


void mousePressed() {

  // if we are on the initial screen , start the game 
  if (gameScreen==0) { 
    startGame();
  }
  if (gameScreen==1) {
    startGame();
  }
  if (gameScreen==2) {
    startGame();
  }
  if (gameScreen==3) {
    startQuiz();
   if(correctAnswer==1){
    startGame();
   }else if(falseAnswer==1){
    gameStartScreen(); 
   }
}
}



public void mouseReleased() {
  if (mouseX >0 && mouseX < (0+150) && mouseY >150 && mouseY<(150+150)) {
    falseAnswer=1;
  } else if (mouseX >150 && mouseX <(150+150) && mouseY >150 && mouseY< (150+150)) {
    falseAnswer=1;
  } else if ( mouseX > 300 && mouseX <(300+150) && mouseY >150 && mouseY <(150+150)) {
    falseAnswer=1;
  } else if (mouseX >450 && mouseX <(450+150) && mouseY >150 && mouseY <(150+150)) {
    correctAnswer=1;
  }
}



public void enterRect() {

  //not finished
  if (x>=85 && x<=245 && y>=60 && y<=135 ) {
    startQuiz();
  }
}
Tagged:

Answers

  • You've got a lot going on here. I suspect the issue is that, after the quiz answer is given, you never actually have the sketch leave the in-a-quiz state.

  • Yeah that seems to be the Problem but i dont know how to solve it. Shall I post my whole main sketch code ?

  • MAIN SKETCH

    import java.util.*;
    public int x=40;
    public int y=350;
    public final int speed=15;
    public int radius=40;
    public Shape s;
    public Ball ball;
    public boolean startTimer=true;
    public int time;
    public int count;
    public int gameScreen=3;
    public Question question;
    public int correctAnswer=0;
    public int falseAnswer=0;
    public ArrayList<Ball> smallBalls;
    public ArrayList<Ball> bigBalls;
    public Question [] questions;
    
    
    
    
    public void settings() {
      size(1200, 700);
    }
    
    public void setup() {
      frameRate(60);
      questions= new Question[7];
      //ball=new Ball(40);
      //Creating Ball adding them to ArrayList,bigger Balls slower smaller ones faster
      smallBalls= new ArrayList<Ball>();
      bigBalls=new ArrayList<Ball>();
      addBigBalls();
      addSmallBalls();
    
      s= new Shape();
      s.createRect(100, 80);
      s.createRect(100, 500);
    
      s.createRect(800, 80);
      s.createRect(800, 500);
    }
    
    public void draw() {
    
      if (gameScreen==0) {
        initScreen();
      } else if (gameScreen==1) {
        gameStartScreen();
      } else if (gameScreen==2) {
        gameOverScreen();
      } else if (gameScreen==3) {
        quizScreen();
      }
      //print("K "+correctAnswer+" F "+falseAnswer);
    }
    //add Balls to ArrayList
    
    
    public void initScreen() {
      background(236, 240, 241);
      textAlign(CENTER);
      fill(52, 73, 94);
      textSize(70);
      text("Ma Game", width/2, height/2);
      textSize(25); 
      text("Click to start", width/2, height-30);
    }
    
    
    public void gameOverScreen() {
      background(44, 62, 80);
      textAlign(CENTER);
      fill(236, 240, 241);
      textSize(70);
      text("GAME OVER", width/2, height/2);
      textSize(25);
      text("Click to Restart", width/2, height-30);
    }
    
    public void gameStartScreen() {
      s.createBackground();
    
      //create Shapefigur
      //  background(173, 244, 66);
      fill(244, 232, 66);
      ellipse(x, y, radius, radius);
      fill(255, 255);
      ellipse(x-5, y-10, 5, 5);
      ellipse(x+5, y-10, 5, 5);
      rect(x-10, y, 20, 3);
      //move and create Ball
    
    
    
    
    
    
    
    
      //create rects on display
    
      //s.createRect(100, 500);
    
    
      // s.createRect(800, 500);
      s.createClouds();
      s.createSun();
      s.createGround();
    
    
    
      //timer
      startTimer();
      ballTimer();
    
      //figur entering rects?
      enterRect();
    
      //addBalls();
    }
    
    public void quizScreen() {
      questions[0]=new Question("Wer", "Wer", "Wer", "Wer", "Wer");
      question=questions[0];
      if (correctAnswer==1) {
        question.correctAnswer();
      } else if (falseAnswer==1) {
        question.falseAnswer();
      }
    }
    
    
    public void addSmallBalls() {
    
      for (int i=0; i<5; i++) {
        smallBalls.add(new Ball(random(10, 20), random(18, 25), random(18, 20)));
      }
    }
    
    public void addBigBalls() {
      for (int i=0; i<5; i++) {
        bigBalls.add(new Ball(random(30, 50), random(8, 15), random(8, 15)));
      }
    }
    
    public void ballTimer() {
    
      bigBalls.get(0).move();
      bigBalls.get(0).display();
    
    
      if ((time/1000)>10) {
        smallBalls.get(0).move();
        smallBalls.get(0).display();
      }
    
      if ((time/1000)>20) {
        bigBalls.get(1).move();
        bigBalls.get(1).display();
      }
      if ((time/1000)>30) {
        smallBalls.get(1).move();
        smallBalls.get(1).display();
      }
    }
    
    // This method sets the necessery variables to start the game  
    public void startGame() {
      gameScreen=1;
    }
    public void gameOver() {
      gameScreen=2;
    }
    public void startQuiz() {
      gameScreen=3;
    }
    
    public void mousePressed() {
    
      // if we are on the initial screen when clicked, start the game 
      if (gameScreen==0) { 
        startGame();
      }
      if (gameScreen==1) {
        startGame();
      }
      if (gameScreen==2) {
        restart();
      }
      if (gameScreen==3) {
        startQuiz();
        if (correctAnswer==1) {
          startGame();
        }
        if (falseAnswer==1) {
          restart();
        }
      }
    }
    
    public void mouseReleased() {
      if (mouseX >0 && mouseX < (0+150) && mouseY >150 && mouseY<(150+150)) {
        falseAnswer=1;
      } else if (mouseX >150 && mouseX <(150+150) && mouseY >150 && mouseY< (150+150)) {
        falseAnswer=1;
      } else if ( mouseX > 300 && mouseX <(300+150) && mouseY >150 && mouseY <(150+150)) {
        falseAnswer=1;
      } else if (mouseX >450 && mouseX <(450+150) && mouseY >150 && mouseY <(150+150)) {
        correctAnswer=1;
      }
    }
    
    
    public void keyPressed() {
    
      if (key == CODED) {
        if (keyCode == UP && y>radius) {
          y -= speed;
        } else if (keyCode == DOWN && y<height-radius) {
          y += speed;
        } else if (keyCode == LEFT && x>radius) {
          x -= speed;
        } else if (keyCode == RIGHT && x< width-radius) {
          x += speed;
        }
      }
    }
    
    
    public void startTimer() {
    
      if (startTimer==true) {
        time= millis();
        textSize(20);
        fill(0, 0, 0);
        text(("Time: "+time/1000), 1100, 30);
      }
    }
    
    public void restart() {
      time=0;
      gameScreen=1;
    }
    
    public void enterRect() {
    
      //not finished
      if (x>=85 && x<=245 && y>=60 && y<=135 ) {
        startQuiz();
      }
    

    }

    Question Class

    class Question {
    
    
    
      public String question;
      public String a1;
      public String a2;
      public String a3;
      public String a4;
    
    
    
    
      public Question(String question, String a1, String a2, String a3, String a4) {
    
        //filling global variables with parameters
        this.question=question;
        this.a1=a1;
        this.a2=a2;
        this.a3=a3;
        this.a4=a4;
    
        //creating rects for question and asnwers
        background(150);
        fill(255, 200, 200);
        rect(0, 0, 1200, 350);
        fill(255, 0, 0, 127);
        rect(0, height/2, 300, 600);
        rect(300, height/2, 600, 600);
        rect(600, height/2, 900, 600);
        rect(900, height/2, 1200, 600);
    
        //filling the rects with question and answers
        fill(0);
        textSize(15);
        text(question, 0, 175);
        text(a1, 0, 525);
        text(a2, 300, 525);
        text(a3, 600, 525);
        text(a4, 900, 525);
      }
    
    
    
      public void correctAnswer() {
        // background(124, 252, 0);
        background(173, 244, 66);
        textAlign(CENTER);
        textSize(70);
        text("Correct Answer", width/2, height/2);
        textSize(25); 
        text("Click to resume the game", width/2, height-30);
    
      }
    
      public void falseAnswer() {
        background(200, 150, 0);
        textAlign(CENTER);
        textSize(70);
        text("False Answer", width/2, height/2);
        textSize(25); 
        text("Start new game", width/2, height-30);
      }
    
    }
    

    Ball Class

    class Ball {
      public float radius;   
      public float x;
      public float y; 
      public float speedX;
      public float speedY; 
    
    
      public Ball(float radius, float speedX,float speedY) {
        this.radius = radius;
        this.x = random(width);
        this.y = random(height);
        this.speedX = speedX;
        this.speedY = speedY;
    
    }
    
    
      public void setSpped(int speedX, int speedY){
       this.speedX=speedX;
       this.speedY=speedY;
      }
    
    
    
    
      public void move() {
        x += speedX; 
        y += speedY; 
    
    
        if (x > width || x < 0) {
          speedX *= -1;
        }
    
        if (y > height || y < 0) {
          speedY *= -1;
        }
      }
    
      // Draw the ball
     public void display() {
        fill(150);
        ellipse(x, y, radius*2, radius*2);
      }
    
    
    
    
    
    }
    

    class Shape

    class Shape {
      public float a;
      public float b;
      public PImage cloud;
      public PImage sun;
      PImage ground;
      PImage background;
      PImage sky;
    
      public void createRect( float a, float b) {
        fill(255); 
        rect(a, b, 130, 40);
      }
    
      public void createClouds(){
    /*
        image(cloud,100,50);
        image(cloud,500,50);
        image(cloud,900,50);*/
    
      }
    
      public void createSun(){
    /*
        image(sun,400,10);
        */
      }
    
      public void createGround(){
     /*
        image(ground,0,550);*/
      }
    
      public void createEllipse(float a, float b) {
        ellipse(a, b, 100, 80);
      }
    
      public void createBackground(){
      background= loadImage("Ebackground.png");
      image(background,0,0);
      }
    
    
    }
    
  • why can't people post their tabs in one go...

  • Iam sry, first time posting a question here.

  • don't worry

    wait a second

  • edited December 2017

    is the issue around here, in mousePressed()? :

      else if (gameScreen==3) {
        startQuiz(); // ????????
        if (correctAnswer==1) {
          startGame();
        }
        if (falseAnswer==1) {
          restart();
        }
      }
    

    because startGame() and restart() are almost the same: gameScreen=1;

    in mousePressed() you should use not if...if... but if...else if..... !!!

    in mouseReleased() and in keyPressed() you should use gameScreen too in this way.

    you need to reset correctAnswer and falseAnswer to 0

    Also because of this:

    When the figur enters a sprecific space defined in the method enterRect() a new quiz screen is called

    you maybe want to move the figure out of this space when going back to the ball game

    Remark

    when I click an answer nothing happens - only when clicking the question? The rects are totally off.

    Remark II

    I've seen horrible horrible things in this code. I won't comment on them. I haven't changed them.

    New version

    new version with reset correctAnswer and falseAnswer to 0 :

    //
    
    
    import java.util.*;
    
    
    public int x=40;
    public int y=350;
    public final int speed=15;
    public int radius=40;
    public Shape s;
    public Ball ball;
    public boolean startTimer=true;
    public int time;
    public int count;
    
    public int gameScreen=3;  // 3
    
    public Question question;
    public int correctAnswer=0;
    public int falseAnswer=0;
    public ArrayList<Ball> smallBalls;
    public ArrayList<Ball> bigBalls;
    public Question [] questions;
    
    
    // ------------------------------------------------------------------
    
    public void settings() {
      size(1200, 700);
    }
    
    public void setup() {
      frameRate(60);
      questions= new Question[7];
      //ball=new Ball(40);
      //Creating Ball adding them to ArrayList,bigger Balls slower smaller ones faster
      smallBalls= new ArrayList<Ball>();
      bigBalls=new ArrayList<Ball>();
      addBigBalls();
      addSmallBalls();
    
      s= new Shape();
      s.createRect(100, 80);
      s.createRect(100, 500);
    
      s.createRect(800, 80);
      s.createRect(800, 500);
    }
    
    public void draw() {
    
      if (gameScreen==0) {
        initScreen();
      } else if (gameScreen==1) {
        gameStartScreen();
      } else if (gameScreen==2) {
        gameOverScreen();
      } else if (gameScreen==3) {
        quizScreen();
      }
      //print("K "+correctAnswer+" F "+falseAnswer);
    }
    //add Balls to ArrayList
    
    
    public void initScreen() {
    
      background(236, 240, 241);
    
      textAlign(CENTER);
      fill(52, 73, 94);
      textSize(70);
      text("Ma Game", width/2, height/2);
      textSize(25); 
      text("Click to start", width/2, height-30);
    }
    
    
    public void gameOverScreen() {
      background(44, 62, 80);
      textAlign(CENTER);
      fill(236, 240, 241);
      textSize(70);
      text("GAME OVER", width/2, height/2);
      textSize(25);
      text("Click to Restart", width/2, height-30);
    }
    
    public void gameStartScreen() {
      s.createBackground();
    
      //create Shapefigur
      //  background(173, 244, 66);
      fill(244, 232, 66);
      ellipse(x, y, radius, radius);
      fill(255, 255);
      ellipse(x-5, y-10, 5, 5);
      ellipse(x+5, y-10, 5, 5);
      rect(x-10, y, 20, 3);
      //move and create Ball
    
      //create rects on display
    
      //s.createRect(100, 500);
    
    
      // s.createRect(800, 500);
      s.createClouds();
      s.createSun();
      s.createGround();
    
      //timer
      startTimer();
      ballTimer();
    
      //figur entering rects?
      enterRect();
    
      //addBalls();
    }
    
    public void quizScreen() {
      questions[0]=new Question("Who ? ", " Answer 1", " A 2 ", " A 3", "A 4");
      question=questions[0];
      if (correctAnswer==1) {
        question.correctAnswer();
      } else if (falseAnswer==1) {
        question.falseAnswer();
      }
    }
    
    
    public void addSmallBalls() {
    
      for (int i=0; i<5; i++) {
        smallBalls.add(new Ball(random(10, 20), random(18, 25), random(18, 20)));
      }
    }
    
    public void addBigBalls() {
      for (int i=0; i<5; i++) {
        bigBalls.add(new Ball(random(30, 50), random(8, 15), random(8, 15)));
      }
    }
    
    public void ballTimer() {
    
      bigBalls.get(0).move();
      bigBalls.get(0).display();
    
    
      if ((time/1000)>10) {
        smallBalls.get(0).move();
        smallBalls.get(0).display();
      }
    
      if ((time/1000)>20) {
        bigBalls.get(1).move();
        bigBalls.get(1).display();
      }
      if ((time/1000)>30) {
        smallBalls.get(1).move();
        smallBalls.get(1).display();
      }
    }
    
    // This method sets the necessary variables to start the game  
    public void startGame() {
      gameScreen=1;
      correctAnswer=0;
      falseAnswer=0;
    }
    public void gameOver() {
      gameScreen=2;
    }
    public void startQuiz() {
      gameScreen=3;
    }
    
    public void restart() {
      time=0;
      gameScreen=1;
      correctAnswer=0;
      falseAnswer=0;
    }
    
    public void mousePressed() {
    
      // if we are on the initial screen when clicked, start the game 
      if (gameScreen==0) { 
        startGame();
      } else if (gameScreen==1) {
        startGame();
      } else if (gameScreen==2) {
        restart();
      } else if (gameScreen==3) {
        startQuiz(); // ????????we are already on 3
        if (correctAnswer==1) {
          startGame();
        } else if (falseAnswer==1) {
          restart();
        }
      }
    }
    
    public void mouseReleased() {
      if (mouseX >0 && mouseX < (0+150) && mouseY >150 && mouseY<(150+150)) {
        falseAnswer=1;
      } else if (mouseX >150 && mouseX <(150+150) && mouseY >150 && mouseY< (150+150)) {
        falseAnswer=1;
      } else if ( mouseX > 300 && mouseX <(300+150) && mouseY >150 && mouseY <(150+150)) {
        falseAnswer=1;
      } else if (mouseX >450 && mouseX <(450+150) && mouseY >150 && mouseY <(150+150)) {
        correctAnswer=1;
      }
    }
    
    
    public void keyPressed() {
    
      if (key == CODED) {
        if (keyCode == UP && y>radius) {
          y -= speed;
        } else if (keyCode == DOWN && y<height-radius) {
          y += speed;
        } else if (keyCode == LEFT && x>radius) {
          x -= speed;
        } else if (keyCode == RIGHT && x< width-radius) {
          x += speed;
        }
      }
    }
    
    
    public void startTimer() {
    
      if (startTimer==true) {
        time= millis();
        textSize(20);
        fill(0, 0, 0);
        text(("Time: "+time/1000), 1100, 30);
      }
    }
    
    
    
    public void enterRect() {
    
      //not finished
      if (x>=85 && x<=245 && y>=60 && y<=135 ) {
        startQuiz();
      }
    }
    
    ///Question Class
    
    
    class Question {
    
    
    
      public String question;
      public String a1;
      public String a2;
      public String a3;
      public String a4;
    
    
    
    
      public Question(String question, String a1, String a2, String a3, String a4) {
    
        //filling global variables with parameters
        this.question=question;
        this.a1=a1;
        this.a2=a2;
        this.a3=a3;
        this.a4=a4;
    
        //creating rects for question and asnwers
        background(150);
        fill(255, 200, 200);
        rect(0, 0, 1200, 350);
        fill(255, 0, 0, 127);
        rect(0, height/2, 300, 600);
        rect(300, height/2, 600, 600);
        rect(600, height/2, 900, 600);
        rect(900, height/2, 1200, 600);
    
        //filling the rects with question and answers
        fill(0);
        textSize(15);
        text(question, 0, 175);
        text(a1, 0, 525);
        text(a2, 300, 525);
        text(a3, 600, 525);
        text(a4, 900, 525);
      }
    
    
    
      public void correctAnswer() {
        // background(124, 252, 0);
        background(173, 244, 66);
        textAlign(CENTER);
        textSize(70);
        text("Correct Answer", width/2, height/2);
        textSize(25); 
        text("Click to resume the game", width/2, height-30);
      }
    
      public void falseAnswer() {
        background(200, 150, 0);
        textAlign(CENTER);
        textSize(70);
        text("False Answer", width/2, height/2);
        textSize(25); 
        text("Start new game", width/2, height-30);
      }
    }
    
    // =========================================================================
    //Ball Class
    
    
    class Ball {
      public float radius;   
      public float x;
      public float y; 
      public float speedX;
      public float speedY; 
    
    
      public Ball(float radius, float speedX, float speedY) {
        this.radius = radius;
        this.x = random(width);
        this.y = random(height);
        this.speedX = speedX;
        this.speedY = speedY;
      }
    
    
      public void setSpped(int speedX, int speedY) {
        this.speedX=speedX;
        this.speedY=speedY;
      }
    
    
    
    
      public void move() {
        x += speedX; 
        y += speedY; 
    
    
        if (x > width || x < 0) {
          speedX *= -1;
        }
    
        if (y > height || y < 0) {
          speedY *= -1;
        }
      }
    
      // Draw the ball
      public void display() {
        fill(150);
        ellipse(x, y, radius*2, radius*2);
      }
    }
    
    // class Shape
    
    
    
    class Shape {
      public float a;
      public float b;
      public PImage cloud;
      public PImage sun;
      PImage ground;
      PImage background;
      PImage sky;
    
      public void createRect( float a, float b) {
        fill(255); 
        rect(a, b, 130, 40);
      }
    
      public void createClouds() {
        /*
        image(cloud,100,50);
         image(cloud,500,50);
         image(cloud,900,50);*/
      }
    
      public void createSun() {
        /*
        image(sun,400,10);
         */
      }
    
      public void createGround() {
        /*
        image(ground,0,550);*/
      }
    
      public void createEllipse(float a, float b) {
        ellipse(a, b, 100, 80);
      }
    
      public void createBackground() {
        background= loadImage("Ebackground.png");
        if (background!=null) 
          image(background, 0, 0);
        else {
          background(0); 
          text("something", 122, 122);
        }//else
      }
    }
    //
    
  • edited December 2017

    first

    correctAnswer and falseAnswer are redundant: only one can be 1 at a time, or both be 0.

    instead, you should have 2 new gameScreens, gameScreenFalseAnswer and gameScreenRightAnswer

    handle those like the other gameScreens in draw, in mousePressed, in mouseReleased, in keyPressed

    so you got rid of correctAnswer and falseAnswer completely

    second

    you can name your states:

    final int gameScreenInit = 0;
    final int gameScreenStartScreen = 1;
    final int gameScreenGameOverScreen = 2;
    final int gameScreenQuizScreen = 3;
    

    etc.

    note that they are constants (final) and all are starting with their purpose: gameScreen.....

    then your draw() is better readable:

     if (gameScreen==gameScreenInit ) {
        initScreen();
      } else if (gameScreen==gameScreenStartScreen ) {
        gameStartScreen();
      } else if (gameScreen==gameScreenGameOverScreen ) {
        gameOverScreen();
      } else if (gameScreen==gameScreenQuizScreen ) {
        quizScreen();
      }
      //print("K "+correctAnswer+" F "+falseAnswer);
    }
    

    Third, use switch() in draw(), in mousePressed, in mouseReleased, in keyPressed :

    switch(gameScreen) {
    
      case gameScreenInit: 
            initScreen();
          break; 
    ....
    ...
    

    Fourth

    loadImages() only in setup

    Fifth

    define the entire array for your questions in setup, not on the way

        questions[0]=new Question("Who ? ", " Answer 1", " A 2 ", " A 3", "A 4");
        questions[1]=new Question("Where? ", " Answer B1", "A B2 ", " A B3", "A B4");
        ...
        .....
    

    Sixth

    you don't need the variable question at all, the array question is enough

  • when I click an answer nothing happens - only when clicking the question?

    its because i used at first PApplet with a small size than changed it to full size but forgot to adjust the coordinates sry!

    I've seen horrible horrible things in this code. I won't comment on them. I haven't changed them. Would be nice if u could tell me there i need to improve the code ofc if u arent anooyed already haha.

    Iam just gonna adjust the coordinates in mouspressed() but my main problem is when i start the game and enter the specific space defined in enterRect()

    mouseReleased() corrected public void mouseReleased() { if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) { falseAnswer=1; } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) { falseAnswer=1; } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) { falseAnswer=1; } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) { correctAnswer=1; } }

    and gameScreen corrected gameScreen=0

  • Would be nice if u could tell me there i need to improve the code

    our posts were at the same time, I discussed some of the issues above

    but my main problem is when i start the game and enter the specific space defined in enterRect()

    what is the problem there then?

  • don't do the question display in the constructor of the class

    make a method display() in the class

    and call it

  • edited December 2017

    our posts were at the same time, I discussed some of the issues above

    thank u alot!

    what is the problem there then?

    i guess the problem is that i never leave the in quiz state. when i answer the question either correct or answer the code switches between quiz and answer/false display but doesnt return to the game.thats why i think the problem is in the mousePressed() method ` public void mousePressed() {

      // if we are on the initial screen when clicked, start the game 
      if (gameScreen==0) { 
        startGame();
      } else if (gameScreen==1) {
        startGame();
      } else if (gameScreen==2) {
        restart();
      } else if (gameScreen==3) {
        startQuiz(); // ????????we are already on 3
        if (correctAnswer==1) {
          startGame();
        } else if (falseAnswer==1) {
          restart();
        }
      }
    }`
    
  • edited December 2017

    i guess the problem is that i never leave the in quiz state.

    I posted the entire sketch

    did you try it?

    It returns to the game alright

  • edited December 2017

    I posted the entire sketch

    did you try it?

    It returns to the game alright

    yeah it returns to the game after the first time answering the quiz but when u enter the space again its like loop between quiz and false/correct Answer display. And when u change gameScreen to 0 and enter the quiz space u cant reenter the game and its just a loop between quiz and false/answer display.

  • edited December 2017

    as I said :

    correctAnswer and falseAnswer are redundant: only one can be 1 at a time, or both be 0.

    instead, you should have 2 new gameScreens, gameScreenFalseAnswer and gameScreenRightAnswer

    handle those like the other gameScreens in draw, in mousePressed, in mouseReleased, in keyPressed

    so you got rid of correctAnswer and falseAnswer completely

  • edited December 2017

    as I said

    correctAnswer and falseAnswer are redundant: only one can be 1 at a time, or both be 0.

    instead, you should have 2 new gameScreens, gameScreenFalseAnswer and gameScreenRightAnswer

    handle those like the other gameScreens in draw, in mousePressed, in mouseReleased, in keyPressed

    so you got rid of correctAnswer and falseAnswer completely

    yeah i did it but either iam doing something wrong or it doesnt wanna work :/Below is my corrected Code did i do something wrong?

    import java.util.*;
    
    
    public int x=40;
    public int y=350;
    public final int speed=15;
    public int radius=40;
    public Shape s;
    public Ball ball;
    public boolean startTimer=true;
    public int time;
    public int count;
    public int gameScreen=0;  
    public Question question;
    public ArrayList<Ball> smallBalls;
    public ArrayList<Ball> bigBalls;
    public Question [] questions;
    final int gameScreenInit = 0;
    final int gameScreenStartScreen = 1;
    final int gameScreenGameOverScreen = 2;
    final int gameScreenQuizScreen = 3;
    final int gameScreenCorrectScreen=4;
    final int gameScreenFalseScreen =5;
    
    
    // ------------------------------------------------------------------
    
    public void settings() {
      size(1200, 700);
    }
    
    public void setup() {
      frameRate(60);
      questions= new Question[7];
      questions[0]=new Question("Who ? ", " Answer 1", " A 2 ", " A 3", "A 4");
      //ball=new Ball(40);
      //Creating Ball adding them to ArrayList,bigger Balls slower smaller ones faster
      smallBalls= new ArrayList<Ball>();
      bigBalls=new ArrayList<Ball>();
      addBigBalls();
      addSmallBalls();
    
      s= new Shape();
      s.createRect(100, 80);
      s.createRect(100, 500);
    
      s.createRect(800, 80);
      s.createRect(800, 500);
    }
    
    public void draw() {
    
      if (gameScreen==gameScreenInit ) {
        initScreen();
      } else if (gameScreen==gameScreenStartScreen ) {
        gameStartScreen();
      } else if (gameScreen==gameScreenGameOverScreen ) {
        gameOverScreen();
      } else if (gameScreen==gameScreenQuizScreen ) {
        quizScreen();
      } else if (gameScreen==gameScreenCorrectScreen) {
        gameScreenCorrectAnswer();
      } else if (gameScreen==gameScreenFalseScreen) {
        gameScreenFalseAnswer();
      }
      //print("K "+correctAnswer+" F "+falseAnswer);
    }
    
    
    public void initScreen() {
    
      background(236, 240, 241);
    
      textAlign(CENTER);
      fill(52, 73, 94);
      textSize(70);
      text("Ma Game", width/2, height/2);
      textSize(25); 
      text("Click to start", width/2, height-30);
    }
    
    
    public void gameOverScreen() {
      background(44, 62, 80);
      textAlign(CENTER);
      fill(236, 240, 241);
      textSize(70);
      text("GAME OVER", width/2, height/2);
      textSize(25);
      text("Click to Restart", width/2, height-30);
    }
    
    public void gameStartScreen() {
      s.createBackground();
    
      //create Shapefigur
      //  background(173, 244, 66);
      fill(244, 232, 66);
      ellipse(x, y, radius, radius);
      fill(255, 255);
      ellipse(x-5, y-10, 5, 5);
      ellipse(x+5, y-10, 5, 5);
      rect(x-10, y, 20, 3);
      //move and create Ball
    
      //create rects on display
    
      //s.createRect(100, 500);
    
    
      // s.createRect(800, 500);
      s.createClouds();
      s.createSun();
      s.createGround();
    
      //timer
      startTimer();
      ballTimer();
    
      //figur entering rects?
      enterRect();
    
      //addBalls();
    }
    
    public void quizScreen() {
    
      question=questions[0];
      question.display();
    
    }
    
    public void gameScreenCorrectAnswer() {
    
      // background(124, 252, 0);
      background(173, 244, 66);
      textAlign(CENTER);
      textSize(70);
      text("Correct Answer", width/2, height/2);
      textSize(25); 
      text("Click to resume the game", width/2, height-30);
    }
    
    public void gameScreenFalseAnswer() {
      background(200, 150, 0);
      textAlign(CENTER);
      textSize(70);
      text("False Answer", width/2, height/2);
      textSize(25); 
      text("Start new game", width/2, height-30);
    }
    
    public void addSmallBalls() {
    
      for (int i=0; i<5; i++) {
        smallBalls.add(new Ball(random(10, 20), random(18, 25), random(18, 20)));
      }
    }
    
    public void addBigBalls() {
      for (int i=0; i<5; i++) {
        bigBalls.add(new Ball(random(30, 50), random(8, 15), random(8, 15)));
      }
    }
    
    public void ballTimer() {
    
      bigBalls.get(0).move();
      bigBalls.get(0).display();
    
    
      if ((time/1000)>10) {
        smallBalls.get(0).move();
        smallBalls.get(0).display();
      }
    
      if ((time/1000)>20) {
        bigBalls.get(1).move();
        bigBalls.get(1).display();
      }
      if ((time/1000)>30) {
        smallBalls.get(1).move();
        smallBalls.get(1).display();
      }
    }
    
    // This method sets the necessary variables to start the game  
    public void startGame() {
      gameScreen=1;
    }
    public void gameOver() {
      gameScreen=2;
    }
    public void startQuiz() {
      gameScreen=3;
    }
    public void falseAnswer() {
      gameScreen=4;
    }
    public void correctAnswer() {
      gameScreen=5;
    }
    
    public void restart() {
      time=0;
      gameScreen=1;
    
    }
    
    public void mousePressed() {
    
      // if we are on the initial screen when clicked, start the game 
      if (gameScreen==0) { 
        startGame();
      } else if (gameScreen==1) {
        startGame();
      } else if (gameScreen==2) {
        restart();
      } else if (gameScreen==3) {
        startQuiz();
      } else if (gameScreen==4) {
        startGame();
      } else if (gameScreen==5) {
        restart();
      }
    }
    
    public void mouseReleased() {
      if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) {
        gameScreen=5;
      } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) {
        gameScreen=5;
      } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) {
        gameScreen=5;
      } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) {
        gameScreen=4;
      }
    }
    
    
    public void keyPressed() {
    
      if (key == CODED) {
        if (keyCode == UP && y>radius) {
          y -= speed;
        } else if (keyCode == DOWN && y<height-radius) {
          y += speed;
        } else if (keyCode == LEFT && x>radius) {
          x -= speed;
        } else if (keyCode == RIGHT && x< width-radius) {
          x += speed;
        }
      }
    }
    
    
    public void startTimer() {
    
      if (startTimer==true) {
        time= millis();
        textSize(20);
        fill(0, 0, 0);
        text(("Time: "+time/1000), 1100, 30);
      }
    }
    
    
    
    public void enterRect() {
    
      //not finished
      if (x>=85 && x<=245 && y>=60 && y<=135 ) {
        startQuiz();
      }
    }
    
  • edited December 2017

    I don't know, I can't run the code

    Does it work? What happens falsely, what do you want to happen instead?

  • not sure what you want here,

    but shouldn't you use

     if (gameScreen==0) { 
        .....
      } else if (gameScreen==1) {
        ........
    

    in this:

    public void mouseReleased() {
      if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) {
        gameScreen=5;
      } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) {
        gameScreen=5;
      } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) {
        gameScreen=5;
      } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) {
        gameScreen=4;
      }
    }
    
  • There two problems when i run the code, first when i start the code and click on the false/correct space i get false/correct display without even entering the coordinates defined in enterRect() method. and second when i enter the defined space in enterRect() i just have the same problem as before a loop between quiz and false/correct answer

  • as I said, this is unnecessary

      question=questions[0];
      question.display();
    

    just say

             questions[0].display();
    
  • not sure what you want here,

    but shouldn't you use 1 2 3 4
    if (gameScreen==0) { ..... } else if (gameScreen==1) { ........

    in this:

    public void mouseReleased() { if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) { gameScreen=5; } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) { gameScreen=5; } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) { gameScreen=5; } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) { gameScreen=4; } }

    that solved my first problem but now when quiz display starts and i click on one of the answers nothing happens. and i think the problem is in one of these two methods below changed version: public void mousePressed() {

          // if we are on the initial screen when clicked, start the game 
          if (gameScreen==0) { 
            startGame();
          } else if (gameScreen==1) {
            startGame();
          } else if (gameScreen==2) {
            restart();
          } else if (gameScreen==3) {
            startQuiz();
          } 
        }
    
        public void mouseReleased() {
          if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) {
            restart();
          } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) {
            restart();
          } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) {
            restart();
          } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) {
           startGame();
          }
        }
    
  • What is the job of mouseReleased?

  • edited December 2017
    public void mouseReleased() {
      if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) {
        restart();**//False Answer when u click on this space u get redirected to falseAnswer Display**
      } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) {
        restart();**//falseAnswer happens the same **
      } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) {
        restart();**//falseAnswer happens the same **
      } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) {
       startGame();**//correctAnswer when u click on this space u get redirected to correctAnswer display**
      }
    }
    

    So all in one mouseReleased determine if u click on the correct or false Answer.

  • edited December 2017

    Sorry, but I suggested to test the gameScreen in mouseReleased()

    then you said:

    that solved my first problem

    then you post the old code of mouseReleased() ???

    Did you change it?

    What is the job of mouseReleased?

    mouseReleased is only appropriate when if (gameScreen==gameScreenQuizScreen ), right ?

        public void mouseReleased() {
    
    
            if (gameScreen==gameScreenQuizScreen ) {
    
    
    
              if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) {
                restart();
              } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) {
                restart();
              } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) {
                restart();
              } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) {
               startGame();
              }
    
    
            }
    
    
        }
    
  • edited December 2017

    The functions restart() and startGame() are wrong

    they must be

    gameScreen=gameScreenCorrectScreen

    OR

    gameScreen=gameScreenFalseScreen

    !!!!!

  • Sorry, but I suggest to test the gameScreen in mouseReleased() {

    then you said: that solved my first problem

    then you post the old code of mouseReleased() ???

    Did you change it?

    What is the job of mouseReleased?

    mouseReleased is only appropriate when if (gameScreen==gameScreenQuizScreen ) {, right ?

    yeah as u said i changed mouseReleased and its only appropriate when my gameScreen ist Quizscreen a u said.but now when i click iam in quizdisplay and click on any of the four answers nothing happens :/

  • The functions restart() and startGame() are wrong

    they must be

    gameScreen=gameScreenCorrectScreen

    OR

    gameScreen=gameScreenFalseScreen

    !!!!!

  • edited December 2017

    you can make a zip file from your entire project in the menu tools

    please send it to me via e-mail

    I PM you my email

  • edited December 2017

    The functions restart() and startGame() are wrong

    they must be

    gameScreen=gameScreenCorrectScreen

    OR

    gameScreen=gameScreenFalseScreen

    !!!!!

    thank u alot that solved my second problem i just have another question for example when i click corrctanswer i get redirect to correctAnswerscreen as decleared in mouseReleased() but how can i be redirected from that screen to the game again?

    you can make a zip file from your entire project in the menu tools
    
    please send it to me via e-mail
    
    I PM you my email
    

    thank u alot for ur effort. iam gonna send u but we solved already most of the problems thanks to u there is only one above.

  • there is only one above.

    I bet there are more to come...

    when i click corrctanswer i get redirect to correctAnswerscreen as decleared in mouseReleased() but how can i be redirected from that screen to the game again?

    Are you serious?

    What about mousePressed()?

  • Are you serious?

    What about mousePressed()?

    yeah i modified mousePressed but when i click either correct/or false display after answering my quiz i get redirect to quiz display again

    public void mousePressed() {

      // if we are on the initial screen when clicked, start the game 
      if (gameScreen==0) { 
        startGame();
      } else if (gameScreen==1) {
        startGame();
      } else if (gameScreen==2) {
        restart();
      } else if (gameScreen==3) {
        startQuiz();
      } else if(gameScreen==4){
      startGame();
    }   else if(gameScreen==5){
       startGame(); 
    }
    
    }
    
  • ??

    you need to check the functions:

    is startGame(); doing what you expect?

  • ??

    you need to check the functions:

    is startGame(); doing what you expect?

    yeah it changes the value of gameScreen to 1 .

    public void startGame() {
      gameScreen=1;
    }
    
  • aber jetzt geht es oder?

  • Answer ✓

    Solved.

Sign In or Register to comment.