How to have all platforms stay?

how to have all of the platforms stay visible?

//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);
}
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);
    }
}
1 Like

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

and in draw() just display the platforms


//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);
  }
}

multiple platforms





//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


//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);
  }
}//
//