0

我想做一个小应用程序,你有 4 行,每行都有第一个坐标在窗口的一侧随机设置,第二个坐标将跟随鼠标,所以你得到这个效果:

图像

您看不到光标,但线条在中心与光标相遇。

我的问题是我无法让它保持随机移动线条。因此,每一帧、每滴答或每一毫秒,这些行都会随机改变位置并删除前面的行。在此之后,我想随机更改线条的颜色,以便它会选择一个位置和一个随机位置,并且几乎可以让任何人都适合癫痫症,但我需要先弄清楚位置。

我试过做一些 glClear(GL_...) 命令,但它们似乎不起作用。有没有办法完全清除屏幕并重做 glBegin(GL_LINES) 命令将其定位在另一个地方?

这是我的代码:

    package tests;

import static org.lwjgl.opengl.GL11.*;
import java.util.Random;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;

public class game {

int wx = 600;
int hy = 400;

int rx = new Random().nextInt(wx);
int ry = new Random().nextInt(hy);

public game() throws LWJGLException {

    int mouseX = Mouse.getX();
    int mouseY = hy-Mouse.getY()-1;

    Display.setDisplayMode(new DisplayMode(wx, hy));
    Display.create();
    Display.setTitle("Game");

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, wx, hy, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);

    while(!Display.isCloseRequested()) {
        lines();

        while(Keyboard.next()) {
            if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
                Display.destroy();
                System.exit(0);
            }
        }

        while(Mouse.next()) {
            if(Mouse.isButtonDown(0)) {
                System.out.println("(" + mouseX + ", " + mouseY + ")");
            }
        }
    }
}

public void lines() {

    glClear(GL_COLOR_BUFFER_BIT);

    int mouseX = Mouse.getX();
    int mouseY = hy-Mouse.getY()-1;

    glBegin(GL_LINES);
        glVertex2i(rx, 0);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(0, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();
    glBegin(GL_LINES);
        glVertex2i(rx, hy);
        glVertex2i(mouseX, mouseY);
    glEnd();
    glBegin(GL_LINES);
        glVertex2i(wx, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();
        glLineWidth(5);
        glColor3f(1f, 0f, 0f);

    Display.update();
    Display.sync(500);

}


public static void main(String[] args) throws LWJGLException {
    new game();
    }
}
4

1 回答 1

0

Try moving your rx/ry calculation inside lines():

public void lines() 
{
    int rx = new Random().nextInt(wx);
    int ry = new Random().nextInt(hy);

    glClear(GL_COLOR_BUFFER_BIT);

    int mouseX = Mouse.getX();
    int mouseY = hy-Mouse.getY()-1;

    glLineWidth(5);
    glColor3f(1f, 0f, 0f);

    glBegin(GL_LINES);
        glVertex2i(rx, 0);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(0, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(rx, hy);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(wx, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();

    Display.update();
    Display.sync(500);
}
于 2013-02-20T15:52:39.463 回答