Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package movingbox;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.input.Mouse;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL11;
- public class MovingBox {
- public void start() {
- try {
- Display.setDisplayMode(new DisplayMode(800,600));
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- // init OpenGL here
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glLoadIdentity();
- GL11.glOrtho(0, 800, 600, 0, 1, -1);
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- float x = 100; float y = 100;
- float xDif = 1;
- float red = 0.5f;
- while (!Display.isCloseRequested()) {
- // Clear the screen and depth buffer
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
- // set the color of the quad (R,G,B,A)
- GL11.glColor3f(red,red,1.0f);
- // draw quad
- GL11.glBegin(GL11.GL_QUADS);
- GL11.glVertex2f(x,y);
- GL11.glVertex2f(x+200,y);
- GL11.glVertex2f(x+200,y+200);
- GL11.glVertex2f(x,y+200);
- GL11.glEnd();
- GL11.glBegin(GL11.GL_QUADS);
- GL11.glVertex2f(x,y);
- GL11.glVertex2f(x+250,y);
- GL11.glVertex2f(x+250,y+300);
- GL11.glVertex2f(x,y+250);
- GL11.glEnd();
- if(checkHit(x,y)){
- red+=.005;
- System.out.println("win");
- }
- pollInput(x,y);
- Display.update();
- if(x>500) xDif = -1;
- if(x<100) xDif = 1;
- x+=xDif;
- }
- Display.destroy();
- }
- public boolean checkHit(float x, float y){
- if(Mouse.isButtonDown(0)){
- int mouseX = Mouse.getX();
- int mouseY = Mouse.getY();
- if(mouseX>x&&mouseX<x+200&&mouseY>200+y&&mouseY<=y+400) return true;
- else return false;
- }
- return false;
- }
- public void pollInput(float xx, float yy) {
- if (Mouse.isButtonDown(0)) {
- int x = Mouse.getX();
- int y = Mouse.getY();
- System.out.println("MOUSE DOWN @ X: " +xx+":"+ x + " Y: "+yy+":" + y);
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
- System.out.println("SPACE KEY IS DOWN");
- }
- while (Keyboard.next()) {
- if (Keyboard.getEventKeyState()) {
- if (Keyboard.getEventKey() == Keyboard.KEY_A) {
- System.out.println("A Key Pressed");
- }
- if (Keyboard.getEventKey() == Keyboard.KEY_S) {
- System.out.println("S Key Pressed");
- }
- if (Keyboard.getEventKey() == Keyboard.KEY_D) {
- System.out.println("D Key Pressed");
- }
- } else {
- if (Keyboard.getEventKey() == Keyboard.KEY_A) {
- System.out.println("A Key Released");
- }
- if (Keyboard.getEventKey() == Keyboard.KEY_S) {
- System.out.println("S Key Released");
- }
- if (Keyboard.getEventKey() == Keyboard.KEY_D) {
- System.out.println("D Key Released");
- }
- }
- }
- }
- public static void main(String[] argv) {
- MovingBox displayExample = new MovingBox();
- displayExample.start();
- }
- }
Add Comment
Please, Sign In to add comment