How to make the game end?

edited December 2016 in Library Questions

Once the white ellipse reaches the green ellipse i would like to make a message come across the screen that says "Congratulations you won!", however, I have been looking at other references but I can't seem to find a way to have the game end with the white ellipse colliding with the green ellipse.

Tagged:

Answers

  • edited December 2016
    ``//Make it to the end of the maze without hitting any of the maze walls
    int radius = 15, directionX = 1, directionY = 0;
    float x=20, y=20, speed=4;
    
    import ddf.minim.*;
    Minim minim;
    AudioPlayer player1;
    AudioPlayer player2;
    AudioInput input;
    
    void setup()
    {
      size(750, 600);
      smooth();
      noStroke();
      ellipseMode(RADIUS); 
    
        minim = new Minim(this);
      player1 = minim.loadFile("Congrats.wav");
       minim = new Minim(this);
      player1.loop();
     //player2 =minim.loadFile("Error.wav");
    
    
    }
    
    void reset(){
      x=20; y=20;
    }
    
    void draw()
    {
      background(0);
      //draws the finish spot in green
      fill(0, 255, 0);
      ellipse(515, 565, 25, 25);
      // draws a barrier in red 
      fill(255, 0, 0);
      rect(200, 120, 130, 10);
      rect(400, 90, 10, 400);
      rect(200, 40, 10, 90);
      rect(0, 40, 200, 10);
      rect(275, 40, 500, 10);
      rect(325, 90, 10, 40);
      rect(475, 40, 10, 100);
      rect(60, 180, 200, 10);
      rect(325, 180, 10, 150);
      rect(550, 120, 10, 270);
      rect(100, 400, 230, 10);
      rect(200, 270, 10, 130);
      rect(260, 325, 75, 10);
      rect(475, 210, 10, 100);
      rect(400, 300, 80, 10);
      rect(475, 380, 10, 220);
      rect(60, 260, 150, 10);
      rect(0, 330, 90, 10);
      rect(0, 100, 120, 10);
      rect(70, 480, 340, 10);
      rect(70, 550, 410, 10);
      rect(550, 450, 200, 10);
      rect(475, 520, 200, 10);
      rect(475, 380, 200, 10);
      rect(550, 120, 120, 10);
      rect(620, 200, 140, 10);
      rect(670, 310, 10, 80);
      rect(620, 310, 60, 10);
      rect(620, 270, 10, 50);
    
    
    
      // calculate next position of x and y to know when it will hit a wall
      int nextX = int(x+speed*directionX);
      int nextY = int(y+speed*directionY); 
    
      // check if next position has a bright color(red) to show that the ellipse hit a wall
      if (brightness(get(nextX, nextY)) > 200) {
        // hit a barrier -> go back to beginning
        x=20; y=20;
        directionX=1; directionY=0;
      } else {
        // no barrier -> change Position
        x=nextX;
        y=nextY;
      }
    
    
      // check boundaries
      if ((x>width-radius) || (x<radius)) 
    
      {   
        directionX=-directionX;
      }
      if ((y>height-radius) || (y<radius)) 
    
      {   
        directionY=-directionY;
      } 
    
      fill (color(255, 255, 255)); 
      ellipse (x, y, radius, radius);   
      fill (color(22, 82, 22));
    }
    
    
    void keyPressed()
    {
      if (key == CODED)
      {
        if (keyCode == LEFT)
        {
          //if (directionX>0) { 
          directionX=-1;
          directionY=0;
          //}
        }
        else if (keyCode == RIGHT)
        {
          //if (directionX<0) {  
          directionX=1;
          directionY=0;
          //}
        }
        else if (keyCode == UP)
        {
          //if (directionY<0) {
          directionY=-1;
          directionX=0;
          //}
        }
        else if (keyCode == DOWN)
        {
          //if (directionY<0) { 
          directionY=1;
          directionX=0;
          //}
    
        }
      }
    }
    
  • Add a boolean that tracks if you have won. Since you won't have won when the game starts, it should initially be set to false. When your green and white circles intersects, you have won, so set the boolean to true.

    Now that you have a boolean that tracks if you have won, you can use it in conditional statements. For example, instead of drawing and simulating the entire game in draw(), you can now first check to see if you have won. If you have, then you can draw a screen that congratulates the player. If you haven't, draw the game and simulate it as before.

  • This is the same as your earlier problem with wall collisions. Remember how you needed to do something inside the 'if' statement that identified the collision? Again, find the place in your code that checks if the white and green balls are touching. Add the code there as per TfGuy44 's suggestion above.

  • @jeremydouglass the section in my code where the collision occurs between the green and white ellipse is also the same section where the white ellipse collides with the red barriers, do I need to change this if I want the game to only end when the white ellipse hits the green ellipse?

  • boolean won;
    boolean lost;
    
    void setup(){
      size(400,400);
      textAlign(CENTER,CENTER);
      won = false;
      lost = false;
    }
    
    void draw(){
      if( won ){
        background(255);
        fill(0);
        text("You won.", 200,200);
      } else if ( lost ){
        background(64,0,0);
        fill(255,0,128);
        text("You lost.", 200, 200);
      } else {
        background(0,196,0);
        fill(0);
        text("Press a key to win. Click to lose.", 200, 200);
      }
    }
    
    void keyPressed(){
      if( !won && !lost ){
        won = true;
      }
    }
    
    void mousePressed(){
      if( !won && !lost ){
        lost = true;
      }
    }
    
  • @TFGuy44 okay I see what you did in your code and I started to try and do this in my own code but I can't seem to come up with a conditional statement to use in my code in relation to the green and white ellipse. Could you possibly help me with this? I am very sorry for asking so many questions, I just don't have any coding experience and I just can't seem to figure this out. I can post what I have tried doing with my code so far.

  • edited December 2016
    ``boolean won;
    boolean lost;
    
    //Make it to the end of the maze without hitting any of the maze walls
    int radius = 15, directionX = 1, directionY = 0;
    float x=20, y=20, speed=4;
    
    
    import ddf.minim.*;
    Minim minim;
    AudioPlayer player1;
    AudioPlayer player2;
    AudioInput input;
    
    void setup()
    {
      size(750, 600);
      smooth();
      noStroke();
      ellipseMode(RADIUS); 
    
        minim = new Minim(this);
      player1 = minim.loadFile("Congrats.wav");
       minim = new Minim(this);
      player1.loop();
     //player2 =minim.loadFile("Error.wav");
    
      textAlign(CENTER,CENTER);
      won = false;
      lost = false;
    }
    
    void draw()
      if(won){
      background(255, 200, 250);
      fill(0);
      text("You won.", 200,200);
    } else {
    
      background(0);
      // draws a barrier in red 
      fill(255, 0, 0);
      rect(200, 120, 130, 10);
      rect(400, 90, 10, 400);
      rect(200, 40, 10, 90);
      rect(0, 40, 200, 10);
      rect(275, 40, 500, 10);
      rect(325, 90, 10, 40);
      rect(475, 40, 10, 100);
      rect(60, 180, 200, 10);
      rect(325, 180, 10, 150);
      rect(550, 120, 10, 270);
      rect(100, 400, 230, 10);
      rect(200, 270, 10, 130);
      rect(260, 325, 75, 10);
      rect(475, 210, 10, 100);
      rect(400, 300, 80, 10);
      rect(475, 380, 10, 220);
      rect(60, 260, 150, 10);
      rect(0, 330, 90, 10);
      rect(0, 100, 120, 10);
      rect(70, 480, 340, 10);
      rect(70, 550, 410, 10);
      rect(550, 450, 200, 10);
      rect(475, 520, 200, 10);
      rect(475, 380, 200, 10);
      rect(550, 120, 120, 10);
      rect(620, 200, 140, 10);
      rect(670, 310, 10, 80);
      rect(620, 310, 60, 10);
      rect(620, 270, 10, 50);
      //draws the finish spot in green
      fill(0, 255, 0);
      ellipse(515, 565, 25, 25);
    
    
    
      // finds the next position of x and y to know when it will hit a wall
      int nextX = int(x+speed*directionX);
      int nextY = int(y+speed*directionY); 
    
      // check if next position has a bright color(red) to show that the ellipse hit a wall
      if (brightness(get(nextX, nextY)) > 200) {
        // hits a barrier and goes back to beginning
        x=20; y=20;
        directionX=1; directionY=0;
      } else {
        // no barrier continue to go through maze
        x=nextX;
        y=nextY;
      }
    
    
      // check boundaries
      if ((x>width-radius) || (x<radius)) 
    
      {   
        directionX=-directionX;
      }
      if ((y>height-radius) || (y<radius)) 
    
      {   
        directionY=-directionY;
      } 
    
      fill (color(255, 255, 255)); 
      ellipse (x, y, radius, radius);   
      fill (color(22, 82, 22));
    }
    
    
    void keyPressed()
    {
      if (key == CODED)
      {
        if (keyCode == LEFT)
        {
          //if (directionX>0) { 
          directionX=-1;
          directionY=0;
          //}
        }
        else if (keyCode == RIGHT)
        {
          //if (directionX<0) {  
          directionX=1;
          directionY=0;
          //}
        }
        else if (keyCode == UP)
        {
          //if (directionY<0) {
          directionY=-1;
          directionX=0;
          //}
        }
        else if (keyCode == DOWN)
        {
          //if (directionY<0) { 
          directionY=1;
          directionX=0;
          //}
    
        }
      }
    }
    
  • When I start to add other "if" statements to my code, it causes error in my existing "if" statement

  • Look, your green ellipse is draw on line 74:

    ellipse(515, 565, 25, 25);
    

    And your white ellipse is drawn on line 107:

      ellipse (x, y, radius, radius);   
    

    Notice the parameters that you are using. These are the X position, Y position, and diameters of your two circles.

    How can you tell if these two circles are touching? HINT: How far apart will they be?

    What CONDITION must be met for them to be touching?

    If that CONDITION is met, what ACTIONS should you do?

    if( CONDITION ){
      // ACTIONS to do when CONDITION is true.
    }
    
Sign In or Register to comment.