我已经建立了一个非常基本的平台游戏,我现在正在设置它。
除了跳跃之外,一切都很完美,我的跳跃长度非常不一致。它们的范围从非常短到非常长。
我不习惯使用 Slick2D、JWJGL 或 delta 整数,请帮忙!
这是我的整个关卡代码:
package game;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class LevelOne extends BasicGameState{
Animation walkingLeft, walkingRight, still;
int[] duration = {200,200,200};
Image p;
Image map;
public String mouse = "Mouse not detected";
public String gpos = "null";
public int health = 1000;
float CharX = 0;
float CharY = 0;
float PosX = CharX + 100;
float PosY = CharY + 300;
boolean jumping = false;
float verticalSpeed = 0.0f;
public LevelOne(int state){
}
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
p = new Image("res/char.png");
map = new Image("res/map.png");
Image[] Left = {p.getSubImage(0,32,32,32), p.getSubImage(32,32,32,32), p.getSubImage(64,32,32,32)};
Image[] Right = {p.getSubImage(0,64,32,32), p.getSubImage(0,64,32,32), p.getSubImage(0,64,32,32)};
walkingLeft = new Animation(Left, duration, false);
walkingRight = new Animation(Right, duration, false);
still = walkingLeft;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.setColor(Color.magenta);
g.drawString("Developers Build", 650, 100);
g.setColor(Color.white);
g.drawString(mouse, 650, 150);
g.drawString(gpos,650, 200); //Developers Build Sidebar thing
g.drawString("Class: ", 650, 250);
g.drawString("" + this.getClass(), 670, 270);
g.drawString("State: " + this.getID(), 650, 300);
g.drawString("Stage: Charlie", 650, 350);
g.drawRect(620, 70, 400, 320);
map.draw(CharX, CharY); //Floor
g.setColor(Color.white);
if(health < 500){
g.setColor(Color.red);
}
g.drawString("Health: " + health, 10, 50);
g.setColor(Color.white); //Health & Level
g.drawString("Level: 1", 10, 70);
still.draw(PosX, PosY); //Draw Character
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
int MouseX = Mouse.getX();
int MouseY = Mouse.getY();
mouse = "JPos: " + MouseX + " , " + MouseY;
gpos = "GPos: " + MouseX + " , " + (460 - MouseY);
if(input.isKeyDown(Input.KEY_D)){
still = walkingRight;
CharX -= delta * .2f;
}
if(input.isKeyDown(Input.KEY_A)){
still = walkingLeft;
CharX += delta * .2f;
if(CharX > 100){
CharX -= delta *.2f;
}
}
if (input.isKeyPressed(Input.KEY_W) && !jumping) {
verticalSpeed = -1f * delta;//negative value indicates an upward movement
jumping = true;
}
if (jumping) {
verticalSpeed += .0099f * delta;//change this value to alter gravity strength
}
if(PosY > 300){
jumping = false;
verticalSpeed = 0;
PosY = 300;
}
PosY += verticalSpeed;
if(health == 0){
CharX = 0;
health = 1000;
}
}
public int getID(){
return 2;
}
}