2

我正在学习如何使用 OpenGL 并开始围绕 Slick 2D 游戏库构建一些实验,但我遇到了一些问题。

我正在尝试绘制简单的线条,将它们存储在一个数组中,然后遍历该数组以呈现每一行。这是主要课程:

    ArrayList<Bloco> listaBlocos = new ArrayList<Bloco>();
ArrayList<Linha> listaLinhas = new ArrayList<Linha>();
boolean random = false;

@Override
public void init(GameContainer gc) throws SlickException {
    listaBlocos = new ArrayList<Bloco>();
    listaLinhas = new ArrayList<Linha>();
}

@Override
public void update(GameContainer gc, int delta) throws SlickException {
    Input input = gc.getInput();

    if (input.isMousePressed(0))
        listaBlocos.add(new Bloco(input.getMouseX() - 5, input.getMouseY() - 5, 10, new Color(0, 255, 0)));
    if (input.isMousePressed(1))
        listaBlocos.add(new Bloco(input.getMouseX() - 5, input.getMouseY() - 5, 10, new Color(255, 0, 0)));

    if (input.isKeyPressed(Input.KEY_A))
        if (random == false)
            random = true;
        else
            random = false;

    if (input.isKeyPressed(Input.KEY_S))
        listaLinhas.add(new Linha(input.getMouseX(), input.getMouseY(), 100, 100));

    if (input.isKeyDown(Input.KEY_D))
        listaLinhas.get(0).drawLine();

    if (!listaBlocos.isEmpty() && random)
        for (Bloco b : listaBlocos)
            b.moveRandom(delta);

}

public void render(GameContainer gc, Graphics g) throws SlickException {

    for (Linha l : listaLinhas)
        l.drawLine();

    for (Bloco b : listaBlocos)
        b.draw(g);

}

这是 Linha 类(它扩展了 Line):

public class Linha extends Line {

public Linha(float x1, float y1, float x2, float y2) {
    super(x1, y1, x2, y2);
}

public void drawLine() {

    GL11.glColor3f(1.0f, 1.0f, 1.0f);
    GL11.glBegin(GL11.GL_LINES);
    GL11.glLineWidth(5.0f);
    GL11.glVertex2f(getX1(), getY1());
    GL11.glVertex2f(getX2(), getY2());
    GL11.glEnd();
}

}

这只是为了测试,如果有更好的方法来渲染一堆线条,请告诉我。问题是线条在屏幕上闪烁,它们并不总是全彩。我已经记录了这个问题,所以你可以看到发生了什么http://www.youtube.com/watch?v=ymO00yb5NVE

4

1 回答 1

1

我猜你在渲染中看到了奇怪的东西,因为你没有限制你的 FPS。

AppGameContainer gameContainer = new AppGameContainer(new Game("My Game"));
gameContainer.setTargetFrameRate(30);

对 FPS 没有限制可能会导致您的update方法无法以足够的间隔定期调用,因为您的游戏render过于频繁地调用该方法。

于 2012-07-09T18:06:45.130 回答