Guest User

Untitled

a guest
Jan 22nd, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. package movingbox;
  2.  
  3. import org.lwjgl.LWJGLException;
  4. import org.lwjgl.input.Keyboard;
  5. import org.lwjgl.input.Mouse;
  6. import org.lwjgl.opengl.Display;
  7. import org.lwjgl.opengl.DisplayMode;
  8. import org.lwjgl.opengl.GL11;
  9.  
  10. public class MovingBox {
  11.  
  12. public void start() {
  13. try {
  14. Display.setDisplayMode(new DisplayMode(800,600));
  15. Display.create();
  16. } catch (LWJGLException e) {
  17. e.printStackTrace();
  18. System.exit(0);
  19. }
  20.  
  21. // init OpenGL here
  22. GL11.glMatrixMode(GL11.GL_PROJECTION);
  23. GL11.glLoadIdentity();
  24. GL11.glOrtho(0, 800, 600, 0, 1, -1);
  25. GL11.glMatrixMode(GL11.GL_MODELVIEW);
  26.  
  27. float x = 100; float y = 100;
  28. float xDif = 1;
  29. float red = 0.5f;
  30. while (!Display.isCloseRequested()) {
  31. // Clear the screen and depth buffer
  32. GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  33.  
  34. // set the color of the quad (R,G,B,A)
  35. GL11.glColor3f(red,red,1.0f);
  36.  
  37. // draw quad
  38. GL11.glBegin(GL11.GL_QUADS);
  39. GL11.glVertex2f(x,y);
  40. GL11.glVertex2f(x+200,y);
  41. GL11.glVertex2f(x+200,y+200);
  42. GL11.glVertex2f(x,y+200);
  43. GL11.glEnd();
  44.  
  45.  
  46. GL11.glBegin(GL11.GL_QUADS);
  47. GL11.glVertex2f(x,y);
  48. GL11.glVertex2f(x+250,y);
  49. GL11.glVertex2f(x+250,y+300);
  50. GL11.glVertex2f(x,y+250);
  51. GL11.glEnd();
  52. if(checkHit(x,y)){
  53. red+=.005;
  54. System.out.println("win");
  55. }
  56. pollInput(x,y);
  57. Display.update();
  58. if(x>500) xDif = -1;
  59. if(x<100) xDif = 1;
  60. x+=xDif;
  61. }
  62.  
  63. Display.destroy();
  64. }
  65.  
  66. public boolean checkHit(float x, float y){
  67. if(Mouse.isButtonDown(0)){
  68. int mouseX = Mouse.getX();
  69. int mouseY = Mouse.getY();
  70. if(mouseX>x&&mouseX<x+200&&mouseY>200+y&&mouseY<=y+400) return true;
  71. else return false;
  72. }
  73. return false;
  74. }
  75.  
  76. public void pollInput(float xx, float yy) {
  77.  
  78. if (Mouse.isButtonDown(0)) {
  79. int x = Mouse.getX();
  80. int y = Mouse.getY();
  81.  
  82. System.out.println("MOUSE DOWN @ X: " +xx+":"+ x + " Y: "+yy+":" + y);
  83. }
  84.  
  85. if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
  86. System.out.println("SPACE KEY IS DOWN");
  87. }
  88.  
  89. while (Keyboard.next()) {
  90. if (Keyboard.getEventKeyState()) {
  91. if (Keyboard.getEventKey() == Keyboard.KEY_A) {
  92. System.out.println("A Key Pressed");
  93. }
  94. if (Keyboard.getEventKey() == Keyboard.KEY_S) {
  95. System.out.println("S Key Pressed");
  96. }
  97. if (Keyboard.getEventKey() == Keyboard.KEY_D) {
  98. System.out.println("D Key Pressed");
  99. }
  100. } else {
  101. if (Keyboard.getEventKey() == Keyboard.KEY_A) {
  102. System.out.println("A Key Released");
  103. }
  104. if (Keyboard.getEventKey() == Keyboard.KEY_S) {
  105. System.out.println("S Key Released");
  106. }
  107. if (Keyboard.getEventKey() == Keyboard.KEY_D) {
  108. System.out.println("D Key Released");
  109. }
  110. }
  111. }
  112. }
  113. public static void main(String[] argv) {
  114. MovingBox displayExample = new MovingBox();
  115. displayExample.start();
  116. }
  117. }
Add Comment
Please, Sign In to add comment