🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
让敌人坦克动起来 Test.java ~~~ package tkdz; import javax.swing.*; import java.awt.*; public class Test extends JFrame{ MyPanel mp =null; public static void main(String[] args){ Test t1 =new Test(); } public Test(){ mp =new MyPanel();//面板 Thread t=new Thread(mp);//多余吗? t.start();//多余吗? this.add(mp); this.addKeyListener(mp);//添加键盘监听 this.setSize(400,300); this.setLocation(300, 280); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } ~~~ Tank.java ~~~ package tkdz; public class Tank { int x=0,y=0; int fangxiang=0;//方向 int sudu=5;//速度 int color; boolean shengming=true; public int getFangxiang() { return fangxiang; } public void setFangxiang(int fangxiang) { this.fangxiang = fangxiang; } public int getSudu() { return sudu; } public void setSudu(int sudu) { this.sudu = sudu; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } public boolean isShengming() { return shengming; } public void setShengming(boolean shengming) { this.shengming = shengming; } public Tank(int x, int y) { super(); this.x = x; this.y = y; } } class Baozha{ int x,y; int shengcunqi=900;//生存期 boolean shengming=true; public Baozha(int x,int y){ this.x=x; this.y=y; } public void suqsd(){ if(shengcunqi>0){ shengcunqi--; }else{ this.shengming=false; } } } ~~~ MyTank.java ~~~ package tkdz; import java.util.Vector; //因为子弹都是跟着坦克走的,所以子弹的功能大多数都跟坦克类有关 public class MyTank extends Tank{ Vector<Zidan> aa =new Vector<Zidan>(); Zidan zd=null; public MyTank(int x, int y) { super(x, y); } public void xiangshang() { y-=sudu; } public void xiangzuo() { x-=sudu; } public void xiangxia() { y+=sudu; } public void xiangyou() { x+=sudu; } //子弹定义在我的坦克里,因为子弹是随着我的坦克走的 public void fszd(){ switch(this.fangxiang){ case 0: zd=new Zidan(x+10,y,0);//0控制的子弹方向 aa.add(zd); break; case 1: zd=new Zidan(x,y+10,1); aa.add(zd); break; case 2: zd=new Zidan(x+10,y+30,2); aa.add(zd); break; case 3: zd=new Zidan(x+30,y+10,3); aa.add(zd); break; } Thread t=new Thread(zd);//启动线程,发射子弹 t.start(); } } ~~~ DiTank.java ~~~ package tkdz; public class DiTank extends Tank implements Runnable{ int sudu=2; public DiTank(int x, int y) { super(x, y); } @Override public void run() { while(true){ try { Thread.sleep(50); } catch (Exception e) { } switch(this.fangxiang){ case 0: for(int i=0;i<30;i++){ y-=sudu; try { Thread.sleep(50); } catch (Exception e) {} } break; case 1: for(int i=0;i<30;i++){ x-=sudu; try { Thread.sleep(50); } catch (Exception e) {} } break; case 2: for(int i=0;i<30;i++){ y+=sudu; try { Thread.sleep(50); } catch (Exception e) {} } break; case 3: for(int i=0;i<30;i++){ x+=sudu; try { Thread.sleep(50); } catch (Exception e) {} } break; } //随机出现0-3之间的数字,0,1,2,3 this.fangxiang=(int)(Math.random()*4); if(this.shengming==false){ break; } } } } ~~~ Zidan.java ~~~ package tkdz; //由于敌人的子弹是随意动的,直接把子弹做成线程,继承Runnable接口 public class Zidan implements Runnable{ int x; int y; int fangxiang; int sudu=5; boolean shengming=true;//生命 public Zidan(int x, int y,int fangxiang) { super(); this.x = x; this.y = y; this.fangxiang=fangxiang; } @Override public void run() {//让子弹动起来 //凡是线程下面的死循环里都要有个睡眠 while(true){ try{ Thread.sleep(50);//睡眠 }catch(Exception e){} switch(this.fangxiang){ case 0: y-=sudu; break; case 1: x-=sudu; break; case 2: y+=sudu; break; case 3: x+=sudu; break; } if(x<0||x>400||y<0||y>300){//让子弹超出画布就消失 this.shengming=false; break; } } } } ~~~ MyPanel.java ~~~ package tkdz; import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.*; public class MyPanel extends JPanel implements KeyListener,Runnable{ MyTank mt=null; Vector<DiTank> dtk =new Vector<DiTank>();//敌坦克集合,多线程用Vector Vector<Baozha> bzjh=new Vector<Baozha>();//爆炸集合 int tksl =3;//敌人坦克的数量 //爆炸图片初始化 Image tp1=null; Image tp2=null; Image tp3=null; public MyPanel(){ mt=new MyTank(140,232);//我的坦克,1辆 for(int i=0;i<tksl;i++){//敌坦克数量,3辆 DiTank dt=new DiTank((i)*181+5,0); Thread t =new Thread(dt);//启动线程 t.start(); dtk.add(dt); } tp1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bzxg1.gif")); tp2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bzxg2.gif")); tp3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bzxg3.gif")); } public void paint(Graphics g){ super.paint(g); g.fillRect(0, 0, 400, 300);//黑色400*300画布 this.drawTank(mt.getX(), mt.getY(), g, mt.fangxiang, 0);//绘制我方坦克 for(int i = 0; i < dtk.size(); i++){//绘制敌方坦克 //this.drawTank(dtk.get(i).getX(), dtk.get(i).getY(), g, 2, 1); DiTank dt=dtk.get(i); if(dt.shengming){ this.drawTank(dt.getX(), dt.getY(), g, dt.fangxiang, 1); } } for(int i=0;i<mt.aa.size();i++){ Zidan zd=mt.aa.get(i); if(zd!=null&& zd.shengming==true){//绘制子弹 g.setColor(Color.white); g.fill3DRect(zd.x, zd.y, 3, 3, false); } if(zd.shengming==false){ mt.aa.remove(zd); } } for(int i=0;i<bzjh.size();i++){ Baozha bz=bzjh.get(i); if(bz.shengcunqi>600){ g.drawImage(tp1, bz.x, bz.y, 30, 30, this); }else if(bz.shengcunqi>300){ g.drawImage(tp2, bz.x, bz.y, 30, 30, this); }else{ g.drawImage(tp3, bz.x, bz.y, 30, 30, this); } bz.suqsd(); if(bz.shengcunqi==0){ bzjh.remove(bz); } } } //击中敌方 public void jzdf(Zidan zd,DiTank dt){ switch(dt.fangxiang){ case 0://上下很像,一起判断 case 2: if(zd.x>dt.x&&zd.x<dt.x+20&&zd.y>dt.y&&zd.y<dt.y+30){ zd.shengming=false; dt.shengming=false; //b2=true; Baozha bz=new Baozha(dt.x,dt.y); bzjh.add(bz); } break; case 1: case 3: if(zd.x>dt.x&&zd.x<dt.x+30&&zd.y>dt.y&&zd.y<dt.y+20){ zd.shengming=false; dt.shengming=false; //b2=true; Baozha bz=new Baozha(dt.x,dt.y); bzjh.add(bz); } } } public void drawTank(int x,int y,Graphics g,int fangxiang,int leixing) { switch(leixing) { case 0://我的坦克 g.setColor(Color.yellow); break; case 1://敌人的坦克 g.setColor(Color.green); break; } switch(fangxiang) { case 0://上 g.fill3DRect(x, y, 5, 30,false); g.fill3DRect(x+15,y , 5, 30,false); g.fill3DRect(x+5,y+5 , 10, 20,false); g.fillOval(x+5, y+10, 10, 10); g.drawLine(x+10, y+15, x+10, y-3); break; case 1://左 g.fill3DRect(x, y, 30, 5,false); g.fill3DRect(x, y+15, 30, 5, false); g.fill3DRect(x+5, y+5, 20, 10, false); g.fillOval(x+10, y+5, 10, 10); g.drawLine(x+15, y+10, x-3, y+10); break; case 2://下 g.fill3DRect(x, y, 5, 30,false); g.fill3DRect(x+15,y , 5, 30,false); g.fill3DRect(x+5,y+5 , 10, 20,false); g.fillOval(x+5, y+10, 10, 10); g.drawLine(x+10, y+15, x+10, y+33); break; case 3://右 g.fill3DRect(x, y, 30, 5,false); g.fill3DRect(x, y+15, 30, 5, false); g.fill3DRect(x+5, y+5, 20, 10, false); g.fillOval(x+10, y+5, 10, 10); g.drawLine(x+15, y+10, x+33, y+10); break; } this.repaint(); } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_W) { this.mt.setFangxiang(0);//方向为0 this.mt.xiangshang();//启动函数向上 }else if(e.getKeyCode()==KeyEvent.VK_A) { this.mt.setFangxiang(1); this.mt.xiangzuo(); }else if(e.getKeyCode()==KeyEvent.VK_S) { this.mt.setFangxiang(2); this.mt.xiangxia(); }else if(e.getKeyCode()==KeyEvent.VK_D) { this.mt.setFangxiang(3); this.mt.xiangyou(); } //子弹 if(e.getKeyCode()==KeyEvent.VK_J){ if(this.mt.aa.size()<8){ this.mt.fszd();//发射子弹 } } this.repaint(); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void run() { while(true){ try { Thread.sleep(100); } catch (Exception e) { //this.repaint(); } //这里用二层循环的目的是,让每发子弹去和每个敌人坦克比较 for(int i=0;i<mt.aa.size();i++){ Zidan zd=mt.aa.get(i); if(zd.shengming){ for(int j=0;j<dtk.size();j++){ DiTank dt=dtk.get(j); if(dt.shengming){ this.jzdf(zd,dt);//击中敌方 } } } this.repaint(); } //this.repaint(); } } } ~~~ ![](https://box.kancloud.cn/7127f083550d0bd73fc5637d61aa257d_391x288.png)