How Do I code jumping with gravity?

I am trying to code jumping with gravity bringing me back down when I reach the maximum. But when I jump if I land on one of the platforms on the way down I want to stay on the platform. The up arrow makes it jump up but it doesn't come back down. The down arrow makes it go somewhat down but not all the way. Here is the code I currently have for the whole thing.

PImage backgroundPic;
PImage playerPic;                   // friendly tip: player is 50px wide & 125px tall
PImage bullet1Pic;
PImage bullet2Pic;

int playerX = 0;        
int playerY = 490;
int bullet1Y = playerY-50;
int bullet2Y = playerY-50;
int bullet3Y = 260; 
int bullet4Y = 260;
int bullet1X = playerX+80;
int bullet2X = playerX+80;
int bullet3X = 500;
int bullet4X = 580;
int shoot = 1;


void setup ()
{
  size (900, 675);
  backgroundPic = loadImage ("http://" + "i.imgur.com/tO513qf.jpg");          // pre-loads each image
  playerPic = loadImage ("http://" + "i.imgur.com/RppfHLp.png");
  bullet1Pic = loadImage ("http://" + "i.imgur.com/g4Ug0nv.png");
  bullet2Pic = loadImage ("http://" + "i.imgur.com/BJWgv8x.png");
}

void draw ()
{
  background (backgroundPic);
  image(playerPic, playerX, playerY);  
  image(bullet2Pic, bullet3X, bullet3Y);
  image(bullet2Pic, bullet4X, bullet4Y);

if (shoot == 1){
  image(bullet1Pic, bullet1X, bullet1Y);       
  image(bullet1Pic, bullet2X, bullet2Y);

  bullet1X += 10;              // "gravity" for falling bulletes
  bullet2X += 10;
  bullet3X -= 10;
  bullet4X -= 10;
  bullet1Y = playerY-50;
  bullet2Y = playerY-50;
  bullet3Y = 260;
  bullet4Y = 260;
}


  if (bullet1X > 950)          // re-loops each bullet when it reaches the bottom
    bullet1X = playerX+80;                  // NOTE: because there's only a single line in the body 
  if (bullet2X > 950)              // body of each if, brackets are technically not necessary
    bullet2X = playerX+80;
  if (bullet3X < -130)
    bullet3X = 500;
  if (bullet4X < -50)
    bullet4X = 580;


  println (playerX, playerY);      // top left corner of player's head

}

void keyPressed()
{
  if  (key == CODED)
  {
    if (keyCode == UP)
    {
      playerY -= 10;

    }if (keyCode == DOWN)
    {
      playerY += 10;
    }
    if (keyCode == LEFT)
    {
      playerX -= 10;
    }
    if (keyCode == RIGHT)
    {
      playerX += 10;
    }


    //shooting





    //boundaries
  }
  if (playerX < 0)
  {
    playerX = 0;
  }
  if (playerX > 440)
  {
    playerX = 440;
  }
  if (playerY > 490)
  {
    playerY = 490;
  }
  if (playerY < 220)
  {
    playerY = 220;
  }
}
void keyReleased() {
 if (shoot == 1) {
   shoot = 0;
 } else {
   shoot = 1;
 }
}
Tagged:

Answers

  • Edit your post. Select your code. Press ctrl + o.

  • You have two variables - playerX and playerY - that determine the player's position. There is a lot of code that changes these values and makes sure they have valid values already. You just need to add some more logic to make the values of those variables change in a way that makes it feel like there are platforms. Start, for example, by increasing the playerY value each time draw() runs. Now do that unless the player is over a platform. How do you know if the player is over a platform? You will need more variables to remember where the platforms are, and more code logic to check...

  • this doesn't make much sense...

    void keyReleased() {
      if (shoot == 1) {
        shoot = 0;
      } else {
        shoot = 1;
      }
    }
    

    because you fire when you jump imho

    you want to check e.g. space bar ' ' in keyPressed()

  • PImage backgroundPic;
    PImage playerPic;                   // friendly tip: player is 50px wide & 125px tall
    PImage bullet1Pic;
    PImage bullet2Pic;
    
    int playerX = 0;        
    float playerY = 490;
    
    float bullet1Y = playerY-50;
    float bullet2Y = playerY-50;
    
    int bullet3Y = 260; 
    int bullet4Y = 260;
    int bullet1X = playerX+80;
    int bullet2X = playerX+80;
    int bullet3X = 500;
    int bullet4X = 580;
    int shoot = 1;
    
    float playervy; // velocity
    float gravity = 0.63; //  gravity = 0.03;
    
    float platform1, platform2;
    
    
    void setup ()
    {
      size (900, 675);
      backgroundPic = loadImage ("http://" + "i.imgur.com/tO513qf.jpg");          // pre-loads each image
      playerPic = loadImage ("http://" + "i.imgur.com/RppfHLp.png");
      bullet1Pic = loadImage ("http://" + "i.imgur.com/g4Ug0nv.png");
      bullet2Pic = loadImage ("http://" + "i.imgur.com/BJWgv8x.png");
    
      platform1 = playerY+playerPic.height-110;
      platform2 = playerY+playerPic.height-310;
    }
    
    void draw ()
    {
      background (backgroundPic);
    
      image(playerPic, playerX, playerY);  
      image(bullet2Pic, bullet3X, bullet3Y);
      image(bullet2Pic, bullet4X, bullet4Y);
    
      if (shoot == 1) {
        image(bullet1Pic, bullet1X, bullet1Y);       
        image(bullet1Pic, bullet2X, bullet2Y);
    
        bullet1X += 10;              // "gravity" for falling bulletes
        bullet2X += 10;
        bullet3X -= 10;
        bullet4X -= 10;
        bullet1Y = playerY-50;
        bullet2Y = playerY-50;
        bullet3Y = 260;
        bullet4Y = 260;
    
    
    
        if (bullet1X > 950)          // re-loops each bullet when it reaches the bottom
          bullet1X = playerX+80;                  // NOTE: because there's only a single line in the body 
        if (bullet2X > 950)              // body of each if, brackets are technically not necessary
          bullet2X = playerX+80;
        if (bullet3X < -130)
          bullet3X = 500;
        if (bullet4X < -50)
          bullet4X = 580;
      }
    
      //  println (playerX, playerY);      // top left corner of player's head
    
      playervy += gravity; // new 
      playerY += playervy;
      if (playerY>=platform1) {
        playervy=0;
        playerY=platform1;
      }
    }
    
    void keyPressed()
    {
      if  (key == CODED)
      {
        if (keyCode == UP)
        {
          playerY -= 70; // jump
          playervy = 0.0;
        }
        if (keyCode == DOWN)
        {
          playerY += 10;
        }
        if (keyCode == LEFT)
        {
          playerX -= 10;
        }
        if (keyCode == RIGHT)
        {
          playerX += 10;
        }
    
    
        //shooting
    
    
    
    
    
        //boundaries
      }
      if (playerX < 0)
      {
        playerX = 0;
      }
      if (playerX > 440)
      {
        playerX = 440;
      }
      if (playerY > 490)
      {
        playerY = 490;
      }
      if (playerY < 220)
      {
        playerY = 220;
      }
    }
    void keyReleased() {
      if (shoot == 1) {
        shoot = 0;
      } else {
        shoot = 1;
      }
    }
    
    //
    
Sign In or Register to comment.