Can't figure out how to make a character jump in Slick2d+LWJL

812 Views Asked by At

I have been thinking about this and I cannot think of anyway I can make a character jump in my game. It s a platformer, state based game in slick2d+LWJGL.

Here is my code

package net.battle.main;

import java.io.Console;
import java.util.Arrays;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;

public class Level implements GameState {

private static final float shfitX = 0;
Animation ninja, fairy, ninja_left, fairy_left, ninja_right, fairy_right;
Image WorldMap;
boolean pause = false;
int[] duration = {200, 200, 200};
float charPosX= 0;
float charPosY= 0;
float shiftX= charPosX + 360;
float shiftY= charPosY + 160;

public Level(int level) {
}

@Override
public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {


}

@Override
public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void mouseWheelMoved(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void inputEnded() {
    // TODO Auto-generated method stub

}

@Override
public void inputStarted() {
    // TODO Auto-generated method stub

}

@Override
public boolean isAcceptingInput() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void setInput(Input arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerButtonPressed(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerButtonReleased(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerDownPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerDownReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerLeftPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerLeftReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerRightPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerRightReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerUpPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerUpReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void enter(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

@Override
public int getID() {
    // TODO Auto-generated method stub
    return 1;
}

@Override
public void init(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    WorldMap = new Image("res/testmap.png");
    Image[] ninjaRight = {new Image("res/ninja-right.png"),new Image("res/ninja-right-walking.png"), new Image("res/ninja-right-walking-two.png")};
    Image[] ninjaLeft = {new Image("res/ninja-left.png"),new Image("res/ninja-left.png"), new Image("res/ninja-left.png")};
    Image[] ninjaStanding = {new Image("res/ninja.png"),new Image("res/ninja.png"), new Image("res/ninja.png")};

    Image[] fairyRight = {new Image("res/fairy-right.png"),new Image("res/fairy-right-walking.png"), new Image("res/fairy-right-walking-two.png")};
    Image[] fairyLeft = {new Image("res/fairy-left.png"), new Image("res/fairy-left-walking.png"), new Image("res/fairy-left-walking-two.png")};
    Image[] fairyStanding = {new Image("res/fairy.png"),new Image("res/fairy.png"), new Image("res/fairy.png")};

ninja_right = new Animation(ninjaRight, duration, true);
ninja_left = new Animation(ninjaLeft, duration, true);
ninja = new Animation(ninjaStanding, duration, false);

fairy_right = new Animation(fairyRight, duration, true);
fairy_left = new Animation(fairyLeft, duration, true);
fairy = new Animation(fairyStanding, duration, false);
}

@Override
public void leave(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

@Override
public void render(GameContainer gc, StateBasedGame s, Graphics g)throws SlickException {

    WorldMap.draw(charPosX,charPosY);
    fairy.draw(shiftX,shiftY);

    g.drawString("char X: "+charPosX+"\nchar Y: "+charPosY, 400, 20);

    if(pause==true){
        g.setColor(Color.black);
        g.fillRect(240, 100, 200, 100);
        g.setColor(Color.white);
        g.drawString("Resume (Press esc)", 250, 110);
        g.drawString("Main Menu (Press M)", 250, 130);
        g.drawString("Quit (Press Q)", 250, 150);

    if(pause==false){
        g.clear();

    }
    }



}

@Override
public void update(GameContainer gc, StateBasedGame s, int delta)
        throws SlickException {
    boolean n;
    Input input = gc.getInput();
    //System.out.println(pause);

// PAUSE MENU
    if(pause==true){
    if(Keyboard.isKeyDown(input.KEY_M)){
        System.out.println("Key Press: M has been pressed");
        System.out.println(pause);
        s.enterState(0);
    }

    if(Keyboard.isKeyDown(input.KEY_Q)){

        System.out.println("Key Press: Q has been pressed \nExiting game");
        System.exit(0);
    }
    }
    if(input.isKeyPressed(input.KEY_ESCAPE)){
        System.out.println("Key Press: Escape has been pressed");

        if(pause==true){
            System.out.println(pause);
            System.out.println("boolean change");
            pause=false;
            System.out.println(pause);
            gc.setPaused(false);
            System.out.println("unpaused");
        }else if(pause==false){
            System.out.println(pause);
            System.out.println("boolean change");
            pause=true;
            System.out.println(pause);
            gc.setPaused(true);
            System.out.println("paused");
        }

    }
    // END OF PAUSE MENU
    // CHARACTER MOVEMENT

    if(Keyboard.isKeyDown(input.KEY_SPACE)){
        fairy=fairy;


    }
    if(Keyboard.isKeyDown(input.KEY_D)){

        fairy = fairy_right;


        charPosX -= delta *.1;
        if(charPosX<-1607){
            charPosX += delta *.1;
        }

    }

        if(input.isKeyDown(input.KEY_A)){
            fairy = fairy_left;
            charPosX += delta *.1;
            if(charPosX>361){
                charPosX -= delta *.1;

        }
    }
}

}

So if anyone can come up with a way to let my character be able to jump that would be awesome!

1

There are 1 best solutions below

0
On

Alright, that's quite simple to solve. At first, I'd create a Vector2f (I think it's called that way anyway) and call it like player velocity.

if(Keyboard.isKeyDown(input.KEY_SPACE)){
    fairy=fairy;

    velocity.y = 1; //play around with that number to influence gravity
}

updatePlayer();

and then the updatePlayer() method:

protected void updatePlayer() {

if(velocity.y > 0) {
    charPosY = charPosY + (delta * velocity.y);
    velocity.y = velocity.y - (0.1 * delta);
}

Those are just code snippets, they do not work. I wrote them to give you and Idea of what it should look like