Help resetting game

Really need some help with an IF statement that would restart this simple ball game after the ball leaves the screen.

float x = 200;
float y = 200;
float spdX = random (3, 5); 
float spdY = random (3, 5); 
float r = random (20, 255); 
float g = random (20, 255); 
float b = random (20, 255); 
int sky; 
int chop = 200; 
float BOP; 
boolean animation =false; 



void setup() { 
  size (1250, 500); 

  smooth(); 
}

void draw() { 
  background(0, 0, 0);
  sky=20; 
  ellipse(x, y, sky, sky); 

  fill(r, g, b); 
  rect(0, 0, 20, height); 
  fill(r, g, b); 
  rect(width-30, mouseY-chop/2, 10, chop);


  if (animation) { 

    x = x + spdX; 
    y = y + spdY; 

    if (x>width-30 && x<width -20 && y<mouseY+BOP && y>mouseY-chop/2) {
      spdX=spdX*-1; 
      x=x +spdX;

      BOP =random(60, 100); 
      chop=chop-20; 
      chop= constrain(chop, 10, 150);
    } else if (x<25) { 
      spdX = spdX * -1.1;
      x = x + spdX;

      if (x > width-2) { //THIS IS THE RESTART GAME IF STATEMENT THAT ISN'T WORKING!!!
        animation = false;
        x = 150;
        y = 150; 
        spdX = random(3, 5);
        spdY = random(3, 5);

         }

      if ( y > height || y < 0 ) {
        spdY = spdY * -1;
        y = y + spdY;
      }
    }
  }

  if (y+spdY>height||y<0) {
    spdY*=-1;
    r =random(0, 255);
    g =random(0, 255);
    b =random(0, 255);
  }
}
void mousePressed () { 
  animation=true;
}
Tagged:

Answers

  • Your width is set to 1250. Is that right? Also could you post the entire code?

  • +Alex_Pr Yeah its the same width as my mac and sorry should be able to see whole code now

  • Answer ✓

    Its very simple really. You have embeded this if statement inside another one which prevents the second from being true. Look at line 44. The value of x is <25 and so is not possible for the embeded if statement to become true, in other words, if x<25 then x>width-2 is always wrong (width = 1250)

  • i'm such an idiot...ah Thank you soon much!!!!!!

Sign In or Register to comment.