How to make ball jump with Arrow Keys??

edited November 2017 in Questions about Code
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;

float cercleX = 50;
float cercleY = 50;
float gravetat = 0.8;
float posx = 200;
float posy = 50;
float vy = 0;
float bounce = -0.9;


//////////////////////////////////////////////////////////////////////////////////////

void setup(){

size (500,500);
smooth();
noStroke();
}
void draw(){
  frameRate (70);
background (255);
fill(0);
rect(0,400,500,100);
fill(255,0,0);
  ellipse(posx, posy, 20,20);

  vy += gravetat;
  posy += vy;
  if(posy > height - 120){
    vy *= bounce;
  }


  if (upPressed) {
    posy--;
  }

  if (downPressed) {
    posy++;
  }

  if (leftPressed) {
    posx--;
  }

  if (rightPressed) {
    posx++;
  }

}

void keyPressed() {
  if (keyCode == UP) {
    upPressed = true;
  }
  else if (keyCode == DOWN) {
    downPressed = true;
  }
  else if (keyCode == LEFT) {
    leftPressed = true;
  }
  else if (keyCode == RIGHT) {
    rightPressed = true;
  }
}

void keyReleased() {
  if (keyCode == UP) {
    upPressed = false;
  }
  else if (keyCode == DOWN) {
    downPressed = false;
  }
  else if (keyCode == LEFT) {
    leftPressed = false;
  }
  else if (keyCode == RIGHT) {
    rightPressed = false;
  }
}
Tagged:

Answers

  • First let's shorten your code a ton :

    char direction;
    
    float cercleX = 50;
    float cercleY = 50;
    float gravetat = 0.8;
    float posx = 200;
    float posy = 50;
    float vy = 0;
    float bounce = -0.9;
    
    
    //////////////////////////////////////////////////////////////////////////////////////
    
    void setup() {
    
     size(500, 500);
     smooth();
     noStroke();
    }
    void draw() {
     frameRate(70);
     background(255);
     fill(0);
     rect(0, 400, 500, 100);
     fill(255, 0, 0);
     ellipse(posx, posy, 20, 20);
    
     vy += gravetat;
     posy += vy;
     if (posy > height - 120) {
      vy *= bounce;
     }
    
     switch (direction) {
      case 'w':
       posy--;
       break;
      case 's':
       posy++;
       break;
      case 'a':
       posx--;
       break;
      case 'd':
       posx++;
       break;
     }
    }
    
    void keyPressed() {
     switch (keyCode) {
       case UP:
        direction = 'w';
        break;
       case DOWN:
        direction = 's';
        break;
       case LEFT:
        direction = 'a';
        break;
       case RIGHT:
        direction = 'd';
        break;
     }
    }
    

    You can read on what a switch statement is here. It's basically a shortened if statement for cases such as these where the condition is just what a variable is.

    As for jumping, I would appreciate a few more details. Do you want the ball to have the capability to jump at all times? Or just when it's in contact with the ground. If it's the latter, do you want the ball to lose its bounce height the more it bounces?

  • edited November 2017

    [Edited: Remark: Sorry, I haven't seen the previous two posts]

    in theory, to jump you say vy=-12;

    it's a high speed upwards (the jump) and the speed gets smaller by gravity

    boolean upPressed = false;
    boolean downPressed = false;
    boolean leftPressed = false;
    boolean rightPressed = false;
    
    float cercleX = 50;
    float cercleY = 50;
    float gravetat = 0.8;
    float posx = 0;
    float posy = 50;
    float vy = 0;
    float bounce = -0.9;
    
    boolean stopFlag=false; 
    
    //////////////////////////////////////////////////////////////////////////////////////
    
    void setup() {
      size (500, 500);
      smooth();
      noStroke();
    }
    
    void draw() {
    
      background (255);
    
      posx++;
    
      frameRate (70);
      fill(0);
      rect(0, 400, 500, 100);
      fill(255, 0, 0);
      ellipse(posx, posy, 20, 20);
    
      if (!stopFlag) {
        vy += gravetat;
        posy += vy;
      }
    
      ellipse(33, height - 120, 
        7, 7);
    
      if (posy > height - 120) {
    
        vy = bounce * abs(vy);
    
        if (abs(vy)<0.281) {
          println("full stop");
          vy=0.0;
          // gravetat=0.0;
          stopFlag=true;
        }//if
      }//if 
    
      // booleans 
      if (upPressed) {
        // posy--;
        stopFlag=false; 
        vy=-12;
      }
    
      if (downPressed) {
        posy++;
      }
    
      if (leftPressed) {
        posx--;
      }
    
      if (rightPressed) {
        posx++;
      }
    }
    
    void keyPressed() {
      if (keyCode == UP) {
        upPressed = true;
      } else if (keyCode == DOWN) {
        downPressed = true;
      } else if (keyCode == LEFT) {
        leftPressed = true;
      } else if (keyCode == RIGHT) {
        rightPressed = true;
      }
    }
    
    void keyReleased() {
      if (keyCode == UP) {
        upPressed = false;
      } else if (keyCode == DOWN) {
        downPressed = false;
      } else if (keyCode == LEFT) {
        leftPressed = false;
      } else if (keyCode == RIGHT) {
        rightPressed = false;
      }
    }
    //
    
Sign In or Register to comment.