我想做一个小应用程序,你有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();
}
}
发布于 2013-02-20 23:52:39
尝试在lines()
中移动rx
/ry
计算
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);
}
https://stackoverflow.com/questions/14983694
复制相似问题