Te.java
package T1; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Te extends JFrame implements ActionListener { MyPanel mp = null; GuankaPanel gkmp=null; JMenuBar cd1=null; JMenu Cd1=null; JMenuItem cdx1=null; JMenuItem cdx2=null; JMenuItem cdx3=null; JMenuItem cdx4=null; public static void main(String[] args) { Te tank1=new Te(); } public Te() { cd1=new JMenuBar(); Cd1=new JMenu("游戏(G)"); Cd1.setMnemonic('G'); cdx1=new JMenuItem("新游戏(N)"); cdx2=new JMenuItem("退出游戏(E)"); cdx2.addActionListener(this); cdx2.setActionCommand("exit"); cdx2.setMnemonic('E'); cdx3=new JMenuItem("存盘退出(S)"); cdx3.addActionListener(this); cdx3.setActionCommand("save"); cdx3.setMnemonic('S'); cdx4=new JMenuItem("继续游戏(G)"); cdx4.addActionListener(this); cdx4.setActionCommand("goon"); cdx4.setMnemonic('G'); /*mp=new MyPanel(); this.add(mp); this.addKeyListener(mp); Thread t=new Thread(mp); t.start();*/ cdx1.addActionListener(this); cdx1.setActionCommand("xyx"); Cd1.add(cdx1); Cd1.add(cdx4); Cd1.add(cdx3); Cd1.add(cdx2); cd1.add(Cd1); gkmp = new GuankaPanel(); this.add(gkmp); Thread t1=new Thread(gkmp); t1.start(); this.setJMenuBar(cd1); this.setIconImage(new ImageIcon("src/1.jpg").getImage()); this.setSize(500,600); this.setTitle("坦克大战"); this.setLocation(300,280); //this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("xyx")) { mp=new MyPanel("new"); this.remove(gkmp); this.add(mp); this.addKeyListener(mp); Thread t=new Thread(mp); t.start(); this.setVisible(true); Jilu.dqjl(); Jilu.getSdtj(); } else if(e.getActionCommand().equals("exit")) { Jilu.bcjl(); System.exit(0); } else if(e.getActionCommand().equals("save")) { Jilu jl=new Jilu(); jl.SetDtk(mp.dtk); jl.cunpan(); System.exit(0); } else if(e.getActionCommand().equals("goon")) { mp=new MyPanel("goon"); this.remove(gkmp); this.add(mp); this.addKeyListener(mp); Thread t=new Thread(mp); t.start(); this.setVisible(true); } } } class GuankaPanel extends JPanel implements Runnable { int times=0; public void paint(Graphics g) { super.paint(g); g.fillRect(0, 0, 400, 300); if(times%2==0) { g.setColor(Color.yellow); Font myFont=new Font("楷体",Font.BOLD,38); g.setFont(myFont); g.drawString("第1关", 140, 140); } } public void run() { while(true) { try { Thread.sleep(600); } catch (Exception e){} times++; this.repaint(); } } } class MyPanel extends JPanel implements KeyListener,Runnable { //Jilu.dqjl(); int key=0; MyTank mt=null; Vector<DiTank> dtk=new Vector<DiTank>(); Vector<Baozha> bzjh=new Vector<Baozha>(); Vector<Weizhi> wzjh=new Vector<Weizhi>(); int tksl=3; Image tp1=new ImageIcon("src/1.gif").getImage(); Image tp2=new ImageIcon("src/2.gif").getImage(); Image tp3=new ImageIcon("src/3.gif").getImage(); public MyPanel() { mt=new MyTank(100,100); for(int i=0;i<tksl;i++) { DiTank dt=new DiTank((i)*181+5,0); dt.setFangxiang(2); Thread t= new Thread(dt); t.start(); Zidan zd=new Zidan(dt.x+10,dt.y+30,2); dt.dzd.add(zd); Thread t3=new Thread(zd); t3.start(); dtk.add(dt); dt.dtkxl(dtk); } //Image tp1=new ImageIcon("src/1.gif").getImage(); //Image tp2=new ImageIcon("src/2.gif").getImage(); //Image tp3=new ImageIcon("src/3.gif").getImage(); } public MyPanel(String ss) { //Jilu.dqjl(); //mt=new MyTank(140,232); if(ss.equals("new")) { mt=new MyTank(100,100); for(int i=0;i<tksl;i++) { DiTank dt=new DiTank((i)*180+5,0); dt.setFangxiang(2); dt.dtkxl(dtk); Thread t2=new Thread(dt); t2.start(); Zidan zd=new Zidan(dt.x+10,dt.y+30,2); dt.dzd.add(zd); Thread t3=new Thread(zd); t3.start(); dtk.add(dt); } }else if(ss.equals("goon")) { mt=new MyTank(100,100); wzjh=Jilu.dupan(); for(int i=0;i<wzjh.size();i++) { Weizhi wz=wzjh.get(i); DiTank dt=new DiTank(wz.x,wz.y); dt.setFangxiang(wz.fangxiang); dt.dtkxl(dtk); Thread t2=new Thread(dt); t2.start(); Zidan zd=new Zidan(dt.x+10,dt.y+30,2); dt.dzd.add(zd); Thread t3=new Thread(zd); t3.start(); dtk.add(dt); } } Shengyin sy=new Shengyin("src/1.wav"); sy.start(); } public void tjsj(Graphics g) { this.drawTank(80, 330, g, 0, 0); g.setColor(Color.black); g.drawString(Jilu.getMtsl()+"", 116, 350); this.drawTank(150, 330, g, 0, 1); g.setColor(Color.black); g.drawString(Jilu.getDtsl()+"", 186, 350); this.drawTank(450,86,g,0,1); g.setColor(Color.black); g.drawString(Jilu.getSdtj()+"",486,107); g.setColor(Color.black); Font f=new Font("宋体",Font.BOLD,20); g.setFont(f); g.drawString("你消灭的坦克总数", 410, 40); } public void paint(Graphics g) { if(this.mt.shengming) { if(key==1) { this.mt.setFangxiang(0); this.mt.xiangshang(); }else if(key==2) { this.mt.setFangxiang(1); this.mt.xiangzuo(); } else if(key==3){ this.mt.setFangxiang(2); this.mt.xiangxia(); } else if(key ==4) { this.mt.setFangxiang(3); this.mt.xiangyou(); } } super.paint(g); g.fillRect(0, 0, 400, 300); if(mt.shengming) { this.drawTank(mt.getX(),mt.getY(),g,mt.fangxiang,0); } for(int i=0;i<dtk.size();i++) { DiTank dt=dtk.get(i); if(dt.shengming) { for (int j=0;j<dt.dzd.size();j++){ Zidan zd=dt.dzd.get(j); if(zd!=null && zd.shengming){ g.setColor(Color.white); g.fillOval(zd.x,zd.y, 4, 4);} if(zd.shengming==false){ dt.dzd.remove(j); } } this.drawTank(dtk.get(i).getX(),dtk.get(i).getY(), g, dt.fangxiang, 1); //g.drawImage(tp1, 40, 40, 30, 30, this); } } 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); //this.repaint(); } if(zd.shengming==false) { mt.aa.remove(zd); } } for(int i=0;i<bzjh.size();i++) { Baozha bz=bzjh.get(i); if(bz.shengcunqi>60) { g.drawImage(tp1, bz.x, bz.y, 30, 30, this); }else if(bz.shengcunqi>30) { 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); } } this.tjsj(g); } public void jzwf() { for(int i=0;i<this.dtk.size();i++) { DiTank dt=dtk.get(i); for(int j=0;j<dt.dzd.size();j++) { Zidan zd= dt.dzd.get(j); this.jzdf(zd, mt,0); } } } public void jzdf1() { 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,1); } } } this.repaint(); } } public void jzdf(Zidan zd,Tank dt,int n) { if(dt.shengming) { 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; if(n==1) { Jilu.dtjs(); Jilu.sdtj(); } 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; if(n==1) { Jilu.dtjs(); Jilu.sdtj(); } 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; } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_W) { key=0; }else if(e.getKeyCode()==KeyEvent.VK_A) { key=0; } else if(e.getKeyCode()==KeyEvent.VK_S) { key=0; }else if(e.getKeyCode()==KeyEvent.VK_D) { key=0; } } public void keyPressed(KeyEvent e) { /* if(e.getKeyCode()==KeyEvent.VK_J) { if(this.mt.aa.size()<2) { this.mt.fszd(); } } if(e.getKeyCode()==KeyEvent.VK_W) { this.mt.setFangxiang(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()<2&&this.mt.shengming) { this.mt.fszd(); } } if(e.getKeyCode()==KeyEvent.VK_W) { key=1; }else if(e.getKeyCode()==KeyEvent.VK_A) { key=2; } else if(e.getKeyCode()==KeyEvent.VK_S) { key=3; }else if(e.getKeyCode()==KeyEvent.VK_D) { key=4; } this.repaint(); } public void run() { while(true) { try{ Thread.sleep(20);} catch(Exception e) {} /*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); } } } }*/ jzdf1(); jzwf(); this.repaint(); } } }
package T1; //import java.io.BufferedReader; //import java.io.BufferedWriter; //import java.io.FileReader; import java.io.*; import java.util.Vector; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; class Tank { int x=0,y=0; int fangxiang=0; int sudu=1; boolean shengming=true; public int getSudu(){ return sudu; } public void setSudu(int sudu){ this.sudu=sudu; } public int getFangxiang(){ return fangxiang; } public void setFangxiang(int fangxiang){ this.fangxiang=fangxiang; } 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 Tank(int x,int y){ this.x=x; this.y=y; } } class DiTank extends Tank implements Runnable { int sudu=2; int time = 0; Vector<Zidan> dzd=new Vector<Zidan>(); Vector<DiTank> dtk=new Vector<DiTank>(); public DiTank(int x,int y) { super(x,y); } public void dtkxl(Vector<DiTank> dxl) { this.dtk=dxl; } public void run(){ while (true) { switch(this.fangxiang) { case 0: for(int i=0;i<30;i++) { if(y>0&&!bpz()) {y-=sudu;} try{ Thread.sleep(50); }catch(Exception e){} } break; case 1: for(int i=0;i<30;i++) { if(x>0&&!bpz()) {x-=sudu;} try{ Thread.sleep(50); }catch(Exception e){} } break; case 2: for(int i=0;i<30;i++) { if(y<250&&!bpz()) {y+=sudu;} try{ Thread.sleep(50); }catch(Exception e){} } break; case 3: for(int i=0;i<30;i++) { if(x<360&&!bpz()) {x+=sudu;} try{ Thread.sleep(50); }catch(Exception e){} } break; } /* try{ Thread.sleep(500); }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; }*/ this.fangxiang=(int)(Math.random()*4); if(this.shengming==false) { break; } this.time++; if(time%2==0) { if(shengming) { if(dzd.size()<5) { Zidan zd=null; switch(fangxiang) { case 0: zd=new Zidan(x+10,y,0); dzd.add(zd); break; case 1: zd=new Zidan(x,y+10,1); dzd.add(zd); break; case 2: zd = new Zidan(x+10,y+30,2); dzd.add(zd); break; case 3: zd=new Zidan(x+30,y+10,3); dzd.add(zd); break; } Thread t5 = new Thread(zd); t5.start(); } } } } } public boolean bpz() { boolean b=false; switch(this.fangxiang) { case 0: for(int i=0;i<dtk.size();i++) { DiTank dt=dtk.get(i); if(dt!=this) { if(dt.fangxiang==0||dt.fangxiang==2) { if(this.x>dt.x&&this.x<dt.x+20&&this.y>=dt.y&&this.y<=dt.y+30) { return true; } if(this.x+20>=dt.x&&this.x+20<=dt.x+20&&this.y>=dt.y&&this.y<=dt.y+30) { return true; } } if(dt.fangxiang==3||dt.fangxiang==1) { if(this.x>=dt.x&&this.x<=dt.x+30&&this.y>=dt.y&&this.y<=dt.y+20) { return true; } if(this.x+20>=dt.x&&this.x+20<=dt.x+30&&this.y>=dt.y&&this.y<=dt.y+20) { return true; } } } } break; case 1: for(int i=0;i<dtk.size();i++) { DiTank dt=dtk.get(i); if(dt!=this) { if(dt.fangxiang==0||dt.fangxiang==2) { if(this.x>=dt.x&&this.x<dt.x+20&&this.y>=dt.y&&this.y<=dt.y+30) { return true; } if(this.x>=dt.x&&this.x<=dt.x+20&&this.y+20>=dt.y&&this.y+20<=dt.y+30) { return true; } } if(dt.fangxiang==3||dt.fangxiang==1) { if(this.x>=dt.x&&this.x<=dt.x+30&&this.y>=dt.y&&this.y<=dt.y+20) { return true; } if(this.x>=dt.x&&this.x<=dt.x+30&&this.y+20>=dt.y&&this.y+20<=dt.y+20) { return true; } } } } break; case 2: for(int i=0;i<dtk.size();i++) { DiTank dt=dtk.get(i); if(dt!=this) { if(dt.fangxiang==0||dt.fangxiang==2) { if(this.x>dt.x&&this.x<dt.x+20&&this.y+30>=dt.y&&this.y+30<=dt.y+30) { return true; } if(this.x+20>=dt.x&&this.x+20<=dt.x+20&&this.y+30>=dt.y&&this.y+30<=dt.y+30) { return true; } } if(dt.fangxiang==3||dt.fangxiang==1) { if(this.x>=dt.x&&this.x<=dt.x+30&&this.y+30>=dt.y&&this.y+30<=dt.y+20) { return true; } if(this.x+20>=dt.x&&this.x+20<=dt.x+30&&this.y+30>=dt.y&&this.y+30<=dt.y+20) { return true; } } } } break; case 3: for(int i=0;i<dtk.size();i++) { DiTank dt=dtk.get(i); if(dt!=this) { if(dt.fangxiang==0||dt.fangxiang==2) { if(this.x+30>=dt.x&&this.x+30<dt.x+20&&this.y>=dt.y&&this.y<=dt.y+30) { return true; } if(this.x+30>=dt.x&&this.x+30<=dt.x+20&&this.y+20>=dt.y&&this.y+20<=dt.y+30) { return true; } } if(dt.fangxiang==3||dt.fangxiang==1) { if(this.x+30>=dt.x&&this.x+30<=dt.x+30&&this.y>=dt.y&&this.y<=dt.y+20) { return true; } if(this.x+30>=dt.x&&this.x+30<=dt.x+30&&this.y+20>=dt.y&&this.y+20<=dt.y+20) { return true; } } } } break; } return b; } } class MyTank extends Tank { Vector<Zidan>aa=new Vector<Zidan>(); Zidan zd=null; public MyTank(int x,int y) { super(x,y); } public void fszd() { switch(this.fangxiang) { case 0: zd=new Zidan(x+10,y,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(); } public void xiangshang() { y-=sudu; } public void xiangzuo() { x-=sudu; } public void xiangxia() { y+=sudu; } public void xiangyou() { x+=sudu; } } class Zidan implements Runnable { int x; int y; int fangxiang; int sudu=1; boolean shengming=true; public Zidan(int x,int y,int fangxiang) { this.x=x; this.y=y; this.fangxiang=fangxiang; } public void run() { while(true) { try{ Thread.sleep(10); } catch(Exception e){} switch(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; } } } } class Baozha { int x,y; int shengcunqi=90; 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; } } } class Jilu { private static int dtsl=10; private static int mtsl=2; private static int sdtj=0; private static FileWriter fw=null; private static BufferedWriter bw=null; private static FileReader fr=null; private static BufferedReader br=null; private Vector<DiTank> dtk=new Vector<DiTank>(); static Vector<Weizhi> wzjh=new Vector<Weizhi>(); public Vector<DiTank> getDtk(){ return dtk; } public void SetDtk(Vector<DiTank> dtk){ this.dtk=dtk; } public static Vector<Weizhi> dupan(){ try{ fr=new FileReader("src/cpjl.txt"); br=new BufferedReader(fr); String s=""; s=br.readLine(); sdtj=Integer.parseInt(s); while((s=br.readLine())!=null) { String[] sz=s.split(" "); Weizhi wz=new Weizhi(Integer.parseInt(sz[0]),Integer.parseInt(sz[1]),Integer.parseInt(sz[2])); wzjh.add(wz); } }catch(Exception e){} finally{ try{ br.close(); fr.close(); }catch (Exception e){} } return wzjh; } public static void bcjl() { try{ fw=new FileWriter("src/cpjl.txt"); bw=new BufferedWriter(fw); bw.write(Integer.toString(sdtj)); }catch (Exception e){} finally { try{ bw.close(); fw.close(); }catch(Exception e){} } } public void cunpan() { try{ fw=new FileWriter("src/cpjl.txt"); bw=new BufferedWriter(fw); bw.write(sdtj+"\r\n"); for(int i=0;i<dtk.size();i++) { DiTank dt=dtk.get(i); if(dt.shengming) { String zb=dt.x+" "+dt.y+" "+dt.fangxiang+""; bw.write(zb+"\r\n"); } } }catch(Exception e){} finally{ try{ bw.close(); fw.close(); }catch(Exception e){} } } public static void dqjl() { try{ fr=new FileReader("src/cpjl.txt"); br=new BufferedReader(fr); String s=br.readLine(); sdtj=Integer.parseInt(s); }catch(Exception e){} finally{ try{ br.close(); fr.close(); }catch(Exception e){} } } public static int getSdtj(){ return sdtj; } public static void setSdtj(int sdtj){ Jilu.sdtj=sdtj; } public static void sdtj() { sdtj++; } public static int getDtsl() { return dtsl; } public static void setDtsl(int dtsl){ Jilu.dtsl=dtsl; } public static int getMtsl(){ return mtsl; } public static void setMtsl(int mtsl){ Jilu.mtsl=mtsl; } public static void dtjs(){ dtsl--; } } class Weizhi{ int x; int y; int fangxiang; public Weizhi(int x,int y,int fangxiang) { this.x=x; this.y=y; this.fangxiang=fangxiang; } } class Shengyin extends Thread{ private String wjm; public Shengyin(String ypwj) { wjm=ypwj; } public void run(){ File wjl=new File(wjm); AudioInputStream ypsrl=null; try{ ypsrl=AudioSystem.getAudioInputStream(wjl); }catch(Exception e){} AudioFormat format=ypsrl.getFormat(); SourceDataLine aqsj=null; DataLine.Info info=new DataLine.Info(SourceDataLine.class, format); try{ aqsj=(SourceDataLine)AudioSystem.getLine(info); aqsj.open(format); }catch(Exception e){} aqsj.start(); int zjtj=0; byte[] hczj=new byte[1024]; try{ while(zjtj !=-1){ zjtj=ypsrl.read(hczj, 0, hczj.length); if(zjtj>=0) aqsj.write(hczj, 0, zjtj); } }catch(Exception e){} finally{ aqsj.drain(); aqsj.close(); } } }