# How to have all platforms stay?

**URL:** <https://discourse.processing.org/t/how-to-have-all-platforms-stay/32610>\
**Category:** Coding Questions\
**Created:** [October 4, 2021, 3:56pm UTC](https://discourse.processing.org/t/how-to-have-all-platforms-stay/32610 "2021-10-04T15:56:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [October 4, 2021, 3:56pm UTC](https://discourse.processing.org/t/how-to-have-all-platforms-stay/32610/1 "2021-10-04T15:56:25Z")

</div>

how to have all of the platforms stay visible?

```auto
//visual
PImage shrek;
boolean invert= false;
PImage[] soldier= new PImage[3];
platform platform;
//timer

//moving
float charx=0;
float chary=563-100;
boolean jumping= false;
float gravity= 10;
float jumpingheight=100;
float jumpspeed;
int i=1;

void setup() {
  size(1000, 563);
  shrek= loadImage("shrek.jpg");
    soldier[1]= loadImage("soldier.png");
  soldier[2]= loadImage("revsoldier.png");
}

void draw() {
  platform= new platform(random(10,200),random(10,200));
  frameRate(60);
  //visual
  background(shrek);
  color(1005);
  
  platform.display();
  
  image(soldier[i], charx, chary, 100, 100);

  //timer
  println(millis()/1000);
  if (millis()/1000%300==0&&millis()/1000>10&& invert==false) {
    invert=true;
  }
  if (millis()/1000%600==0&&millis()/1000>10&& invert==true) {
    invert=false;
  }
  if (invert==true) {
    filter(INVERT);
  }

  //moving
  if (charx>=width-100) {
    charx=width-100;
  }
  
  jumpspeed= int(dist(0, jumpingheight, 0, chary)/20);

  if (chary>height-100) {
    chary=height-100;
  }
  if (keyPressed) {

    //move left with a
    if (key == 'a'&&invert==false) {
      charx=charx-10;
      i=2;
    } 

    //move right with d
    if (key == 'd'&&invert==false) {
      charx=charx+10;
      i=1;
    }

    //move right with a
    if (key == 'a'&&invert==true) {
      charx=charx+10;
      i=1;
    } 

    //move left with d
    if (key == 'd'&&invert==true) {
      charx=charx-10;
      i=2;
    }
    //jump with space
    if (key == ' '&&chary>height-jumpingheight) {
      jumping=true;
    }
  }

  if (charx<=0+100) {
    charx= 0+100;
  }
  if (jumping==true) {
    chary=int(chary-jumpspeed);
    charx=charx+0.1;
    if (jumpspeed<10&&chary<height-30) {
      jumping=false;
    }
  }
  if (chary!=height-100&&jumping==false) {
    chary+=gravity;
  }
  println(chary);
}

```

```auto
class platform{
  float x,y,w,h;
  
  platform(float xp, float yp){
    x= xp;
    y= yp;
    w=200;
    h=20;}
    void display(){
      fill(255,0,0);
      rect(x,y,w,h);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 4, 2021, 9:29pm UTC](https://discourse.processing.org/t/how-to-have-all-platforms-stay/32610/2 "2021-10-04T21:29:05Z")

</div>

it’s easier when you define it in setup()

and in draw() just display the platforms

```auto

//visual
PImage shrek;
boolean invert= false;
PImage[] soldier= new PImage[3];
Platform platform;

//moving
float charx=0;
float chary=563-100;
boolean jumping= false;
float gravity= 10;
float jumpingheight=100;
float jumpspeed;
int i=1;

//---------------------------------------------------------------------------------------

void setup() {
  size(1000, 563);
  shrek= loadImage("shrek.jpg");
  soldier[1]= loadImage("soldier.png");
  soldier[2]= loadImage("revsoldier.png");

  platform= new Platform(random(10, 200), random(10, 200));
}

void draw() {

  frameRate(60);
  //visual
  background(0);
  color(1005);

  platform.display();

  text("soldier[i]", 
    charx, chary, 100, 100);

  //timer
  println(millis()/1000);
  if (millis()/1000%300==0&&millis()/1000>10&& invert==false) {
    invert=true;
  }
  if (millis()/1000%600==0&&millis()/1000>10&& invert==true) {
    invert=false;
  }
  if (invert==true) {
    filter(INVERT);
  }

  //moving
  if (charx>=width-100) {
    charx=width-100;
  }

  jumpspeed= int(dist(0, jumpingheight, 0, chary)/20);

  if (chary>height-100) {
    chary=height-100;
  }
  if (keyPressed) {

    //move left with a
    if (key == 'a'&&invert==false) {
      charx=charx-10;
      i=2;
    } 

    //move right with d
    if (key == 'd'&&invert==false) {
      charx=charx+10;
      i=1;
    }

    //move right with a
    if (key == 'a'&&invert==true) {
      charx=charx+10;
      i=1;
    } 

    //move left with d
    if (key == 'd'&&invert==true) {
      charx=charx-10;
      i=2;
    }
    //jump with space
    if (key == ' '&&chary>height-jumpingheight) {
      jumping=true;
    }
  }

  if (charx<=0+100) {
    charx= 0+100;
  }
  if (jumping==true) {
    chary=int(chary-jumpspeed);
    charx=charx+0.1;
    if (jumpspeed<10&&chary<height-30) {
      jumping=false;
    }
  }
  if (chary!=height-100&&jumping==false) {
    chary+=gravity;
  }
  println(chary);
}

//======================================================

class Platform {
  float x, y, w, h;

  Platform(float xp, float yp) {
    x= xp;
    y= yp;
    w=200;
    h=20;
  }

  void display() {
    fill(255, 0, 0);
    rect(x, y, w, h);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 4, 2021, 9:33pm UTC](https://discourse.processing.org/t/how-to-have-all-platforms-stay/32610/3 "2021-10-04T21:33:47Z")

</div>

multiple platforms

```auto

//visual
PImage shrek;
boolean invert= false;
PImage[] soldier= new PImage[3];

ArrayList<Platform> platforms = new ArrayList();

//timer

//moving
float charx=0;
float chary=563-100;
boolean jumping= false;
float gravity= 10;
float jumpingheight=100;
float jumpspeed;
int i=1;

//---------------------------------------------------------------------------------------

void setup() {
  size(1000, 563);
  shrek= loadImage("shrek.jpg");
  soldier[1]= loadImage("soldier.png");
  soldier[2]= loadImage("revsoldier.png");

  Platform platform;
  for (int i=0; i<4; i++) {
    platform = new Platform(random(10, 200), random(10, 200));
    platforms.add(platform);
  }
}

void draw() {

  frameRate(60);
  //visual
  background(0);
  color(1005);

  for (Platform platform : platforms)
    platform.display();

  text("soldier[i]", 
    charx, chary, 100, 100);

  //timer
  println(millis()/1000);
  if (millis()/1000%300==0&&millis()/1000>10&& invert==false) {
    invert=true;
  }
  if (millis()/1000%600==0&&millis()/1000>10&& invert==true) {
    invert=false;
  }
  if (invert==true) {
    filter(INVERT);
  }

  //moving
  if (charx>=width-100) {
    charx=width-100;
  }

  jumpspeed= int(dist(0, jumpingheight, 0, chary)/20);

  if (chary>height-100) {
    chary=height-100;
  }
  if (keyPressed) {

    //move left with a
    if (key == 'a'&&invert==false) {
      charx=charx-10;
      i=2;
    } 

    //move right with d
    if (key == 'd'&&invert==false) {
      charx=charx+10;
      i=1;
    }

    //move right with a
    if (key == 'a'&&invert==true) {
      charx=charx+10;
      i=1;
    } 

    //move left with d
    if (key == 'd'&&invert==true) {
      charx=charx-10;
      i=2;
    }
    //jump with space
    if (key == ' '&&chary>height-jumpingheight) {
      jumping=true;
    }
  }

  if (charx<=0+100) {
    charx= 0+100;
  }
  if (jumping==true) {
    chary=int(chary-jumpspeed);
    charx=charx+0.1;
    if (jumpspeed<10&&chary<height-30) {
      jumping=false;
    }
  }
  if (chary!=height-100&&jumping==false) {
    chary+=gravity;
  }
  println(chary);
}

//======================================================

class Platform {
  float x, y, w, h;

  Platform(float xp, float yp) {
    x= xp;
    y= yp;
    w=200;
    h=20;
  }

  void display() {
    fill(255, 0, 0);
    rect(x, y, w, h);
  }
}

```

* * *

More distance between platforms, still a bit random in y-direction

```auto

//visual
PImage shrek;
boolean invert= false;
PImage[] soldier= new PImage[3];

ArrayList<Platform> platforms = new ArrayList();

//moving
float charx=0;
float chary=563-100;
boolean jumping= false;
float gravity= 10;
float jumpingheight=100;
float jumpspeed;
int i=1;

//---------------------------------------------------------------------------------------

void setup() {
  size(1000, 563);

  shrek= loadImage("shrek.jpg");
  soldier[1]= loadImage("soldier.png");
  soldier[2]= loadImage("revsoldier.png");

  float prevYValue=30; 
  Platform platform;
  for (int i=0; i<4; i++) {
    float yValueForRandom=random(prevYValue+80, prevYValue+170);
    prevYValue = yValueForRandom; 

    platform = new Platform(random(10, 400), yValueForRandom);
    platforms.add(platform);
  }
}//

void draw() {

  frameRate(60);
  //visual
  background(0);
  color(1005);

  for (Platform platform : platforms)
    platform.display();

  text("soldier[i]", 
    charx, chary, 100, 100);

  //timer
  println(millis()/1000);
  if (millis()/1000%300==0&&millis()/1000>10&& invert==false) {
    invert=true;
  }
  if (millis()/1000%600==0&&millis()/1000>10&& invert==true) {
    invert=false;
  }
  if (invert==true) {
    filter(INVERT);
  }

  //moving
  if (charx>=width-100) {
    charx=width-100;
  }

  jumpspeed= int(dist(0, jumpingheight, 0, chary)/20);

  if (chary>height-100) {
    chary=height-100;
  }
  if (keyPressed) {

    //move left with a
    if (key == 'a'&&invert==false) {
      charx=charx-10;
      i=2;
    } 

    //move right with d
    if (key == 'd'&&invert==false) {
      charx=charx+10;
      i=1;
    }

    //move right with a
    if (key == 'a'&&invert==true) {
      charx=charx+10;
      i=1;
    } 

    //move left with d
    if (key == 'd'&&invert==true) {
      charx=charx-10;
      i=2;
    }
    //jump with space
    if (key == ' '&&chary>height-jumpingheight) {
      jumping=true;
    }
  }

  if (charx<=0+100) {
    charx= 0+100;
  }
  if (jumping==true) {
    chary=int(chary-jumpspeed);
    charx=charx+0.1;
    if (jumpspeed<10&&chary<height-30) {
      jumping=false;
    }
  }
  if (chary!=height-100&&jumping==false) {
    chary+=gravity;
  }
  println(chary);
}

//======================================================

class Platform {
  float x, y, 
    w, h;

  Platform(float xp, float yp) {
    x= xp;
    y= yp;
    w=200;
    h=20;
  }

  void display() {
    fill(255, 0, 0);
    rect(x, y, w, h);
  }
}//
//

```
