codice:package gioco.file; import gioco.file.*; import java.awt.Dimension; import java.awt.Point; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JLabel; /** * * @author Giancarlo Mennillo Classe V S */ public class Game2 extends javax.swing.JFrame { private class Aerei extends Thread { @Override public void run() { Energia.setValue(100); boolean control=true; while (control) { for (int j = 0; j < 7; j++) { if (nemici[j].getLocation().y >= 800) { nemici[j].setLocation(nemici[j].getLocation().x, -10); } nemici[j].setLocation(nemici[j].getLocation().x, nemici[j].getLocation().y + 1); Impattofuocomio(nemici[j]); if(Winner()) { win.setVisible(true); control=false; } } try { sleep(12);//velocità con cui scendono gli aerei } catch (InterruptedException ex) { } } } } private class Fuoco extends Thread { @Override // in questa classe gestiamo anke il fatto di lanciare il missile public void run() { fuoco.setLocation(fuoco.getLocation().x - 800, fuoco.getLocation().y); while(colpito(nemici,fuoco.getLocation())==false) { for (int i = 0; i < 800; i++) { fuoco.setVisible(true); /*if fuoco.getLocation().y >= 800) { fuoco].setLocation(fuoco.getLocation().x, -10); }*/ fuoco.setLocation(fuoco.getLocation().x, fuoco.getLocation().y +1); } try { sleep(12);//velocità fuoco } catch (InterruptedException ex) { } } fuoco.setVisible(false); addMouseListener(new Mouse()); } } public boolean colpito(JLabel pNemico[], Point fuocox) { boolean colpitos=false; for (int i = 0; i < 7; i++) { Point nemix = pNemico[i].getLocation(); Dimension fuocoD= this.fuoco.getSize(); Dimension nemicoD= pNemico[i].getSize(); Rectangle fuocoR= new Rectangle(fuocox, fuocoD); Rectangle nemicoR= new Rectangle(nemix, nemicoD); if (fuocoR.intersects(nemicoR) ==true) { colpitos = true; } } return colpitos; } private class Mouse extends MouseAdapter { @Override public void mouseMoved(MouseEvent e) { int x = e.getPoint().x; aereo.setLocation(x - aereo.getSize().height / 2,aereo.getLocation().y); fuoco.setLocation(x - fuoco.getSize().height / 2,fuoco.getLocation().y); } @Override public void mouseClicked(MouseEvent e) { Fuoco f = new Fuoco(); f.start(); } } public JLabel[] nemici; public boolean impatto(JLabel pNemico) { Dimension fuocoD= this.fuoco.getSize(); Dimension nemicoD= pNemico.getSize(); Point locFuoco= this.fuoco.getLocation(); Point locNemico= pNemico.getLocation(); Rectangle fuocoR= new Rectangle(locFuoco, fuocoD); Rectangle nemicoR= new Rectangle(locNemico, nemicoD); return fuocoR.intersects(nemicoR); } private void Impattofuocomio(JLabel pNemico) { if(this.impatto(pNemico)) { pNemico.setIcon(new ImageIcon(getClass().getResource("/gioco/file/images/scoppio1.gif"))); pNemico.setVisible(false); String k= lbScoreV.getText(); int x=Integer.parseInt(k); x=x+10; k=Integer.toString(x); lbScoreV.setText(k); } } private boolean Winner() { int cont=0; for (int j = 0; j < 7; j++) { if(!this.nemici[j].isVisible()) { cont++; } } return cont==7; } private void initLabel() { this.nemici = new JLabel[7]; for (int i = 0; i < 7; i++) { this.nemici[i] = new JLabel(new ImageIcon(getClass().getResource("/gioco/file/images/nemico.png"))); this.nemici[i].setVisible(true); this.nemici[i].setSize(85, 55); this.nemici[i].setLocation((i * 79), -80); this.jPanel1.add(this.nemici[i]); } }



Rispondi quotando