Generating Random Platforms

edited January 2016 in How To...

Hi! For this question's purpose, let's say I want to create platforms similar to Doodle Jump, using the rect function. How would I make the terrain so that it would randomly generate patterns of platforms?

Tagged:

Answers

  • also with different lengths and widths?

  • edited August 2014 Answer ✓

    Depending from your skill level:

    Make an ArrayList of class Platform - call it platforms

    Fill it with random values for x y w and h

    Using random () - see reference

    You might also want to use a viewport or a map for the platforms

    Then loop over the platforms and draw them

    for (Platform p1 : platforms) {
    
        p1.draw();
    
    }
    

    Chrisir

  • edited August 2014

    this is the naiv approach where the platforms exist

    but you can not check whether you are still ON a platform because you don't save the platforms and their positions...

    but this covers what you've asked so far...

    Best wishes, Chrisir ;-)

    ;-)

    void setup() {
      size(700, 700);
    }
    
    void draw() {
      for (int i=0; i<8; i++) {
        // 
        fill(random(0, 255), random(0, 255), random(0, 255));
        rect(random(0, width), random(110, height-60), 
        random(20, 130), random(20, 60));
      } // for
      noLoop();
    } // func 
    //
    
  • ...and don't accept an answer too soon.

    The thread looks solved then....

  • Nah, it's fine. I figured it out. Thanks :)

  • Is that homework of some sort?

  • Nop, personal project.

  • here is an example....

    ArrayList<Platform> platforms = new ArrayList();
    ArrayList<Ball> balls = new ArrayList();
    
    void setup() {
      size(700, 700);
    
      for (int i=0; i<18; i++) {
        // 
        // we don't give x-value, but i
        // the other's are per random
        Platform newPF = new Platform( 
        i, 
        random(210, height-60), 
        random(20, 90), random(20, 60));
    
        platforms.add ( newPF );
      } // for
      background(111);
    }
    
    void draw() {
      //
      background(111);
      fill(255);
      text("Please click mouse to drop a circle. This shows the usage of random platforms.", 12, 12);
    
      // for-loop
      for (int i = balls.size()-1; i >= 0; i--) { 
        Ball ball = balls.get(i);
        ball.moveThis();
        ball.display();
      } // for 
      for (Platform p1 : platforms) {
        p1.display();
      }
      //
    } // func
    
    void mousePressed() {
    
      int xPos = 10;
      int yPos = 0;
      int rectWidth = 30; //  width-20;
      int rectHeight = 30 ;  // 120;
      int beginPosition = 0;
      int stopPosition = 150;
      int mouseVar;
      float rCol, gCol, bCol;
    
      rCol = random(255);
      gCol = random(255);
      bCol = random(255);
    
      balls.add(new Ball(mouseX, mouseY, 
      rectWidth, rectHeight, 
      beginPosition, stopPosition, 
      rCol, gCol, bCol));
    }
    
    // =============================================
    
    class Platform {
    
      float x;
      float y;
      float w;
      float h; 
    
      // constr
      Platform(int i, float y, float w, float h) {
        // we want to pin the next plaltform next
        // to the previous one
        float xTemp;
        if (i==0) {
          // 1st goes to the left screen border
          xTemp=random(0, 0);
        }
        else {
          // i is 1 or 2 or 3....
          // the others are next to the previous platform
          Platform tempPlatform = platforms.get(i-1); 
          float formerX = tempPlatform.x;
          float formerW = tempPlatform.w;
          xTemp = random(formerX+formerW-6, formerX+formerW+6);
        }
        this.x=xTemp;
        this.y=y;
        this.w=w;
        this.h=h;
      }
    
      void display() {
        fill(255);
        rect(x, y, w, h);
      }
    } // class 
    
    //
    
    // ==================================================
    
    class Ball {  
      // pos
      float x;
      float y;
      // size
      float w;
      float h;
      // movement 
      float beginPos;
      float stopPos;
      float speed = 5;
      // color // or use color 
      float r, g, b;
    
      Ball(float tempX, float tempY, 
      float tempW, float tempH, 
      float tempBegin, float tempStop, 
      float tempR, float tempG, float tempB) {
        x = tempX;
        y = tempY;
        w = tempW;
        h = tempH;
        beginPos = tempBegin;
        stopPos = tempStop;
        r = tempR;
        g = tempG;
        b = tempB;
      }
    
      void moveThis() {
        for (Platform p1 : platforms) {
          // p1.display();
          if (x>p1.x&&x<p1.x+p1.w) {
            if (y+h/2+speed>=p1.y) {
              speed=0;
              y=p1.y-h/2;
            }
          }
        }
        y = y + speed;
      }
    
      void display() {
        fill(r, g, b);
        ellipse(x, y, w, h);
      }
      //
    } // class
    
Sign In or Register to comment.