import java.awt.GraphicsEnvironment; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.Color; import java.io.IOException; import javax.imageio.ImageIO; import gamegui.Button; import gamegui.Member; import gamegui.Label; import java.awt.Point; import java.awt.Toolkit; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import gamegui.Textbox; import java.awt.image.BufferedImage; import gamegui.Window; import java.awt.Cursor; import java.awt.Image; import java.awt.Font; import java.awt.Frame; import java.awt.DisplayMode; import java.awt.event.MouseListener; import java.awt.event.KeyListener; public class GalacticHeroes implements KeyListener, MouseListener { private static DisplayMode[] BEST_DISPLAY_MODES; Frame mainFrame; boolean done; GameState gameState; boolean firing; Font font12; Font font14; Font font20; Font font24; Image cursorImage; Cursor defaultCursor; Cursor blankCursor; Window wndMain; Window wndGame; Window wndLoad; Window wndInstruct; Window wndFame; Window wndCredits; Window wndWin; Window wndLose; BufferedImage fighter; BufferedImage bomber; BufferedImage xfighter; BufferedImage boss; BufferedImage player; BufferedImage fighterbullet; BufferedImage bomberbullet; BufferedImage bossbullet; BufferedImage bossminishot; BufferedImage playerbullet; HumanShip playerShip; Level[] levels; Level currentLevel; int levelNum; long timeLastFired; int points; Textbox selectedText; static { GalacticHeroes.BEST_DISPLAY_MODES = new DisplayMode[] { new DisplayMode(800, 600, 32, 0), new DisplayMode(800, 600, 16, 0), new DisplayMode(800, 600, 8, 0) }; } public GalacticHeroes(final GraphicsDevice device) { Label_0173: { try { final GraphicsConfiguration gc = device.getDefaultConfiguration(); (this.mainFrame = new Frame(gc)).setUndecorated(true); this.mainFrame.setIgnoreRepaint(true); device.setFullScreenWindow(this.mainFrame); if (device.isDisplayChangeSupported()) { chooseBestDisplayMode(device); } this.mainFrame.addMouseListener(this); this.mainFrame.addKeyListener(this); this.mainFrame.createBufferStrategy(2); final BufferStrategy bufferStrategy = this.mainFrame.getBufferStrategy(); this.done = false; this.gameState = GameState.MAIN; this.firing = false; this.initGUIElements(); this.initGameElements(); while (!this.done) { final Graphics g = bufferStrategy.getDrawGraphics(); this.render(g); g.dispose(); bufferStrategy.show(); } } catch (Exception e) { e.printStackTrace(); break Label_0173; } finally { device.setFullScreenWindow(null); } device.setFullScreenWindow(null); return; } device.setFullScreenWindow(null); } private void initGUIElements() { this.font12 = new Font("Arial", 0, 12); this.font14 = new Font("Arial", 0, 14); this.font20 = new Font("Arial", 0, 20); this.font24 = new Font("Arial", 0, 24); this.defaultCursor = this.mainFrame.getCursor(); final Image cursorImage = Toolkit.getDefaultToolkit().getImage(""); this.blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), ""); (this.wndMain = new Window("main", 0, 0, 800, 600, true)).add(new Label("title", 250, 65, 300, 20, "Galactic Heroes", this.font24, true)); this.wndMain.add(new Button("new game", 300, 170, 200, 40, "New Game", this.font12)); this.wndMain.add(new Button("load game", 300, 230, 200, 40, "Load Game", this.font12)); this.wndMain.add(new Button("instructions", 300, 290, 200, 40, "Instructions", this.font12)); this.wndMain.add(new Button("hall of fame", 300, 350, 200, 40, "Hall of Fame", this.font12)); this.wndMain.add(new Button("credits", 300, 410, 200, 40, "Credits", this.font12)); this.wndMain.add(new Button("quit", 300, 470, 200, 40, "Quit", this.font12)); this.wndGame = new Window("game", 0, 0, 800, 600, true); (this.wndLoad = new Window("load", 0, 0, 800, 600, true)).add(new Label("title", 250, 65, 300, 20, "Load Game", this.font20, true)); this.wndLoad.add(new Button("back", 340, 520, 120, 30, "Back", this.font12)); (this.wndInstruct = new Window("instruct", 0, 0, 800, 600, true)).add(new Label("title", 250, 65, 300, 20, "Instructions", this.font20, true)); this.wndInstruct.add(new Button("back", 340, 520, 120, 30, "Back", this.font12)); (this.wndFame = new Window("fame", 0, 0, 800, 600, true)).add(new Label("title", 250, 65, 300, 20, "Hall of Fame", this.font20, true)); this.wndFame.add(new Button("back", 340, 520, 120, 30, "Back", this.font12)); (this.wndCredits = new Window("credits", 0, 0, 800, 600, true)).add(new Label("title", 250, 65, 300, 20, "Credits", this.font20, true)); this.wndCredits.add(new Label("lblDesign", 250, 140, 300, 20, "Design", this.font14, true)); this.wndCredits.add(new Label("design", 250, 160, 300, 20, "Dmitry Portnoy", this.font12, true)); this.wndCredits.add(new Label("lblProgramming", 250, 220, 300, 20, "Programming", this.font14, true)); this.wndCredits.add(new Label("programming", 250, 240, 300, 20, "Dmitry Portnoy", this.font12, true)); this.wndCredits.add(new Label("lblArtwork", 250, 300, 300, 20, "Artwork", this.font14, true)); this.wndCredits.add(new Label("artwork", 250, 320, 300, 20, "Dmitry Portnoy", this.font12, true)); this.wndCredits.add(new Button("back", 340, 520, 120, 30, "Back", this.font12)); } private void initGameElements() { try { this.fighter = ImageIO.read(this.getClass().getResource("compfighter.png")); this.bomber = ImageIO.read(this.getClass().getResource("compbomber.png")); this.xfighter = ImageIO.read(this.getClass().getResource("compxfighter.png")); this.boss = ImageIO.read(this.getClass().getResource("compboss.png")); this.player = ImageIO.read(this.getClass().getResource("ship.png")); this.fighterbullet = ImageIO.read(this.getClass().getResource("compbullet.png")); this.bomberbullet = ImageIO.read(this.getClass().getResource("compbomberbullet.png")); this.bossbullet = ImageIO.read(this.getClass().getResource("compbossbullet.png")); this.bossminishot = ImageIO.read(this.getClass().getResource("compbossminishot.png")); this.playerbullet = ImageIO.read(this.getClass().getResource("bullet.png")); this.playerShip = new HumanShip(400, 500, 200, 50, 1, -400, 200, this.player, this.playerbullet); this.levels = new Level[26]; for (int x = 1; x < this.levels.length; ++x) { this.levels[x] = new Level(); } final Ship compBoss1 = new Ship(400, 75, 100, 20, 680, 5, 400, 1000, this.boss, this.bossbullet); compBoss1.addShotType(new ShotType(-36, 25, 1, 500, 500, this.bossminishot)); compBoss1.addShotType(new ShotType(36, 25, 1, 500, 500, this.bossminishot)); final Ship compBoss2 = new Ship(400, 75, 100, 20, 680, 5, 400, 1000, this.boss, this.bossbullet); compBoss2.addShotType(new ShotType(-36, 25, 1, 500, 500, this.bossminishot)); compBoss2.addShotType(new ShotType(36, 25, 1, 500, 500, this.bossminishot)); final Ship compBoss3 = new Ship(400, 75, 100, 20, 680, 5, 400, 1000, this.boss, this.bossbullet); compBoss3.addShotType(new ShotType(-36, 25, 1, 500, 500, this.bossminishot)); compBoss3.addShotType(new ShotType(36, 25, 1, 500, 500, this.bossminishot)); final Ship compBoss4 = new Ship(400, 75, 100, 20, 680, 5, 400, 1000, this.boss, this.bossbullet); compBoss4.addShotType(new ShotType(-36, 25, 1, 500, 500, this.bossminishot)); compBoss4.addShotType(new ShotType(36, 25, 1, 500, 500, this.bossminishot)); final Ship compBoss5 = new Ship(400, 75, 100, 20, 680, 5, 400, 1000, this.boss, this.bossbullet); compBoss5.addShotType(new ShotType(-36, 25, 1, 500, 500, this.bossminishot)); compBoss5.addShotType(new ShotType(36, 25, 1, 500, 500, this.bossminishot)); this.levels[1].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[1].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[1].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[1].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[1].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[2].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[2].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[2].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[2].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[2].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[3].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[3].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[3].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[3].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[3].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[3].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[3].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[3].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[3].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[4].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[4].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[4].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[4].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[4].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[4].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[4].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[4].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[4].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[5].add(compBoss1); this.levels[5].add(new Ship(200, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[5].add(new Ship(300, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[5].add(new Ship(400, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[5].add(new Ship(500, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[5].add(new Ship(600, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[5].add(new Ship(250, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[5].add(new Ship(350, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[5].add(new Ship(450, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[5].add(new Ship(550, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[6].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[6].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[6].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[6].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[6].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[7].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[7].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[7].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[7].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[7].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[8].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[8].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[8].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[8].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[8].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[8].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[8].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[8].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[8].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[9].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[9].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[9].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[9].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[9].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[9].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[9].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[9].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[9].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[10].add(compBoss2); this.levels[10].add(new Ship(200, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[10].add(new Ship(300, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[10].add(new Ship(400, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[10].add(new Ship(500, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[10].add(new Ship(600, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[10].add(new Ship(250, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[10].add(new Ship(350, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[10].add(new Ship(450, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[10].add(new Ship(550, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[11].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[11].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[11].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[11].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[11].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[12].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[12].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[12].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[12].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[12].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[13].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[13].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[13].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[13].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[13].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[13].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[13].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[13].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[13].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[14].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[14].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[14].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[14].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[14].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[14].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[14].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[14].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[14].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[15].add(compBoss3); this.levels[15].add(new Ship(200, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[15].add(new Ship(300, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[15].add(new Ship(400, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[15].add(new Ship(500, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[15].add(new Ship(600, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[15].add(new Ship(250, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[15].add(new Ship(350, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[15].add(new Ship(450, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[15].add(new Ship(550, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[16].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[16].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[16].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[16].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[16].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[17].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[17].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[17].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[17].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[17].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[18].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[18].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[18].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[18].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[18].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[18].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[18].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[18].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[18].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[19].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[19].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[19].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[19].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[19].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[19].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[19].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[19].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[19].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[20].add(compBoss4); this.levels[20].add(new Ship(200, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[20].add(new Ship(300, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[20].add(new Ship(400, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[20].add(new Ship(500, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[20].add(new Ship(600, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[20].add(new Ship(250, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[20].add(new Ship(350, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[20].add(new Ship(450, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[20].add(new Ship(550, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[21].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[21].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[21].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[21].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[21].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[22].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[22].add(new Ship(300, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[22].add(new Ship(400, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[22].add(new Ship(500, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[22].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[23].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[23].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[23].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[23].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[23].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[23].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[23].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[23].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[23].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[24].add(new Ship(200, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[24].add(new Ship(300, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[24].add(new Ship(400, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[24].add(new Ship(500, 100, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[24].add(new Ship(600, 100, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[24].add(new Ship(250, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[24].add(new Ship(350, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[24].add(new Ship(450, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[24].add(new Ship(550, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[25].add(compBoss5); this.levels[25].add(new Ship(200, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[25].add(new Ship(300, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[25].add(new Ship(400, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[25].add(new Ship(500, 150, 100, 5, 360, 2, this.bomber, this.bomberbullet)); this.levels[25].add(new Ship(600, 150, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[25].add(new Ship(250, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[25].add(new Ship(350, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[25].add(new Ship(450, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); this.levels[25].add(new Ship(550, 200, 100, 2, 360, 1, this.fighter, this.fighterbullet)); (this.currentLevel = new Level()).copy(this.levels[1]); this.levelNum = 1; } catch (IOException ioe) { ioe.printStackTrace(); } } private void render(final Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, 800, 600); switch (this.gameState) { case MAIN: { this.drawMain(g); break; } case GAME: { this.drawGame(g); break; } case LOAD: { this.drawLoad(g); break; } case INSTRUCT: { this.drawInstruct(g); break; } case FAME: { this.drawFame(g); break; } case CREDITS: { this.drawCredits(g); break; } case WIN: { this.drawWin(g); break; } case LOSE: { this.drawLose(g); break; } } } private void drawMain(final Graphics g) { if (this.mainFrame.getCursor() == this.blankCursor) { this.mainFrame.setCursor(this.defaultCursor); } this.wndMain.draw(g); } private void drawGame(final Graphics g) { if (this.mainFrame.getCursor() == this.defaultCursor) { this.mainFrame.setCursor(this.blankCursor); } if (this.currentLevel.size() == 0) { this.currentLevel.copy(this.nextLevel()); this.playerShip.getShots().clear(); } if (this.firing) { this.playerShip.produceVolley(); } this.fireVolley(this.currentLevel); this.patrol(this.currentLevel); this.checkCollision(this.currentLevel); this.wndGame.draw(g); this.playerShip.draw(g); this.currentLevel.draw(g); g.setColor(Color.green); g.fillRect(790, 600 - this.playerShip.getHitpoints() * 10, 10, this.playerShip.getHitpoints() * 10); } private void drawLoad(final Graphics g) { if (this.mainFrame.getCursor() == this.blankCursor) { this.mainFrame.setCursor(this.defaultCursor); } this.wndLoad.draw(g); } private void drawInstruct(final Graphics g) { if (this.mainFrame.getCursor() == this.blankCursor) { this.mainFrame.setCursor(this.defaultCursor); } this.wndInstruct.draw(g); } private void drawFame(final Graphics g) { if (this.mainFrame.getCursor() == this.blankCursor) { this.mainFrame.setCursor(this.defaultCursor); } this.wndFame.draw(g); } private void drawCredits(final Graphics g) { if (this.mainFrame.getCursor() == this.blankCursor) { this.mainFrame.setCursor(this.defaultCursor); } this.wndCredits.draw(g); } private void drawWin(final Graphics g) { if (this.mainFrame.getCursor() == this.blankCursor) { this.mainFrame.setCursor(this.defaultCursor); } this.wndWin.draw(g); } private void drawLose(final Graphics g) { if (this.mainFrame.getCursor() == this.blankCursor) { this.mainFrame.setCursor(this.defaultCursor); } this.wndLose.draw(g); } private Level nextLevel() { ++this.levelNum; if (this.levelNum >= this.levels.length) { this.gameState = GameState.WIN; this.points += 360; (this.wndWin = new Window("win", 0, 0, 800, 600, true)).add(new Label("title", 250, 65, 300, 20, "Victory", this.font24, true)); this.wndWin.add(new Label("info", 250, 100, 300, 100, "Press ESC to return to the main menu", this.font14, true)); this.wndWin.add(new Label("score", 250, 200, 300, 100, "Score: " + this.points, new Font("Arial", 0, 14), true)); return this.levels[1]; } return this.levels[this.levelNum]; } private void fireVolley(final Level curLevel) { for (int x = 0; x < curLevel.size(); ++x) { ((Ship)curLevel.get(x)).produceVolley(); } } private void patrol(final Level curLevel) { for (int x = 0; x < curLevel.size(); ++x) { ((Ship)curLevel.get(x)).patrol(); } } private void checkCollision(final Level currentLevel) { Shot collidingShot = new Shot(); Ship collidingShip = new Ship(); Ship shotOwner = new Ship(); for (int x = 0; x < currentLevel.size(); ++x) { for (int y = 0; y < this.playerShip.getShots().size(); ++y) { if (((Ship)currentLevel.get(x)).collides((Shot)this.playerShip.getShots().get(y)) && ((Ship)currentLevel.get(x)).getHitpoints() > 0) { collidingShot = (Shot)this.playerShip.getShots().get(y); collidingShip = (Ship)currentLevel.get(x); } } } this.playerShip.getShots().remove(collidingShot); collidingShip.setHitpoints(collidingShip.getHitpoints() - collidingShot.getDamage()); if (collidingShip.getHitpoints() == 0) { this.points += collidingShip.getMaxHitpoints() * this.playerShip.getHitpoints(); } collidingShot = new Shot(); for (int x = 0; x < currentLevel.size(); ++x) { for (int y = 0; y < ((Ship)currentLevel.get(x)).getShots().size(); ++y) { if (this.playerShip.collides((Shot)((Ship)currentLevel.get(x)).getShots().get(y))) { collidingShot = (Shot)((Ship)currentLevel.get(x)).getShots().get(y); shotOwner = (Ship)currentLevel.get(x); } } } shotOwner.getShots().remove(collidingShot); this.playerShip.setHitpoints(this.playerShip.getHitpoints() - collidingShot.getDamage()); if (this.playerShip.getHitpoints() == 0) { this.gameState = GameState.LOSE; (this.wndLose = new Window("lose", 0, 0, 800, 600, true)).add(new Label("title", 250, 65, 300, 20, "Defeat", this.font24, true)); this.wndLose.add(new Label("info", 250, 100, 300, 100, "Press ESC to return to the main menu", this.font14, true)); this.wndLose.add(new Label("score", 250, 200, 300, 100, "Score: " + this.points, new Font("Arial", 0, 14), true)); } } private void reset() { this.playerShip.reset(); for (int x = 1; x < this.levels.length; ++x) { this.levels[x].reset(); } this.levelNum = 1; this.currentLevel.copy(this.levels[1]); } private static DisplayMode getBestDisplayMode(final GraphicsDevice device) { for (int x = 0; x < GalacticHeroes.BEST_DISPLAY_MODES.length; ++x) { final DisplayMode[] modes = device.getDisplayModes(); for (int i = 0; i < modes.length; ++i) { if (modes[i].getWidth() == GalacticHeroes.BEST_DISPLAY_MODES[x].getWidth() && modes[i].getHeight() == GalacticHeroes.BEST_DISPLAY_MODES[x].getHeight() && modes[i].getBitDepth() == GalacticHeroes.BEST_DISPLAY_MODES[x].getBitDepth()) { return GalacticHeroes.BEST_DISPLAY_MODES[x]; } } } return null; } public static void chooseBestDisplayMode(final GraphicsDevice device) { final DisplayMode best = getBestDisplayMode(device); if (best != null) { device.setDisplayMode(best); } } public void mousePressed(final MouseEvent e) { } public void mouseReleased(final MouseEvent e) { } public void mouseEntered(final MouseEvent e) { } public void mouseExited(final MouseEvent e) { } public void mouseClicked(final MouseEvent e) { switch (this.gameState) { case MAIN: { if (this.wndMain.getMember("new game").isClicked(e.getX(), e.getY())) { this.gameState = GameState.GAME; break; } if (this.wndMain.getMember("load game").isClicked(e.getX(), e.getY())) { this.gameState = GameState.LOAD; break; } if (this.wndMain.getMember("instructions").isClicked(e.getX(), e.getY())) { this.gameState = GameState.INSTRUCT; break; } if (this.wndMain.getMember("hall of fame").isClicked(e.getX(), e.getY())) { this.gameState = GameState.FAME; break; } if (this.wndMain.getMember("credits").isClicked(e.getX(), e.getY())) { this.gameState = GameState.CREDITS; break; } if (this.wndMain.getMember("quit").isClicked(e.getX(), e.getY())) { this.done = true; break; } break; } case LOAD: { if (this.wndLoad.getMember("back").isClicked(e.getX(), e.getY())) { this.gameState = GameState.MAIN; break; } break; } case INSTRUCT: { if (this.wndInstruct.getMember("back").isClicked(e.getX(), e.getY())) { this.gameState = GameState.MAIN; break; } break; } case FAME: { if (this.wndFame.getMember("back").isClicked(e.getX(), e.getY())) { this.gameState = GameState.MAIN; break; } break; } case CREDITS: { if (this.wndCredits.getMember("back").isClicked(e.getX(), e.getY())) { this.gameState = GameState.MAIN; break; } break; } } } public void keyTyped(final KeyEvent e) { } public void keyPressed(final KeyEvent e) { if (this.selectedText != null) { this.selectedText.handleEvent(e); } switch (this.gameState) { case GAME: { if (e.getKeyCode() == 38 || e.getKeyCode() == 40 || e.getKeyCode() == 37 || e.getKeyCode() == 39) { this.playerShip.handleEvent(e); } if (e.getKeyCode() == 32) { this.firing = true; } if (e.getKeyCode() == 27) { this.reset(); this.gameState = GameState.MAIN; break; } break; } case WIN: { if (e.getKeyCode() == 27) { this.reset(); this.gameState = GameState.MAIN; break; } break; } case LOSE: { if (e.getKeyCode() == 27) { this.reset(); this.gameState = GameState.MAIN; break; } break; } } } public void keyReleased(final KeyEvent e) { if (this.gameState == GameState.GAME) { switch (e.getKeyCode()) { case 38: { this.playerShip.setUpPressed(false); break; } case 40: { this.playerShip.setDownPressed(false); break; } case 37: { this.playerShip.setLeftPressed(false); break; } case 39: { this.playerShip.setRightPressed(false); break; } case 32: { this.firing = false; break; } } } } public static void main(final String[] args) { try { final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); final GraphicsDevice device = env.getDefaultScreenDevice(); final GalacticHeroes gameWindow = new GalacticHeroes(device); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } }