62,620
社区成员
发帖
与我相关
我的任务
分享package p1;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class MyZiDan extends Thread{
JLabel image=new JLabel();
private int incY=2;
private boolean alive=true;
public static int stackSpeed=-1;
int q=00000;
MyZiDan(int x,int y){
//image.setLocation(x,y);
init(x,y);
}
private void init(int x,int y){
if(stackSpeed<0){
Icon lbl_plane = new ImageIcon(getClass().getResource("zidan.GIF"));
image.setIcon(lbl_plane); //同理,将子弹图标传给标签,以供之后显示在窗口内。
//image.setBounds(x, y, 20, 20);
image.setBounds(x-lbl_plane.getIconWidth()/2,
y-image.getHeight()-10, lbl_plane.getIconWidth(),lbl_plane.getIconHeight());//子弹出现的初始化位置,
//和大小,初始化位置主要依靠X Y飞机的坐标
Common.panel.add(image);
}
}
private void move(){ //子弹要怎么个动法。
//stackSpeed--;
image.setLocation(image.getX(),image.getY()-5);//将子弹组件移动到新位置,新位置为:横坐标不变,纵坐标为
//原来位置往上加一个高度,这个高度越大,意味着子弹的速度越大
if(image.getY()<=incY)
alive=false; //如果图片现在的纵坐标小于边框纵坐标,说明已经出去了,则不用再走了,否则一直往上走
isover();
}
public void run(){
if(stackSpeed<0){
stackSpeed=5;
while(alive) //只要子弹活着
{
move();//就按move方法让他走
try {
Thread.sleep(5); //子弹线程沉睡时间,也可以影响子弹速度
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
image.setBounds(-1,-1,-1,-1); //将子弹移除屏幕外
Common.panel.remove(image);
Common.panel.repaint();
}
//stackSpeed=-1;
//isover();
}
public void isover(){ //开始写死亡函数
/*if(image.getBounds().intersects(Common.myPlane.image.getBounds())){
Common.myPlane.setAlive(false);
alive=false;
}*/
int n1=Common.planes.size(); //N1为飞机大小,
//System.out.println(n);
for(int i=0;i<n1;i++){ //循环判断,只要接触到飞机任何一个地方
if(alive==true && image.getBounds().intersects(Common.planes.get(i).image.getBounds())){
//System.out.println(i);//判断image矩形边框和Common.planes.get(i).image矩形边框是否交叉
//即判断我方子弹和敌方飞机是否矩形边框交叉
Common.planes.get(i).setAlive(false);//交叉为真则赋值flase给alive,飞机消失
this.alive=false;
++q;
Common.myPlane.image2.setText(q +"分");
}
}
int n2=Common.planes_zidan.size(); //N2为地方飞机子弹大小
//System.out.println(n); //循环判断,只要接触到子弹任何一个地方
for(int i=0;i<n2;i++){ ////if(image.getBounds().intersects(Common.planes_zidan.get(i).image.getBounds()) )&& alive==true
if(image.getBounds().intersects(Common.planes_zidan.get(i).image.getBounds()) ){
//System.out.println(i);
//判断我方子弹敌方子弹是否矩形边框交叉
Common.planes_zidan.get(i).setAlive(false);// 打到之后子弹死亡,实现子弹相遇消失的效果
this.alive=false;
++q;Common.myPlane.image2.setText(q +"分");
}
}
}
}