我目前正在使用LWJGL构建一个小游戏,我得到了一个包含我的游戏循环、实例等的游戏类.以及一个Player类,它包含构造函数(用于x和y弦)、计算的逻辑方法、按键等。以及在屏幕上绘制播放机的渲染方法。由于某种原因,当我按下(Key.UP)时,玩家跳下地板,然后向上跳到地板上(倒转,而不是向上跳,然后下降到地板上)。如果你能告诉我问题出在哪里,我会非常感激的。
Game.java:
import static org.lwjgl.opengl.GL11.*;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Game {
static Texture bgTexture;
static Player player;
public static void main(String[] args) throws LWJGLException, IOException {
// Initialize
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
bgTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/background.png"));
player = new Player(50, 200 - 64);
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
drawBackground();
player.render();
Display.update();
Display.sync(60);
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
Display.destroy();
System.exit(0);
}
}
}
public static void drawBackground() {
bgTexture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(bgTexture.getTextureWidth(), 0);
glTexCoord2f(1, 1);
glVertex2f(bgTexture.getTextureWidth(), bgTexture.getTextureHeight());
glTexCoord2f(0, 1);
glVertex2f(0, bgTexture.getTextureHeight());
glEnd();
}
}Player.java
import static org.lwjgl.opengl.GL11.*;
import java.io.IOException;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Player {
public double x, y, xspeed, yspeed;
private Texture pTexture;
public Player(double x, double y) throws IOException {
this.x = x;
this.y = y;
pTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/player.png"));
}
public void logic() {
x += xspeed;
y += yspeed;
yspeed -= 0.4;
// Collision detection
if (y <= 480 - 64) {
y = 480 - 64;
yspeed = 0;
if (!Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && xspeed > 0) xspeed = xspeed * 0.7;
if (!Keyboard.isKeyDown(Keyboard.KEY_LEFT) && xspeed < 0) xspeed = xspeed * 0.7;
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) yspeed = 8;
}
// Moving
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) xspeed = 4;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) xspeed = -4;
}
public void render() {
logic();
glPushMatrix();
pTexture.bind();
glTranslated(x, y, 0);
glBegin(GL_QUADS);
glTexCoord2d(0, 0);
glVertex2d(0, 0);
glTexCoord2d(1, 0);
glVertex2d(pTexture.getTextureWidth(), 0);
glTexCoord2d(1, 1);
glVertex2d(pTexture.getTextureWidth(), pTexture.getTextureHeight());
glTexCoord2d(0, 1);
glVertex2d(0, pTexture.getTextureHeight());
glEnd();
glPopMatrix();
}
}非常感谢。
发布于 2014-02-08 18:24:58
您正在调用glOrtho(0, 640, 480, 0, 1, -1);,它将480设置为屏幕底部,0设置为顶部。如果您使用glOrtho(0, 640, 0, 480, 1, -1);,一个较大的y值将意味着屏幕上的一个更高的点,这就是我想要的。
https://stackoverflow.com/questions/21647684
复制相似问题