Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package rntest; import org.newdawn.slick.Color; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; 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; import java.util.Arrays; import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.TextureLoader; import org.newdawn.slick.util.ResourceLoader; /** * * @author nticknor19 */ public class Main { private static Board gBoard; private static float scale; public final static int SIDE_YELLOW = 500; public final static int SIDE_BLUE = 501; public final static int PIECE_LINK = 700; public final static int PIECE_VIRUS = 701; public static int[] sideProgression = new int[] { SIDE_YELLOW, SIDE_BLUE }; public static int turn = 0; public static int mySide = SIDE_YELLOW; public static int[] sPiece = new int[] { -1, -1 }; public static int[][] viruses = new int[sideProgression.length][4]; public static int[][] links = new int[sideProgression.length][4]; public static Texture[] textureLinks; public static Texture[] textureViruses; /** * @param args the command line arguments */ public static void main(String[] args) { int b = (int) (Display.getDisplayMode().getHeight() * .9); scale = (float) (b / 1024.); Main textureExample = new Main(); try { textureExample.start(b, b, "Rai-Net Access Battlers"); } catch (IOException ex) { System.out.println("Unable to start: IOException"); ex.printStackTrace(); } } public void start(int w, int h, String t) throws IOException { initGL(w, h, t); init(); while (true) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); pollInput(); render(); Display.update(); Display.sync(100); if (Display.isCloseRequested()) { Display.destroy(); System.exit(0); } } } /** * Initialize resources */ public void init() throws IOException { gBoard = new Board("res/board2.png", 8, 8, pointToScale(22), pointToScale(183), pointToScale(8), pointToScale(8), pointToScale(75), pointToScale(75)); textureLinks = new Texture[] { TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece500700_front.png")), TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece501700_front.png")) }; textureViruses = new Texture[] { TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece500701_front.png")), TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece501701_front.png")) }; //links=new int[][]{{500,501,500,501},{500,501,500,501}}; //viruses=new int[][]{{500,501,500,501},{500,501,500,501}}; } /** * Checks for keyboard input and does stuff */ public void pollInput() throws IOException { while (Mouse.next()) { float mx = Mouse.getX(); float my = Mouse.getY(); int sx = gBoard.getSpace(mx, my)[0]; int sy = gBoard.getSpace(mx, my)[1]; if (Mouse.getEventButtonState()) { if (Mouse.getEventButton() == 0) { gBoard.click(Mouse.getX(), Mouse.getY(), true); } } else { if (Mouse.getEventButton() == 0) { boolean doSelect = true; if (gBoard.inGameBoard(mx, my)) { if (sPiece[0] != -1 && sPiece[1] != -1 && !(sPiece[0] == sx && sPiece[1] == sy)) { if (pCanMove(gBoard.inSpace(sPiece[0], sPiece[1]), sx, sy)) { if (!gBoard.spaceIsEmpty(sx, sy)) { if (canCapture(gBoard.inSpace(sPiece[0], sPiece[1]), gBoard.inSpace(sx, sy))) { capture(gBoard.inSpace(sPiece[0], sPiece[1]), gBoard.inSpace(sx, sy)); gBoard.movePiece(sPiece[0], sPiece[1], sx, sy); sPiece = new int[] { -1, -1 }; doSelect = false; String ls = "L:", vs = "V:"; for (int i = 0; i < 4; i++) { ls += links[Arrays.binarySearch(sideProgression, mySide)][i] + " "; vs += viruses[Arrays.binarySearch(sideProgression, mySide)][i] + " "; } System.out.println(ls + vs); } } else { gBoard.movePiece(sPiece[0], sPiece[1], sx, sy); sPiece = new int[] { -1, -1 }; doSelect = false; } } } if (doSelect) { if (!gBoard.spaceIsEmpty(sx, sy)) { if (sPiece[0] == sx && sPiece[1] == sy) sPiece = new int[] { -1, -1 }; else sPiece = new int[] { sx, sy }; } else { sPiece = new int[] { -1, -1 }; } } } else { } } } } while (Keyboard.next()) { switch (Keyboard.getEventCharacter()) { case ' ': if (sideProgression[turn] == mySide && !(sPiece[0] == -1 && sPiece[1] == -1)) { gBoard.inSpace(sPiece[0], sPiece[1]).flip(); } System.out.println(sPiece[0]); break; case 'f': incrementTurn(); break; case 'o': mySide = SIDE_YELLOW; break; case 'p': mySide = SIDE_BLUE; break; } } } public static void incrementTurn() { turn++; if (turn >= sideProgression.length) turn = 0; } public static boolean pCanMove(Piece p, int gx, int gy) { boolean isT = mySide == sideProgression[turn], cmc = false, cms = true; int[] pxy = gBoard.find(p); if (pxy.length != 2) return false; if (p.getSide() == mySide) //tried to move own piece { //square logic. sadly, this has to be more specific switch (p.getSide()) { case SIDE_YELLOW: // if((pxy[0]==3&&pxy[1]==0)&&!(gx==4&&gy==0)) This doesn't actually apply lol // cms = false; // if((pxy[0]==4&&pxy[1]==0)&&!(gx==3&&gy==0)) // cms = false; if ((gx == 3 && gy == 7) || (gx == 4 && gy == 7)) cms = false; break; case SIDE_BLUE: break; } if (cms == false) return false; switch (p.getpID()) //actual piece logic { case PIECE_VIRUS: cmc = (Math.abs(gx - pxy[0]) == 1 && Math.abs(gy - pxy[1]) == 0) || (Math.abs(gx - pxy[0]) == 0 && Math.abs(gy - pxy[1]) == 1); break; case PIECE_LINK: cmc = (Math.abs(gx - pxy[0]) == 1 && Math.abs(gy - pxy[1]) == 0) || (Math.abs(gx - pxy[0]) == 0 && Math.abs(gy - pxy[1]) == 1); break; default: cmc = false; break; } } else //tried to move opponents { cmc = false; } return isT && cmc; } public boolean canCapture(Piece p1, Piece p2) { if (p1.getSide() != p2.getSide()) //our side { return true; } else { return false; } } public void capture(Piece p1, Piece p2) { int ind1 = Arrays.binarySearch(sideProgression, mySide); if (p1.getSide() != p2.getSide()) //our side { switch (p2.getpID()) { case PIECE_LINK: if (findEmpty(links[ind1]) != -1) links[ind1][findEmpty(links[ind1])] = p2.getSide(); break; case PIECE_VIRUS: if (findEmpty(viruses[ind1]) != -1) viruses[ind1][findEmpty(viruses[ind1])] = p2.getSide(); break; } } else { } } public int findEmpty(int[] n) { for (int i = 0; i < n.length; i++) { if (n[i] == 0) return i; } return -1; } /** * renders the game */ public void render() { Color.white.bind(); gBoard.draw(); // gBoard.drawUpsideDown(); float lx = pointToScale(440), vx = pointToScale(-2), py = pointToScale(934), py2 = pointToScale(4), pwidth = pointToScale(75), pheight = pointToScale(83), pbuff = pointToScale(-13); for (int i = 0; i < 4; i++) { //447, 934 link //5, 934 virus //67 1017 int ind1 = Arrays.binarySearch(sideProgression, SIDE_YELLOW); int ind2 = Arrays.binarySearch(sideProgression, SIDE_BLUE); if (links[ind1][i] != 0) { textureLinks[Arrays.binarySearch(sideProgression, links[ind1][i])].bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0, 0); GL11.glVertex2f(lx + (pwidth + pbuff) * i, py); GL11.glTexCoord2f(1, 0); GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py); GL11.glTexCoord2f(1, 1); GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py + pheight); GL11.glTexCoord2f(0, 1); GL11.glVertex2f(lx + (pwidth + pbuff) * i, py + pheight); GL11.glEnd(); } if (viruses[ind1][i] != 0) { textureViruses[Arrays.binarySearch(sideProgression, viruses[ind1][i])].bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0, 0); GL11.glVertex2f(vx + (pwidth + pbuff) * i, py); GL11.glTexCoord2f(1, 0); GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py); GL11.glTexCoord2f(1, 1); GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py + pheight); GL11.glTexCoord2f(0, 1); GL11.glVertex2f(vx + (pwidth + pbuff) * i, py + pheight); GL11.glEnd(); } if (links[ind2][i] != 0) { textureLinks[Arrays.binarySearch(sideProgression, links[ind2][i])].bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0, 1); GL11.glVertex2f(vx + (pwidth + pbuff) * i, py2); GL11.glTexCoord2f(1, 1); GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py2); GL11.glTexCoord2f(1, 0); GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py2 + pheight); GL11.glTexCoord2f(0, 0); GL11.glVertex2f(vx + (pwidth + pbuff) * i, py2 + pheight); GL11.glEnd(); } if (viruses[ind2][i] != 0) { textureViruses[Arrays.binarySearch(sideProgression, viruses[ind2][i])].bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0, 1); GL11.glVertex2f(lx + (pwidth + pbuff) * i, py2); GL11.glTexCoord2f(1, 1); GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py2); GL11.glTexCoord2f(1, 0); GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py2 + pheight); GL11.glTexCoord2f(0, 0); GL11.glVertex2f(lx + (pwidth + pbuff) * i, py2 + pheight); GL11.glEnd(); } //lx+=lx; } } /** * scales a point to the scale of the game window * @param n input coordinate * @return n,scaled to the window */ public static float pointToScale(float n) { return scale * n; } /* * Initialise the GL display * * @param width The width of the display * @param height The height of the display */ private void initGL(int width, int height, String title) { try { Display.setDisplayMode(new DisplayMode(width, height)); Display.setLocation(6, 7); Display.setTitle(title); Display.create(); Display.setVSyncEnabled(true); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glViewport(0, 0, width, height); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, width, height, 0, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); } }