我正在尝试制作一个角色可以向上(不是跳跃),向左,向右和向下移动的游戏。我对lwjgl非常新,所以你找到的任何其他错误,或者如果你有任何提示给我,请不要退缩。
我的代码:
public class Player {
public float x, y, xspeed, yspeed, delta;
public long lastFrame;
public Player(){
x=350;
y=250;
xspeed=1;
}
public void drawPlayer(Texture tex, float x, float y, float width, float height) {
this.x=x;
this.y=y;
tex.bind();
glPushMatrix();
glTranslatef(x,y,0);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(width,0);
glTexCoord2f(1,1);
glVertex2f(width,height);
glTexCoord2f(0,1);
glVertex2f(0,height);
glEnd();
glLoadIdentity();
}
public void playerMovement(){
while(Keyboard.next()){
float delta= getDelta();
if(Keyboard.isKeyDown(Keyboard.KEY_D))
xspeed = Math.max(-5, xspeed-1);
xspeed*=0.9;
if (Keyboard.getEventKey()==Keyboard.KEY_W && Keyboard.getEventKeyState())
x++;
xspeed=Math.min(5, xspeed+1);
if (Keyboard.getEventKey()==Keyboard.KEY_A && Keyboard.getEventKeyState())
x++;
xspeed=Math.min(5, xspeed+1);
if (Keyboard.getEventKey()==Keyboard.KEY_S && Keyboard.getEventKeyState())
x++;
xspeed=Math.min(5, xspeed+1);
}
}
public float getDelta(){
long currentTime= getTime();
float delta=(float)(currentTime - lastFrame);
lastFrame=getTime();
return delta;
}
public long getTime(){
return (Sys.getTime() *1000 ) / Sys.getTimerResolution();
}
public void update(int xspeed, int yspeed ){
x+=xspeed;
y+=yspeed;
}
}
答案 0 :(得分:1)
我会说玩家运动中的if语句没有意义。
if(Keyboard.isKeyDown(Keyboard.KEY_D))
xspeed = Math.max(-5, xspeed-1);
xspeed*=0.9;
这里,只有第一个赋值取决于KEY_D,而不是乘法。因此,在(!)之后的最后一次xspeed分配,如果一直“胜利”。
if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
xspeed = Math.max(-5, xspeed-1);
xspeed*=0.9;
}
以下if语句也是如此。