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.1;
float rad = 25;

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, rad, rad);

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

  if (vy < 0 && posy > height -120) {
    vy = 0;
    gravetat = 0;
  } else if (posy > height -120) {
    vy = vy *-0.9;
  }


  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;
    gravetat = 0.98;
    bounce = 0.9;
    posy = 300;
  } else if (keyCode == DOWN) {
    downPressed = false;
  } else if (keyCode == LEFT) {
    leftPressed = false;
  } else if (keyCode == RIGHT) {
    rightPressed = false;
  }
}
Tagged:
Sign In or Register to comment.