How do I make a ball continuously bounce on one platform?


#1

This is my code. What can I do to make the ball up and down on one platform. Right now when the right key is pressed, it goes straight up.

int y=770;

int back=20;

int dir=20;

int level=3;

void setup(){

size(700,800);

}

void draw(){

if(level==0){

textSize(40);

fill(0);

text("Level 1",100,100);}

delay(30);

background(#02C2F0);

fill(#FF0808);

ellipse(back,y,60,60);

y=y-dir;

{if(y<600||y>770){

dir=-dir;}} //makes the circle bounce before it hits first rectangle

{if(back>55) //makes circle go up on first rectangle

y=y-30;}

{if(back>180)

y=y-90;}

rect(0,780,700,20);

fill(#FFFFFF);

rect(60,740,100,20);

rect(180,680,100,20);

rect(350,600,100,20);

rect(510,545,100,20);

rect(330,460,100,20);

rect(200,400,100,20);

rect(0,340,100,20);

rect(160,280,100,20);

rect(320,220,100,20);

rect(470,160,100,20);

rect(620,100,100,20);

textSize(30);

fill(255,45,100);

text ("finish",570,50);

stroke(#0D0C0C);

textSize(20);

text ("Instructions: Use arrow keys to reach the finish. If you fall off, you DIE", 0,780);

}

void keyPressed(){

if(keyCode==RIGHT) {back=back+40;} //controll for circle

if(keyCode==LEFT) {back=back-40;} //controll for circle

}


#2

@irukandjijelly please format your code in processing with Ctrl+T and edit your post with the code pasted with the </> sign,
Also your placement of the { } signs seem to be off, take a second look at that.


#3

@irukandjijelly I tried something and it seems to work:

float y=770;
int back=20;
int dir=20;

float ySpeed = 5;
float yAcc = 9.81;

int level=3;
int[][] platforms = {  
  {60, 740}, 
  {180, 680}, 
  {350, 600}, 
  {510, 545}, 
  {330, 460}, 
  {200, 400}, 
  {0, 340}, 
  {160, 280}, 
  {320, 220}, 
  {470, 160}, 
  {620, 100} };
int pHeight = 20;
int pWidth = 100;
boolean died = false;

void setup() {
  size(700, 800);
}

void draw() {
  if (!died && level == 3) {
    background(#02C2F0);
    //if (level==0) {
    //  textSize(40);
    //  fill(0);
    //  text("Level 1", 100, 100);
    //}

    for (int frame = 0; frame < 20; frame++) {
      ySpeed -= yAcc * 0.01;
      y -= ySpeed * 0.01;
      if (y+30 >= height && back < 60) {
        ySpeed = 50;
      } else if (y+30 > height && back > 60) {
        text("you died!", width/2, height/2);
        died = true;
      } else if (back+30 > width && y+30 < 100) {
        text("finished this level!", width/2, height/2);
        level++;
      }
      for (int i = 0; i < platforms.length; i++) {
        if ((back > platforms[i][0]) && (back < platforms[i][0]+pWidth) && (y+30 >= platforms[i][1]) && (y+30 <= platforms[i][1]+(pHeight/2))) {
          ySpeed = 50;
          if (i == 5) ySpeed = 45;
        }
      }
    }
    ellipse(back, y, 60, 60);

    fill(#FFFFFF);
    for (int i = 0; i < platforms.length; i++) {
      rect(platforms[i][0], platforms[i][1], pWidth, pHeight);
    }
    fill(#FF0808);
    rect(60, 780, 700, 20);

    textSize(30);
    fill(255, 45, 100);
    text ("finish", 570, 50);
    stroke(#0D0C0C);
    textSize(20);
    text ("Instructions: Use arrow keys to reach the finish. If you fall off, you DIE", 0, 780);
    fill(#FF0808);
  }
}

void keyPressed() {
  if (key == ' ') {
    died = false;
    back = 20;
  }
  if (keyCode==RIGHT) {
    back=back+40;
  } //controll for circle

  if (keyCode==LEFT) {
    back=back-40;
  } //controll for circle
}

as you can see i stored all x and y values of the platforms in an array to draw them all in a for-loop instead of seperate lines.
i also use a more physics based bounce, with actual acceleration downwards and only if it hits a platform will it gain speed upwards again.
If you land inside the red area on the bottom you die, which it will say and you can start again by pressing spacebar.
if you reach the end and jump out of the screen you have finished this level and it will count to the next one.
be sure to read all the code and fully understand it, if you have any questions, please ask, or try it out yourself :slight_smile: