import java.applet.Applet; import java.applet.AudioClip; import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.awt.image.BufferedImage; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Random; import java.util.Scanner; import javax.imageio.ImageIO; import javax.swing.*; //2d array array[row][col] public class MissleCommand { private static final int WIDTH = 750; private static final int HEIGHT = 770; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ //invoke later public void run(){ createAndShowGUI(); } }); } private static void createAndShowGUI(){ JFrame frame = new theGame(WIDTH, HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class theGame extends JFrame implements MouseListener { /** * */ private static final long serialVersionUID = 1L; private static final int WIDTH = 750; private static final int HEIGHT = 770; BufferedImage image; static Graphics2D g2; //private final JFileChooser chooser; boolean firsttime=true; ImageIcon screen; JLabel thispanel; Container cp; static Rectangle[] homes; static Homes[] hm; static String score; int x[]; int y[]; Missile m[]; Thread missiles; Thread bullets; int numberofM=5; int sleepdelay; int counterofLevels=1; Timer timer; static int chkm=0; boolean paused; int pcount=0; int blowncount=0; boolean firstover=false,secondover=false; JTextArea highscore = new JTextArea(); public theGame(int w, int h){ paused=false; this.setTitle("Missile Command - Karina Alvarez"); this.setSize(w,h); image = new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_ARGB); g2 = image.createGraphics(); screen = createImageIcon("/images/homescreen.png",""); thispanel=new JLabel(screen); this.add(thispanel); addMenu(); addMouseListener(this); score="00000"; hm= new Homes[12]; for(int i =0;i=6){ j=i-6; hm[j].setBlown(true); } else{ hm[i].setBlown(true); } System.out.println("Home :"+ j+ " i:"+i); } } } //------------------------------------------------------------------------ public void shoot(Point p){ // final AudioClip shot =Applet.newAudioClip(getClass().getResource("/images/pop.wav")); // shot.play(); //System.out.println("I'm shooting1!"); g2.setColor(Color.CYAN); int ori_x = 685; int ori_y = 425; double dis_x= p.getX(); double dis_y= p.getY()-50; double y=0; float slope; double x_p[]= new double[500]; double y_p[]=new double[500]; slope=(float) ( (ori_y-dis_y)/(ori_x-dis_x)); int i=1; int px=0, pp=0,ppc=0,ppt=0; y_p[0]=ori_y; x_p[0]=ori_x; for(int x=ori_x;x>=dis_x;x=x-10){ x_p[i] =x; y_p[i] =ori_y+ (slope*(x_p[i]-ori_x)); i++; } // Missile bullet = new Missile(p); // x_p[0]=dis_x; // y_p[0]=dis_y; // for(int h=10;h<=60;h=h+5){ // bullet.drawMissile2(x_p, y_p, g2, 0, true, image,bullets,h); // this.repaint(); // //bullet.drawMissile2(x_p, y_p, g2, 1, true, image,bullets); // // } // for(int h=60;h>=10;h=h-5){ // bullet.drawMissile2(x_p, y_p, g2, 0, true, image,bullets,h); // this.repaint(); // //bullet.drawMissile2(x_p, y_p, g2, 1, true, image,bullets); // // } // bullet.drawMissile2(x_p, y_p, g2, 1, true, image,bullets,10); userBullet m = new userBullet(g2,p,x_p,y_p); new Thread(m).start(); } //----------------------------------------------------------------------- public void updateDiplayForNewLevel(int i){ //ystem.out.println("updating display for new level"); g2.setColor(Color.blue); g2.setColor(Color.black); g2.fillRect(0, 0, 800, 800); //draw the level out now //creating terrain g2.setColor(Color.yellow); g2.fillRect(0, 600, 750, 200); g2.fillOval(-30, 560, 100, 150); g2.fillOval(25, 560, 100, 200); g2.fillOval(70, 560, 100, 200); g2.fillOval(130, 580, 100, 150); g2.fillOval(170, 580, 100, 200); g2.fillOval(240, 560, 100, 150); g2.fillOval(370, 575, 60, 150); g2.fillOval(395, 570, 70, 120); g2.fillOval(420, 575, 60, 150); g2.fillOval(450, 560, 90, 100); g2.fillOval(560, 570, 70, 120); g2.fillOval(600, 575, 60, 150); g2.fillOval(620, 560, 90, 100); g2.setColor(Color.MAGENTA); g2.fillRect(650, 470, 80, 170); g2.fillOval(650,440,80,90); g2.setColor(Color.yellow); g2.fillOval(688, 430, 10, 20); for(int u=0;uscores[k]){ switch(k){ case 4: scores[k]=game_score; name =JOptionPane.showInputDialog("Enter Initials"); names[k]=name; break; case 3: scores[4]=scores[3]; names[4]=names[3]; scores[k]=game_score; name =JOptionPane.showInputDialog("Enter Initials"); names[k]=name; break; case 2: scores[4]=scores[3]; names[4]=names[3]; scores[3]=scores[2]; names[3]=names[2]; scores[k]=game_score; name =JOptionPane.showInputDialog("Enter Initials"); names[k]=name; break; case 1: scores[4]=scores[3]; names[4]=names[3]; scores[3]=scores[2]; names[3]=names[2]; scores[2]=scores[1]; names[2]=names[1]; scores[k]=game_score; name =JOptionPane.showInputDialog("Enter Initials"); names[k]=name; break; case 0: scores[4]=scores[3]; names[4]=names[3]; scores[3]=scores[2]; names[3]=names[2]; scores[2]=scores[1]; names[2]=names[1]; scores[1]=scores[0]; names[1]=names[0]; scores[k]=game_score; name =JOptionPane.showInputDialog("Enter Initials"); names[k]=name; break; } break; } else{ k++; } } try { hs=new FileWriter("src"+File.separator+"images"+File.separator+"highscore.txt"); } catch (IOException e) { e.printStackTrace(); } highscore.append(" HIGH SCORES: \n"); out = new BufferedWriter(hs); int p=0; while(p<5){ try { out.write(names[p]+" "+scores[p]+"\n"); highscore.append(" "+names[p]+" "+scores[p]+"\n"); p++; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //------------------------------------------------ @SuppressWarnings({ "deprecation", "deprecation" }) public void pauseGame(Thread t){ if(paused==true && pcount%2==0 ){ // System.out.println("in pause ==true"); t.resume(); paused=false; // System.out.println("attempting to resume f " +paused); this.setTitle("Missile Command - Karina Alvarez"); } else{ // System.out.println("in not paused..making paused"); //t.stop(); t.suspend(); // thisthread.suspend(); this.setTitle("GAME IS PAUSED"); pcount++; paused=true; // System.out.println("attempting to pause? t "+ paused); } } //------------------------------------------------------------------------ protected ImageIcon createImageIcon(String path, String description) { java.net.URL imgURL = getClass().getResource(path); if (imgURL != null) { return new ImageIcon(imgURL, description); } else { System.err.println("Couldn't find file: " + path); return null; } } } class Missile { double t1,nextX, nextY; Random rand = new Random(); float delta_w; double xa[],ya[]; Random rand2=new Random(); Random rand3=new Random(); int m_placement; Rectangle shoot = new Rectangle(); Thread thread; int collisiondetected[]; int counter=0; String score; boolean firsttime=true, ft=true; boolean collision =false; int presentscore; int missileshot_Count=0; Thread thisthread; // boolean paused=false; public Missile(Thread t){ m_placement = rand.nextInt(750);//each missile has a random start x position xa = new double[700]; ya = new double[700]; xa[0] = m_placement; ya[0] =20; collisiondetected=new int[5]; collisiondetected[0]=99; collisiondetected[1]=99; collisiondetected[2]=99; collisiondetected[3]=99; collisiondetected[4]=99; score="00000"; presentscore= Integer.parseInt(score); presentscore=0; thisthread=t; collision=false; firsttime=true; ft=true; } public Missile(Point p){ int xp=(int) p.getX(); int yp=(int)p.getY(); shoot = new Rectangle(xp-10,yp-10,50,50); } //------------------------------------------------------------ //computes the offset and draws the growth segment to the end of the new one public void moveMissile(Graphics2D g24, int step_num, BufferedImage image, Thread t, int missilenum, int sleepdelay ){ final Graphics2D g2=g24; boolean xissafe=true; final int m = step_num; int next = rand2.nextInt(3); g2.setColor(Color.WHITE); if(next==0){ m_placement =m_placement; xa[m+1]=m_placement; } else if(next==1){ m_placement =m_placement+10; xa[m+1]=m_placement; } else if(next ==2){ m_placement =m_placement-10; xa[m+1]=m_placement; } if(xa[m+1]<80 && ya[m+1]<60){ xa[m+1]=81; m_placement=81; } if(xa[m+1]<=0){ m_placement=5; xa[m+1]=m_placement; } if(xa[m+1]>=745){ m_placement=740; xa[m+1]=m_placement; } int p=0; for(int u =20;u<=700;u=u+4){ ya[p]=u; p++; } thread=t; drawMissile(xa,ya,g2,m,false,image, t, missilenum, sleepdelay); //}//end while }//end of moveMissile public void drawMissile(double x[], double y[], Graphics2D g2, int step_num, boolean which, BufferedImage image, Thread t,int missilenum, int sleepdelay ){ if(which ==false){ if(checksValuesofCollision(missilenum)){ //System.out.println("This missile has been blasted"); if(ft==true){ g2.setColor(Color.black); missileHit(x,y,g2); ft=false; } } else{ g2.setColor(Color.white); for(int j = 0; j0 && y[j+1]<750 && y[j+1]>0) && !checksValuesofCollision(missilenum)){ //System.out.println("Collision detected"); collisiondetected[counter]=missilenum; counter++; missileshot_Count++; missileHit(x,y, g2); setCOllided(true); updateScore(100,g2,0); //break; } else if( (image.getRGB((int)x[j+1], (int)y[j+1])==Color.blue.getRGB()) &&(x[j+1]<740 && x[j+1]>0 && y[j+1]<750 && y[j+1]>0) && !checksValuesofCollision(missilenum)){ Point p = new Point((int)x[j+1],(int)y[j+1]); collisiondetected[counter]=missilenum; counter++; missileHit(x,y,g2); setCOllided(true); missileshot_Count++; updateScore(0,g2,1); theGame.baseHit(p); // final AudioClip explode =Applet.newAudioClip(getClass().getResource("/images/explosion.wav")); // explode.play(); explosionClip m= new explosionClip(); new Thread(m).start(); } else{ Line2D line = new Line2D.Double(); if(y[j]<=15 ||y[j+1]<=15){} else{ line.setLine(x[j], y[j],x[j+1],y[j+1] ); g2.setColor(Color.white); g2.draw(line); } } } //enf for loop } //end else }//end if hich == falsae else if (which==true){ //System.out.println("BULLET"); int size=5; for(int h=0;h<6;h++){ // if(h==5) { // g2.setColor(Color.black); // } // else{ g2.setColor(Color.ORANGE); // g2.drawLine((int)x[h], (int)y[h], (int)x[h+1], (int)y[h+1]); g2.fillOval((int)x[0], (int)y[0], size, size); // } size+=10; } } try{thisthread.sleep(sleepdelay);} catch(InterruptedException e){} } // end of drawMIssile public void drawMissile2(final double x[],final double y[], final Graphics2D g2, final int step_num, boolean which, BufferedImage image, Thread b, final int size ){ //int size=70; // for(int h=0;h<6;h++){ // if(h==5) { // g2.setColor(Color.black); // } // else{ final Runnable bullet = new Runnable(){ public void run(){ if(step_num==0){ g2.setColor(Color.BLACK); g2.fillOval((int)x[0], (int)y[0], size+5, size+5); g2.setColor(Color.ORANGE); // g2.drawLine((int)x[h], (int)y[h], (int)x[h+1], (int)y[h+1]); g2.fillOval((int)x[0], (int)y[0], size, size); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if(step_num==1){ g2.setColor(Color.BLACK); // g2.drawLine((int)x[h], (int)y[h], (int)x[h+1], (int)y[h+1]); g2.fillOval((int)x[0], (int)y[0], size, size); } } }; new Thread(bullet).start(); // } // size+=10; // // // } // try{b.sleep(100);} // catch(InterruptedException e){} } //---------------------------------------------------------------------- public void missileHit(double x[],double y[], Graphics2D g2){ //add code that "eliminates" ballistic missile shot at explosionMClip m = new explosionMClip(); new Thread(m).start(); if(firsttime==true){ firsttime=false; } Color bkgrd; for(int j = 0; j580){ bkgrd = Color.yellow; } else{ bkgrd=Color.black; } g2.setColor(bkgrd); Line2D line = new Line2D.Double(); if(y[j]<=15 ||y[j+1]<=15){} else{ line.setLine(x[j], y[j],x[j+1],y[j+1] ); g2.draw(line); } } } //------------------------------------------------ public boolean checksValuesofCollision(int chk){ boolean answer=false; for(int i=0;i580){ bkgrd = Color.yellow; } else{ bkgrd=Color.black; } while(growing){ for(int h=10;h<=60;h=h+5){ g2.setColor(Color.ORANGE); // g2.drawLine((int)x[h], (int)y[h], (int)x[h+1], (int)y[h+1]); g2.fillOval(x, y, h, h); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } if(h==60){ growing=false; } } } while(shrinking){ for(int h=55;h>=10;h=h-5){ g2.setColor(bkgrd); g2.fillOval(x, y, h+5, h+5); g2.setColor(Color.ORANGE); // g2.drawLine((int)x[h], (int)y[h], (int)x[h+1], (int)y[h+1]); g2.fillOval(x, y, h, h); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } if(h==10){ shrinking=false; } } } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } g2.setColor(bkgrd); g2.fillOval(x, y, 10, 10); } //end run } //######################################################################### class levelOverLabel implements Runnable { Graphics2D g2; int level; // // The run() method will be invoked when the thread of this runnable object // is started. // levelOverLabel(Graphics2D g, int lvl){ g2=g; level=lvl; } public void run() { Font f = new Font(Font.MONOSPACED,Font.BOLD,25); g2.setFont(f); g2.setColor(Color.CYAN); g2.drawString("Level "+level, 700/2,375); try { Thread.sleep(2000); } catch (InterruptedException e) { } g2.setColor(Color.BLACK); g2.drawString("Level "+level, 700/2,375); g2.setColor(Color.BLACK); } //end run } //####################################################################### class peopleScared implements Runnable { Graphics2D g2; Homes hm; int x,y; // // The run() method will be invoked when the thread of this runnable object // is started. // peopleScared(Graphics2D g, Homes h){ g2=g; hm=h; x=hm.width+(hm.width/2); y=hm.height+(hm.height/2); } public void run() { g2.setColor(Color.BLACK); g2.drawString("Oh No!",x+5,y+5 ); g2.fillOval(x, y, 6, 6); try { Thread.sleep(2000); } catch (InterruptedException e) { } // g2.setColor(Color.yellow); // g2.drawString("Oh No!",x+5,y+5 ); } //end run } //#########################################################################3 class sirenClip implements Runnable { // Graphics2D g2; // Homes hm; // int x,y; // // The run() method will be invoked when the thread of this runnable object // is started. // sirenClip(){ } public void run() { final AudioClip airraid =Applet.newAudioClip(getClass().getResource("/images/siren.wav")); airraid.play(); } //end run } //#########################################################################3 class explosionClip implements Runnable { // Graphics2D g2; // Homes hm; // int x,y; // // The run() method will be invoked when the thread of this runnable object // is started. // explosionClip(){ } public void run() { final AudioClip explode =Applet.newAudioClip(getClass().getResource("/images/explosion.wav")); explode.play(); } //end run } //#########################################################################3 class explosionMClip implements Runnable { // Graphics2D g2; // Homes hm; // int x,y; // // The run() method will be invoked when the thread of this runnable object // is started. // explosionMClip(){ } public void run() { final AudioClip explode =Applet.newAudioClip(getClass().getResource("/images/explosion6.wav")); explode.play(); } //end run }