📄 planegame.java
字号:
picLine.setLocation(100,800);
this.stop();
}
}
//敌机一般子弹的线程
class FireEClass extends Thread
{
JLabel picP; //我方飞机
JLabel picPE; //发射子弹的敌方飞机
JLabel picBE; //敌机子弹
public FireEClass(JLabel pPE)
{
picP=picPlane;
picPE=pPE;
picBE = new JLabel(imgBullet[0]);
picBE.setBounds(picPE.getX(),picPE.getY()+50,100,100);
pnl.add(picBE);
picBE.setLocation(picBE.getX(),picBE.getY()-5);
new Thread(this).start();
}
public void run()
{
picBE.setVisible(true);
try
{
this.sleep(100);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
while(picBE.getY()<710&&picBE.isVisible()==true&&picPlane.isVisible()==true&&picBE.getY()<count.saveline-60)
{
picBE.setLocation(picBE.getX(),picBE.getY()+10);//敌机子弹向前移动
//检查我方飞机是否被击中
//判断我方飞机的位置跟敌方子弹的位置是否符合,如果符合则表示我方飞机被击中
if(picBE.getX()>(picP.getX()-30) && picBE.getX()<(picP.getX()+30) && picBE.getY()>(picP.getY()-50) && picBE.getY()<(picP.getY()+20))
{
if(count.lbllife>0)
{
count.lbllife--;
System.out.println("还有生命"+count.lbllife);
lbllife.setText("生命:"+count.lbllife);
}
else
{
picP.setVisible(false);
picP.setLocation(-100,-100);
picPlane.setVisible(false);
if(picPlane!=null)
{
picPlane=null;
}
new GameOver(); //游戏结束时出现排行榜
}
//通过图片的更替显示,显示爆炸效果
for(int i=1;i<16;i++)
{
picBE.setIcon(imgBullet[i]);
try
{
this.sleep(200);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
picBE.setVisible(false);
picBE.setLocation(-100,-100);
this.stop();
}
try
{
this.sleep(50);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
//如果子弹到达400这个底线,则不显示,并杀死子弹线程
picBE.setVisible(false);
picBE.setLocation(-100,-100);
this.stop();
}
}
//敌机跟踪弹的线程
class genzongEClass extends Thread
{
JLabel picP; //我方飞机
JLabel picPE; //发射子弹的敌方飞机
JLabel picBE; //敌机子弹
public genzongEClass(JLabel pPE)
{
picP=picPlane;
picPE=pPE;
picBE = new JLabel(genzongEBullet);
picBE.setBounds(picPE.getX(),picPE.getY()+50,100,100);
pnl.add(picBE);
picBE.setLocation(picBE.getX(),picBE.getY()-5);
new Thread(this).start();
}
public void run()
{
picBE.setVisible(true);
try
{
this.sleep(100);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
while(picBE.getY()<710&&picBE.isVisible()==true&&picPlane.isVisible()==true&&picBE.getX()>-10&&picBE.getY()<count.saveline-60)
{
if(picBE.getY()<picPlane.getY())
{
if(picBE.getX()>picPlane.getX())
{
picBE.setLocation(picBE.getX()-2,picBE.getY()+4);
}
else if(picBE.getX()<picPlane.getX())
{
picBE.setLocation(picBE.getX()+2,picBE.getY()+4);
}
else if(picBE.getX()==picPlane.getX())
{
picBE.setLocation(picBE.getX(),picBE.getY()+4);
}
}
else
{
picBE.setLocation(picBE.getX(),picBE.getY()+4);
}
//检查我方飞机是否被击中
//判断我方飞机的位置跟敌方子弹的位置是否符合,如果符合则表示我方飞机被击中
if(picBE.getX()>(picP.getX()-30) && picBE.getX()<(picP.getX()+30) && picBE.getY()>(picP.getY()-50) && picBE.getY()<(picP.getY()+20))
{
if(count.lbllife>0)
{
count.lbllife--;
System.out.println("还有生命"+count.lbllife);
lbllife.setText("生命:"+count.lbllife);
}
else
{
picP.setVisible(false);
picP.setLocation(-100,-100);
picPlane.setVisible(false);
if(picPlane!=null)
{
picPlane=null;
}
new GameOver(); //游戏结束时出现排行榜
this.stop();
}
//通过图片的更替显示,显示爆炸效果
for(int i=1;i<16;i++)
{
picBE.setIcon(imgBullet[i]);
try
{
this.sleep(200);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
picBE.setVisible(false);
picBE.setLocation(-40,-40);
this.stop();
}
try
{
this.sleep(50);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
//如果子弹到达400这个底线,则不显示,并杀死子弹线程
picBE.setVisible(false);
picBE.setLocation(-40,-40);
this.stop();
}
}
//敌机1的运动线程
class PlaneEClass1 extends Thread //左向右
{
JLabel picPE; //用于显示敌机,记录敌机位置
Icon imgPE;
Thread planeEThread; //敌机的线程
int intSleepTime; //用于控制敌机子弹的运动速度
EPlaneInfor planeEInfo;
public PlaneEClass1(int s)
{
intSleepTime=s;
imgPE = new ImageIcon(getClass().getResource("plane\\eplane2.gif"));
picPE = new JLabel(imgPE);
picPE.setBounds(0,0,100,100); //敌机位置的初始设置
pnl.add(picPE);
picPE.setLocation(picPE.getX()-10,picPE.getY()-10);
planeEThread=new Thread(this,"p1");
planeEInfo=new EPlaneInfor(planeEThread,picPE);
planeEVector.addElement(planeEInfo);//把敌机信息对象添加到Vector中去
planeEThread.start(); //启动敌机线程
}
public void run()
{
Random way=new Random(1230L); //随机取得敌机运动方向
Random fireSpeed=new Random(123450L); //用于控制敌机子弹的运动速度
int intWay;
int intFirePlace=0;
int intFireSpeed=fireSpeed.nextInt(4);
if(intFireSpeed==0) //放置出错,将intFireSpeed设为5;
{
intFireSpeed=5;
}
while(planeEThread==Thread.currentThread())
{
//敌机随机发射子弹
try//随机取得敌机的运动方向
{
picPE.setLocation(picPE.getX()+2,picPE.getY()+3);
this.sleep(intSleepTime);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
if(picPE.getX()<-40||picPE.getX()>600||picPE.getY()<-40||picPE.getY()>700)
{
picPE.setVisible(false);
planeEVector.removeElement(planeEInfo);
this.stop();
}
if(intFirePlace>(intFireSpeed*300+intSleepTime*5))
{
new genzongEClass(picPE);
intFirePlace=0;
count.saveline=710;
}
else//如果敌机靠近我方飞机,则加快发射子弹
{
if(picPE.getX()>picPlane.getX()-60 && picPE.getX()<picPlane.getX()+60)
{
intFirePlace+=70;
}
else
{
intFirePlace+=20;
}
}
//如果两架相撞则都爆炸,如果我方飞机还存在生命,则只显示爆炸效果
if(picPE.getX()>(picPlane.getX()-30) && picPE.getX()<(picPlane.getX()+30) && picPE.getY()>(picPlane.getY()-55) && picPE.getY()<(picPlane.getY()+55)||count.wincontral==true)
{
if(count.lbllife>0)
{
count.lbllife--;
System.out.println("还有生命"+count.lbllife);
lbllife.setText("生命:"+count.lbllife);
}
else
{
picPlane.setVisible(false);
picPlane.setLocation(-100,-100);
new GameOver();
}
for(int i=1;i<16;i++)
{
picPE.setIcon(imgBullet[i]);
try
{
this.sleep(200);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
picPE.setVisible(false);
picPE.setLocation(-100,-100);
planeEVector.removeElement(planeEInfo);
this.stop();
}
try
{
this.sleep(50);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
}
}
//敌机2的运动线程
class PlaneEClass2 extends Thread //右向左
{
JLabel picPE; //用于显示敌机,记录敌机位置
Icon imgPE;
Thread planeEThread; //敌机的线程
int intSleepTime; //用于控制敌机子弹的运动速度
EPlaneInfor planeEInfo;
public PlaneEClass2(int s)
{
intSleepTime=s;
imgPE = new ImageIcon("plane\\eplane1.gif");
picPE = new JLabel(imgPE);
picPE.setBounds(0,0,100,100); //敌机位置的初始设置
pnl.add(picPE);
picPE.setLocation(picPE.getX()+500,picPE.getY());
planeEThread=new Thread(this,"p1");
planeEInfo=new EPlaneInfor(planeEThread,picPE);
planeEVector.addElement(planeEInfo); //把敌机信息对象添加到Vector中去
planeEThread.start(); //启动敌机线程
}
public void run()
{
Random fireSpeed=new Random(123450L); //用于控制敌机子弹的运动速度
int intFirePlace=0;
int intFireSpeed=fireSpeed.nextInt(4);
if(intFireSpeed==0) //放置出错
{
intFireSpeed=5;
}
while(planeEThread==Thread.currentThread())
{
try //随机取得敌机的运动方向
{
picPE.setLocation(picPE.getX()-4,picPE.getY()+5);
this.sleep(intSleepTime);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
if(picPE.getX()<-40||picPE.getX()>600||picPE.getY()<-40||picPE.getY()>700)
{
picPE.setVisible(false);
picPE.setLocation(-100,-100);
planeEVector.removeElement(planeEInfo);
this.stop();
}
//敌机随机发射子弹
if(intFirePlace>(intFireSpeed*300+intSleepTime*5))
{
new FireEClass(picPE);
intFirePlace=0;
count.saveline=710;
}
else//如果敌机靠近我方飞机,则加快发射子弹
{
if(picPE.getX()>picPlane.getX()-60 && picPE.getX()<picPlane.getX()+60)
{
intFirePlace+=70;
}
else
{
intFirePlace+=20;
}
}
//如果两架相撞则都爆炸,如果我方飞机还存在生命,则只显示爆炸效果
if(picPE.getX()>(picPlane.getX()-30) && picPE.getX()<(picPlane.getX()+30) && picPE.getY()>(picPlane.getY()-55) && picPE.getY()<(picPlane.getY()+55)||count.wincontral==true)
{
if(count.lbllife>0)
{
count.lbllife--;
System.out.println("还有生命"+count.lbllife);
lbllife.setText("生命:"+count.lbllife);
}
else
{
picPlane.setVisible(false);
picPlane.setLocation(-100,-100);
new GameOver();
this.stop();
}
for(int i=1;i<16;i++)
{
picPE.setIcon(imgBullet[i]);
try
{
this.sleep(200);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
picPE.setVisible(false);
picPE.setLocation(-100,-100);
planeEVector.removeElement(planeEInfo);
this.stop();
}
try
{
this.sleep(50);
}
catch(InterruptedException e)
{
System.out.println("Sleep Error!! :"+e);
}
}
}
}
//敌机3的运动线程 ,智能机
class PlaneEClass3 extends Thread //左向下
{
JLabel picPE; //用于显示敌机,记录敌机位置
Icon imgPE;
Thread planeEThread; //敌机的线程
int intSleepTime; //用于控制敌机子弹的运动速度
EPlaneInfor planeEInfo;
int MySectionX; //用于保存我机位置,创建智能飞机
int MySectionY;
public PlaneEClass3(int s)
{
intSleepTime=s;
imgPE = new ImageIcon("plane\\eplane1.gif");
picPE = new JLabel(imgPE);
picPE.setBounds(0,0,100,100); //敌机位置的初始设置
pnl.add(picPE);
picPE.setLocation(picPE.getX()+150,picPE.getY()-10);
MySectionX=picPlane.getX();
MySectionY=picPlane.getY();
planeEThread=new Thread(this,"p1");
planeEInfo=new EPlaneInfor(planeEThread,picPE);
planeEVector.addElement(planeEInfo); //把敌机信息对象添加到Vector中去
planeEThread.start(); //启动敌机线程
}
public void run()
{
Random fireSpeed=new Random(123450L); //用于控制敌机子弹的运动速度
int intFirePlace=0;
int intFireSpeed=fireSpeed.nextInt(4);
if(intFireSpeed==0) //放置出错
{
intFireSpeed=5;
}
while(planeEThread==Thread.currentThread())
{
if(picPE.getX()>picPlane.getX())
{
picPE.setLocation(picPE.getX()-2,picPE.getY()+4);
}
else if(picPE.getX()<picPlane.getX())
{
picPE.setLocation(picPE.getX()+2,picPE.getY()+4);
}
else if(picPE.getX()==picPlane.getX())
{
picPE.setLocation(picPE.getX(),picPE.getY()+4);
}
if(picPE.getX()<-20||picPE.getX()>550||picPE.getY()<-10||picPE.getY()>700)
{
picPE.setVisible(false);
picPE.setLocation(-100,-100);
planeEVector.removeElement(planeEInfo);
this.stop();
}
//敌机随机发射子弹
if(intFirePlace>(intFireSpeed*300+intSleepTime*5))
{
new FireEClass(picPE);
intFirePlace=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -