How to Generate 2D movement on a game(left right jump) and platforms?

edited June 2017 in How To...

Hi, I am designing Donkey Kong style game, I have the menu but I am having no succes on creating the movement and the boundaries/platforms, any ideas?

Answers

  • Do you have a player? Does your player have a position? How does the position change? How does what changes the position change? Do you have any code to show us, so we aren't starting from scratch we we try to help you?

  • I have nothing from the game, all the menus i need to know where can i find how to write the programing to create movement an then to create boundaries, it can be used as a box, i will then change it to an image

  • Answer ✓

    Do you have a player?

    class Player {
      Player(){}
    }
    
    Player player;
    
    void setup(){
      size(600,400);
      player = new Player();
    }
    
    void draw(){
      background(255);
    }
    

    Does your player have a position?

    class Player {
     float px, py;
     Player(){
      px = random(width-20);
      py = random(height-20);
     }
     void draw(){
       stroke(0);
       fill(0,0,200);
       rect(px,py,20,20);
     }
    }
    
    Player player;
    
    void setup(){
      size(600,400);
      player = new Player();
    }
    
    void draw(){
      background(255);
      player.draw();
    }
    

    How does the position change?

    class Player {
     float px, py;
     float vx, vy;
     Player(){
      px = random(width-20);
      py = random(height-20);
      vx = random(-4,4);
      vy = random(-4,4);
     }
     void draw(){
       simulate();
       render();
     }
     void simulate(){
       px+=vx;
       py+=vy;
     }
     void render(){
       stroke(0);
       fill(0,0,200);
       rect(px,py,20,20);
     }
    }
    
    Player player;
    
    void setup(){
      size(600,400);
      player = new Player();
    }
    
    void draw(){
      background(255);
      player.draw();
    }
    

    How does what changes the position change?

  • ok, now i have a square that moves, how can i make a platform as a floor and make it move using keys (i need that it can jump and have gravity)

  • Answer ✓
    class Player {
      float px, py;
      float vx, vy;
      float ax, ay;
      Player() {
        px = width/2;
        py = height/2;
        vx = random(-4, 4);
        vy = random(-4, 4);
        ax = 0;
        ay = .32;
      }
      void draw() {
        simulate();
        render();
      }
      void simulate() {
        vx+=ax;
        vy+=ay;
        px+=vx;
        py+=vy;
        px = constrain(px,10,width-10);
        py = constrain(py,20,height);
        if(py==height){
          vy = 0;
        }
    
      }
      void render() {
        stroke(0);
        fill(0, 0, 200);
        rect(px-10, py-20, 20, 20);
        noStroke();
        fill(255,0,0);
        ellipse(px, py,3,3);
      }
    }
    
    Player player;
    
    void setup() {
      size(600, 400);
      player = new Player();
    }
    
    void draw() {
      background(255);
      player.draw();
    }
    
    void keyPressed() {
      if ( keyCode == UP ) {
        player.vy-=10;
      } else if ( keyCode == DOWN ) {
        player.vy = 0;
      } else if ( keyCode == LEFT ) {
        player.vx = -4;
      } else if ( keyCode == RIGHT ) {
        player.vx = 4;
      }
    }
    
    void keyReleased() {
      if ( keyCode == UP ) {    
      } else if ( keyCode == DOWN ) {
      } else if ( keyCode == LEFT ) {
        player.vx = 0;
      } else if ( keyCode == RIGHT ) {
        player.vx = 0;
      }
    }
    

    So now you should write a Platform class. Then check if the player is on a platform.

  • Well, how's it going?

  • its quite nice, but when you press UP i does jump, but if you keep pressing it just smashes itself to the ceiling

  • Answer ✓

    That's minor. How are your Platforms doing?

    Here are mine:

    class Platform {
      float px, py, l, t;
      boolean wasAbove;
      Platform(float ipx, float ipy, float il, float it){
        px = ipx;
        py = ipy;
        l = il;
        t = it;
      }
      boolean isOn(float x, float y){
        return( x>=px && x<=px+l && y>=py && y<=py+t);
      }
      void draw(){
        fill(0);
        noStroke();
        rect(px,py,l,t);
      }
    }
    
    class Player {
      float px, py;
      float vx, vy;
      float ax, ay;
      boolean canJump = false;
      Player() {
        px = width/2;
        py = height/2;
        vx = 0;//random(-4, 4);
        vy = 0;//random(-4, 4);
        ax = 0;
        ay = .32;
      }
      void draw() {
        simulate();
        render();
      }
      void simulate() {
        vx+=ax;
        vy+=ay;
        px+=vx;
        py+=vy;
        //if(vy>4){vy = 4;}
        px = constrain(px,10,width-10);
        canJump = false;
        for( Platform p : platforms ){
          if( p.isOn(px,py)) {
            vy = 0;
            py = p.py;
            canJump = true;
          }
        }    
      }
      void render() {
        stroke(0);
        fill(0, 0, 200);
        rect(px-10, py-20, 20, 20);
        noStroke();
        fill(255,0,0);
        ellipse(px, py,3,3);
      }
    }
    
    Player player;
    ArrayList<Platform> platforms = new ArrayList();
    
    void setup() {
      size(600, 400);
      player = new Player();
      platforms.add( new Platform(0,height,width,100) );
      platforms.add( new Platform(100,300,100,5) );
      platforms.add( new Platform(200,200,100,5) );
      platforms.add( new Platform(300,100,100,5) );  
    }
    
    void draw() {
      background(255);
      player.draw();
      for( Platform p : platforms){
        p.draw();
      }
    }
    
    void keyPressed() {
      if ( keyCode == UP ) {
        if( player.canJump){
          player.vy-=10;
          player.canJump = false;
        }
      } else if ( keyCode == DOWN ) {
      } else if ( keyCode == LEFT ) {
        player.vx = -4;
      } else if ( keyCode == RIGHT ) {
        player.vx = 4;
      }
    }
    
    void keyReleased() {
      if ( keyCode == UP ) {    
      } else if ( keyCode == DOWN ) {
      } else if ( keyCode == LEFT ) {
        player.vx = 0;
      } else if ( keyCode == RIGHT ) {
        player.vx = 0;
      }
    }
    

    Well anyway, that's all I'm doing for now.

  • and that's all i needed, thank you, you are awesome!!!!

  • question, why does it fall through the platform if you jump from a higher platform?

  • Now it's your turn to work it out

Sign In or Register to comment.