package games.rocket; import java.awt.*; import java.awt.event.*; import java.applet.*; public class RocketApplet extends Applet implements Runnable, MouseMotionListener, MouseListener { // game variables protected int sleepInt=50; protected boolean inGame=false; protected boolean newLevel=false; protected boolean gameOver=false; protected int level=1; protected int score=0; protected int highScore=50; protected Thread myThread; protected int width=0; protected int height=0; protected int roundIteration=0; protected Font font = new Font("helvetica",Font.BOLD,12); // player ship variables protected int shipLife=5; protected int shipX=200; protected int shipY=50; protected Point shipFireFrom=new Point(); protected Point[] shipRocket; protected Point[] shipRocketExplode; protected double[] shipRocketDistance; protected int[] shipRocketExplodeTime; protected int shipRockets=5; protected int shipRocketSpeed=20; protected int shipRocketExplodeDuration=20; protected int shipRocketExplodeSize=25; protected int shipRocketWidth=8; protected int shipRocketHeight=8; protected Polygon ship; protected boolean shipHit=false; // enemy rocket variables protected int enemyRockets = 0; protected int enemyRocketsMax = 10; protected int enemyRocketsMaxSpeed = 5; protected int enemyRocketsLeftToLaunch; protected double[] enemyRocketSpeed; protected double[] enemyRocketX; protected double[] enemyRocketY; protected int[] enemyRocketAngle; protected int[] enemyRocketDelay; protected boolean[] enemyRocketShowing; // bonus variables protected double shipRocketSpeedBonusTargetX, shipRocketSpeedBonusTargetY; protected double shipRocketExplodeSizeBonusTargetX, shipRocketExplodeSizeBonusTargetY; protected double pointsBonusTargetX, pointsBonusTargetY; protected int shipRocketSpeedBonusStartTime=0; protected int shipRocketExplodeSizeBonusStartTime=0; protected int pointsBonusStartTime=0; protected int bonusTargetSize = 15; protected int shipRocketSpeedBonus=0; protected int shipRocketExplodeSizeBonus=0; protected static final double BONUSDURATION = 700; // angle distance variables protected double[] xDistance = new double[360]; protected double[] yDistance = new double[360]; // color variables protected Color[] explodeColor = {new Color(105,24,20), new Color(112,26,22), new Color(120,28,23), new Color(128,30,25), new Color(136,32,26), new Color(144,34,28), new Color(151,35,29), new Color(159,37,31), new Color(167,39,33), new Color(175,41,34), new Color(183,43,35)}; public void init () { setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); setFont(font); width=getSize().width; height=getSize().height; setBackground(Color.black); // work out all possible angles to save running calculations for (int angle=0; angle<90; angle++) { double tempRad=0; tempRad = 0.01745329*angle; xDistance[angle] = Math.sin(tempRad); yDistance[angle] = Math.cos(tempRad)*-1; //System.out.println(angle+" X:"+xDistance[angle]+" Y:"+yDistance[angle]); } for (int angle=90; angle<360; angle++) { xDistance[angle] = yDistance[angle-90]*-1; yDistance[angle] = xDistance[angle-90]; //System.out.println(angle+" X:"+xDistance[angle]+" Y:"+yDistance[angle]); } initEnemyRockets(); initShip(); initBonus(); addMouseMotionListener(this); addMouseListener(this); } private void newGame() { // reset game variables enemyRocketsMax=10; enemyRocketsMaxSpeed=5; shipX=200; shipY=50; shipLife=5; level=1; // check highscore if (score > highScore) { highScore = score; } // start new game score=0; initEnemyRockets(); initShip(); initBonus(); gameOver=false; } private void newLevel() { // make ship bigger for new level shipX=(int)(shipX*1.1); shipY=(int)(shipY*1.05); // increase enemy rocket max speed enemyRocketsMax++; enemyRocketsMaxSpeed++; initEnemyRockets(); initShip(); initBonus(); level++; newLevel=false; } private void initShip() { // contructs the ship polygon based on the shipX and shipY values //1 //2 //3 //4 //5 //6 //7 //8 int[] polyX = { (int)(width/2 - shipX/2*0.8), (int)(width/2 + shipX/2*0.8), (int)(width/2 + shipX/2), (int)(width/2 + shipX*0.2), (int)(width/2 + shipX*0.2), (int)(width/2 - shipX*0.2), (int)(width/2 - shipX*0.2), (int)(width/2 - shipX/2)}; int[] polyY = { height , height , (int)(height - shipY*0.7), (int)(height - shipY*0.7), (int)(height - shipY), (int)(height - shipY), (int)(height - shipY*0.7), (int)(height - shipY*0.7)}; ship = new Polygon(polyX, polyY, 8); // where will the ships rockets start from shipFireFrom.x = width/2; shipFireFrom.y = height - shipY; // create fire array shipRocket = new Point[shipRockets]; shipRocketExplode = new Point[shipRockets]; shipRocketExplodeTime = new int[shipRockets]; shipRocketDistance = new double[shipRockets]; for (int i=0; i 0) { shipRocketSpeedBonus--; shipRocketSpeed=50; } else { shipRocketSpeed=20; } if (shipRocketExplodeSizeBonus > 0) { shipRocketExplodeSizeBonus--; shipRocketExplodeSize=35; } else { shipRocketExplodeSize=25; } } private void fireShip(int x, int y) { for (int i=0; i 0) { // check if tip of enemy rocket is closer to the point of explosion // than the radius of the ships rockets explosion double dist = Math.sqrt(Math.pow((shipRocketExplode[ship].x - enemyRocketX[enemy]), 2) + Math.pow((shipRocketExplode[ship].y - enemyRocketY[enemy]), 2)); if ((enemyRocketShowing[enemy]) && (dist < shipRocketExplodeSize / 2)) { // enemy rocket hit score = score + level; enemyRocketShowing[enemy] = false; //System.out.println("Shot down rocket:"+dist); } // also check if hit bonus double bonusDist1 = Math.sqrt(Math.pow((shipRocketExplode[ship].x - (shipRocketSpeedBonusTargetX+bonusTargetSize/2)), 2) + Math.pow((shipRocketExplode[ship].y - (shipRocketSpeedBonusTargetY+bonusTargetSize/2)), 2)); if (bonusDist1 < ((bonusTargetSize/2)+(shipRocketExplodeSize/2))) { // hit speed bonus shipRocketSpeedBonus = (int)BONUSDURATION; shipRocketSpeedBonusTargetX = 0 - bonusTargetSize; shipRocketSpeedBonusStartTime = 0; } double bonusDist2 = Math.sqrt(Math.pow((shipRocketExplode[ship].x - (shipRocketExplodeSizeBonusTargetX+bonusTargetSize/2)), 2) + Math.pow((shipRocketExplode[ship].y - (shipRocketExplodeSizeBonusTargetY+bonusTargetSize/2)), 2)); if (bonusDist2 < ((bonusTargetSize/2)+(shipRocketExplodeSize/2))) { // hit speed bonus shipRocketExplodeSizeBonus = (int)BONUSDURATION; shipRocketExplodeSizeBonusTargetX = 0 - bonusTargetSize; shipRocketExplodeSizeBonusStartTime = 0; } double bonusDist3 = Math.sqrt(Math.pow((shipRocketExplode[ship].x - (pointsBonusTargetX+bonusTargetSize/2)), 2) + Math.pow((shipRocketExplode[ship].y - (pointsBonusTargetY+bonusTargetSize/2)), 2)); if (bonusDist3 < ((bonusTargetSize/2)+(shipRocketExplodeSize/2))) { // hit speed bonus score = score + 50; pointsBonusTargetX = 0 - bonusTargetSize; pointsBonusStartTime = 0; } } } } } private void moveShipRockets() { double percChange = 0; for (int i=0; i 0) { // rocket already exploding shipRocketExplodeTime[i] --; if (shipRocketExplodeTime[i] == 0) { shipRocketExplode[i].x = 0; shipRocketExplode[i].y = 0; } } else { percChange = (shipRocketDistance[i]-shipRocketSpeed) / shipRocketDistance[i]; shipRocketDistance[i] = shipRocketDistance[i] - shipRocketSpeed; // move near to target shipRocket[i].x = (int)(shipRocketExplode[i].x - ((shipRocketExplode[i].x - shipRocket[i].x) * percChange)); shipRocket[i].y = (int)(shipRocketExplode[i].y - ((shipRocketExplode[i].y - shipRocket[i].y) * percChange)); if (shipRocketDistance[i] < 1) { // move near to target shipRocketExplodeTime[i] = shipRocketExplodeDuration; } } } } } private void moveEnemyRockets() { int tempLeft=0; int tempEnemy=0; for (int i=0; i width)) { // rocket has left side of screen enemyRocketShowing[i] = false; } if (enemyRocketY[i] > height) { // enemy has left bottom of screen enemyRocketShowing[i] = false; } if (enemyRocketY[i] == -10) { // not yet launched tempLeft++; } if ((enemyRocketShowing[i]) && (enemyRocketDelay[i] < roundIteration)) { tempEnemy++; enemyRocketX[i] = enemyRocketX[i] + (enemyRocketSpeed[i]*xDistance[enemyRocketAngle[i]]); enemyRocketY[i] = enemyRocketY[i] + (enemyRocketSpeed[i]*yDistance[enemyRocketAngle[i]]); } } enemyRocketsLeftToLaunch=tempLeft; if ((enemyRocketsLeftToLaunch == 0) && (tempEnemy == 0)) { //System.out.println("New level"); newLevel=true; } } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { if (gameOver) { newGame(); } else { if (newLevel) { newLevel(); inGame=true; } else { if (inGame) { // will fire ships rockets - store mouse coords fireShip(e.getX(), e.getY()); } else { inGame=true; } } } } public void mouseReleased(MouseEvent e) { } public void start() { if (myThread == null) { myThread = new Thread(this, "Rocket"); myThread.setPriority(Thread.MAX_PRIORITY); myThread.start(); } } public void stop() { if (myThread != null) { myThread = null; } } synchronized public void paint(Graphics g) { FontMetrics fm = g.getFontMetrics(font); g.setColor(Color.white); // add player ship if (shipHit) { g.setColor(Color.red); shipHit=false; } else { g.setColor(Color.darkGray); } g.fillPolygon(ship); g.setColor(Color.white); if (inGame) { // add bonus indicator and target g.setColor(Color.green); g.fillRect(0, height-(int)(50*(shipRocketSpeedBonus/BONUSDURATION)), 20, (int)(50*(shipRocketSpeedBonus/BONUSDURATION))); g.fillOval((int)shipRocketSpeedBonusTargetX, (int)shipRocketSpeedBonusTargetY, bonusTargetSize, bonusTargetSize); g.setColor(Color.blue); g.fillRect(20, height-(int)(50*(shipRocketExplodeSizeBonus/BONUSDURATION)), 20, (int)(50*(shipRocketExplodeSizeBonus/BONUSDURATION))); g.fillOval((int)shipRocketExplodeSizeBonusTargetX, (int)shipRocketExplodeSizeBonusTargetY, bonusTargetSize, bonusTargetSize); g.setColor(Color.yellow); g.fillOval((int)pointsBonusTargetX, (int)pointsBonusTargetY, bonusTargetSize, bonusTargetSize); // add enemy rockets g.setColor(Color.white); for (int i=0; i