Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package twodee;
- import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
- import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
- import static org.lwjgl.opengl.GL11.GL_PROJECTION;
- import static org.lwjgl.opengl.GL11.glClear;
- import static org.lwjgl.opengl.GL11.glLoadIdentity;
- import static org.lwjgl.opengl.GL11.glMatrixMode;
- import static org.lwjgl.opengl.GL11.glOrtho;
- import static org.lwjgl.opengl.GL11.glRectd;
- import java.util.ArrayList;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- public class Renderer {
- private Engine engine;
- private static int DISPLAY_X=1024, DISPLAY_Y=768;
- public Renderer(Engine engine) {
- this.engine=engine;
- try {
- Display.setDisplayMode(new DisplayMode(DISPLAY_X, DISPLAY_Y));
- Display.setTitle("my2DGameEngine");
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- }
- }
- public void init() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, DISPLAY_X, DISPLAY_Y, 0, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- engine.boxes.add(new Box(500,500,50,50));
- engine.boxes.add(new Box(100,200,50,50));
- engine.boxes.add(new Box(200,300,50,50));
- }
- public void draw() {
- glClear(GL_COLOR_BUFFER_BIT);
- for (Box box : engine.boxes) {
- box.draw();
- }
- Display.update();
- Display.sync(60);
- }
- public void destroy() {
- Display.destroy();
- }
- public boolean isCloseRequested() {
- return Display.isCloseRequested();
- }
- }
- package twodee;
- import org.lwjgl.opengl.Display;
- import java.util.ArrayList;
- public class Engine extends Thread {
- public ArrayList<Box> boxes = new ArrayList<Box>();
- private Renderer render;
- private static Engine engine;
- public Engine() {
- //nothing
- }
- public static Engine instance() {
- if (engine == null) {
- engine = new Engine();
- }
- return engine;
- }
- public void run() {
- render = new Renderer(instance());
- render.init();
- while (!render.isCloseRequested()) {
- this.update();
- this.collision();
- render.draw();
- }
- render.destroy();
- }
- public void update() {
- for (Box box : engine.boxes) {
- box.update();
- }
- }
- public void collision() {
- for (Box box : engine.boxes) {
- for (Box other : engine.boxes) {
- if (box.intersects(other)) {
- if (box!=other) {
- box.collided();
- }
- }
- }
- }
- }
- }
- package twodee;
- import static org.lwjgl.opengl.GL11.glRectd;
- import java.awt.Rectangle;
- public class Box {
- private double x,y,h,w;
- private double directionx, directiony;
- protected Rectangle hitbox = new Rectangle();
- public double boundx=1024, boundy=768;
- public Box(double x, double y, double w, double h) {
- this.x=x; this.y=y; this.h=h; this.w=w;
- if(Math.random()>0.5) {
- directionx=1;
- } else {
- directionx=-1;
- }
- if (Math.random()>0.5) {
- directiony=1;
- } else {
- directiony=-1;
- }
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public double getWidth() {
- return w;
- }
- public double getHeight() {
- return h;
- }
- public void update() {
- if (x+w>boundx) {
- directionx = -1;
- }
- if (y+h>boundy) {
- directiony = -1;
- }
- if (x<0) {
- directionx = 1;
- }
- if (y<0) {
- directiony = 1;
- }
- this.x = x + directionx*1;
- this.y = y + directiony*2;
- }
- public boolean intersects(Box other) {
- hitbox.setBounds((int) x, (int) y, (int) w, (int)h );
- return hitbox.intersects(other.getX(), other.getY(), other.getWidth(), other.getHeight());
- }
- public void draw() {
- glRectd(x, y, x + w, y + h);
- }
- public void collided() {
- if (directionx==-1) {
- directionx=1;
- } else {
- directionx=-1;
- }
- if (directiony==-1) {
- directiony=1;
- } else {
- directiony=-1;
- }
- }
- }
- import twodee.Engine;
- public class my2DEngineGame {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Engine engine = new Engine();
- engine.start();
- }
- }
Add Comment
Please, Sign In to add comment