工程的结构如下:
类名 |
父类/接口 |
功能 |
Paly |
void |
音频播放 |
Wel_Panel |
JPanel |
欢迎界面 |
keylistener |
KeyAdapter |
键盘监听 |
PC_tank |
Runnable |
电脑坦克的控制 |
T_Bullet |
Runnable |
子弹的控制 |
Test |
void |
主函数入口各类的组装 |
A_tank |
JPanel |
左方向坦克的绘制 |
D_tank |
JPanel |
右方向坦克的绘制 |
S_tank |
JPanel |
下方向坦克的绘制 |
W_tank |
JPanel |
上方向坦克的绘制 |
Bullet |
JPanel |
子弹的绘制 |
Bullet.java:
package draw_gra; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; @SuppressWarnings("serial") public class Bullet extends JPanel { int x; public Bullet(int i) { x=i; } @Override public void paint(Graphics g) { // TODO Auto-generated method stub if(x==0) g.setColor(Color.GREEN); else g.setColor(Color.red); g.fillOval(0, 0, 5, 5); } }
paly.java:
package draw_gra; import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; public class Paly { private static AudioInputStream audioInputStream; private static AudioFormat audioFormat; private static SourceDataLine res; public Paly(File file) throws UnsupportedAudioFileException, IOException, LineUnavailableException { audioInputStream = AudioSystem.getAudioInputStream(file);//得到要播放的文件的音频输入流 audioFormat = audioInputStream.getFormat();//音频流的 res = AudioSystem.getSourceDataLine(audioFormat);//得到输出设备对象 res.open(audioFormat);//打开输出设备 // res.open(); res.start();//开始播放 int inBytes = 0; byte[] audioData = new byte[1024*200];//缓冲区 while ((inBytes != -1)) { inBytes = audioInputStream.read(audioData, 0, audioData.length);//从音频输入流读取音频数据到缓冲区 if (inBytes >= 0) {//把音频数据写入到输出设备 res.write(audioData, 0, inBytes); } } res.drain();//直到输出设备对象缓冲区的数据被输出,返回。加上这句,能完整的输出整个音频流 res.close();//关闭输出设备 audioInputStream.close();//关闭音频流 } }
Wel_Panel.java
package draw_gra; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Wel_Panel extends JPanel { JFrame frame; JPanel Tank; public Wel_Panel(JFrame fra,JPanel tank) { frame=fra; Tank=tank; ImageIcon bg =new ImageIcon("./res/wel.png"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); JButton btn=new JButton(); btn.setIcon(bg); this.add(btn); btn.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub if(e.getButton()==MouseEvent.BUTTON1) ((CardLayout)frame.getContentPane().getLayout()).show(frame.getContentPane(), "Game"); Tank.requestFocus(); } }); } }
keylistener.java:
package tank_war; import java.awt.CardLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; public class keylistener extends KeyAdapter { private JPanel Game; private JPanel panel; //玩家坦克 private static int x; //记录tank方向 private ArrayList<PC_tank> pctanks; public keylistener(JPanel Game1, JPanel panel2,ArrayList pcs) { // TODO Auto-generated constructor stub Game=Game1; panel=panel2; x=3; pctanks=pcs; } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub super.keyPressed(e); if(panel.getX()==0&&e.getKeyCode()==KeyEvent.VK_A) //防止越界 return ; if(panel.getY()==0&&e.getKeyCode()==KeyEvent.VK_W) return ; if(panel.getX()>460&&e.getKeyCode()==KeyEvent.VK_D) return ; if(panel.getY()>460&&e.getKeyCode()==KeyEvent.VK_S) return ; switch(e.getKeyCode()) { case KeyEvent.VK_W: panel.setLocation(panel.getX(),panel.getY() -2); ((CardLayout)panel.getLayout()).show(panel, "W"); x=1; break; case KeyEvent.VK_A: ((CardLayout)panel.getLayout()).show(panel, "A"); panel.setLocation(panel.getX()-2,panel.getY() ); x=2; break; case KeyEvent.VK_S: panel.setLocation(panel.getX(),panel.getY() +2); ((CardLayout)panel.getLayout()).show(panel, "S"); x=3; break; case KeyEvent.VK_D: ((CardLayout)panel.getLayout()).show(panel, "D"); panel.setLocation(panel.getX()+2,panel.getY() ); x=4; break; case KeyEvent.VK_J: T_Bullet t_bullet=new T_Bullet(panel,Game,x,pctanks,null); //玩家坦克发子弹,目标:pctanks new Thread(t_bullet,"b1").start(); break; } Game.repaint(); //重绘窗口 } }
PC_tank.java
package tank_war; import java.awt.*; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import tank4T.A_tank; import tank4T.D_tank; import tank4T.S_tank; import tank4T.W_tank; public class PC_tank implements Runnable { private JPanel Game; public JPanel tank; private JPanel usrtank; PC_tank(JPanel mf,JPanel usr) { Game = mf; usrtank=usr; tank=new JPanel(); tank.setLayout(new CardLayout()); tank.add("W",new W_tank(0)); tank.add("A",new A_tank(0)); tank.add("S",new S_tank(0)); tank.add("D",new D_tank(0)); tank.setBounds(200, 200, 30, 30); Game.add(tank); } @Override public void run() { // TODO Auto-generated method stub int x=1; Random r = new Random(); while (true) { int rom=r.nextInt(5); if(rom==0) { for(int i=r.nextInt(20);i>0;i--) { if(tank.getY()-10<10) continue; tank.setLocation(tank.getX(),tank.getY() - 10); ((CardLayout)tank.getLayout()).show(tank, "W"); Game.repaint(); try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } x=1; } if(rom==1) { for(int i=r.nextInt(20);i>0;i--) { if(tank.getX()-10<0) continue; tank.setLocation(tank.getX() - 10,tank.getY()); ((CardLayout)tank.getLayout()).show(tank, "A"); Game.repaint(); try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } x=2; } if(rom==2) { for(int i=r.nextInt(20);i>0;i--) { if(tank.getY()+10>470) continue; tank.setLocation(tank.getX(),tank.getY() +10); ((CardLayout)tank.getLayout()).show(tank, "S"); Game.repaint(); try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } x=3; } if(rom==3) { for(int i=r.nextInt(20);i>0;i--) { if(tank.getX()>470) continue; tank.setLocation(tank.getX() + 10,tank.getY()); ((CardLayout)tank.getLayout()).show(tank, "D"); Game.repaint(); try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } x=4; } if(rom==4) { T_Bullet t_bullet=new T_Bullet(tank,Game,x,null,usrtank); //计算机发射发射子弹,目标:usrtank new Thread(t_bullet,"p1").start(); } try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(tank.getParent()==null) //如果该坦克以被移出Game,结束 return ; Game.repaint(); } } }
T_Bullet.java:
package tank_war; import java.awt.FlowLayout; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.ListIterator; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import draw_gra.Bullet; public class T_Bullet implements Runnable { JPanel tank; //发射子弹的坦克 Bullet bullet; JPanel Game; int dir; ArrayList <PC_tank>pctanks; //目标:计算机坦克 JPanel usrtank; //目标:玩家坦克 static Object lock=new Object(); @SuppressWarnings({ "rawtypes", "unchecked" }) T_Bullet(JPanel panel,JPanel fra,int d,ArrayList pcs,JPanel usr) //方向1->W; 2->A; 3->S; 4->D { tank=panel; usrtank=usr; dir=d; Game=fra; pctanks=pcs; } @Override public void run() { // TODO Auto-generated method stub // bullet=new Bullet(tank.getX(),tank.getY()); if(usrtank==null) bullet=new Bullet(0); else bullet=new Bullet(1); bullet.setLayout(null); // bullet.setBounds(tank.getX(),tank.getY(),20,20); switch(dir) { case 1:bullet.setBounds(tank.getX()+10,tank.getY(),5,5); break; case 2:bullet.setBounds(tank.getX(),tank.getY()+10,5,5); break; case 3:bullet.setBounds(tank.getX()+10,tank.getY(),5,5); break; case 4:bullet.setBounds(tank.getX(),tank.getY()+10,5,5); break; } Game.add(bullet); while(bullet.getParent()!=null) { switch(dir) { case 1:bullet.setLocation(bullet.getX(), bullet.getY()-5); break; case 2:bullet.setLocation(bullet.getX()-5, bullet.getY()); break; case 3:bullet.setLocation(bullet.getX(), bullet.getY()+5); break; case 4:bullet.setLocation(bullet.getX()+5, bullet.getY()); break; } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(usrtank==null) { ListIterator<PC_tank> it=pctanks.listIterator(); synchronized(lock) { while(it.hasNext()) { PC_tank obj=it.next(); if(Math.abs(bullet.getX()-obj.tank.getX())<20&&Math.abs(bullet.getX()-obj.tank.getX())>10 &&Math.abs( bullet.getY()-obj.tank.getY() )<20 ) { // obj.tank.setVisible(false); //坦克消失 it.remove(); pctanks.remove(it); Game.remove(obj.tank); //移出frame Game.remove(bullet); //子弹消失 } } } } else { if( Math.abs(bullet.getX()-usrtank.getX() )<20 &&Math.abs(bullet.getY()-usrtank.getY())<20 ) { // usrtank.setVisible(false); //游戏结束 Game.remove(usrtank); } } Game.repaint(); } } }
Test.java:
package tank_war; import java.awt.CardLayout; import java.awt.Color; import java.io.File; import java.io.IOException; import java.util.ArrayList; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import javax.swing.JFrame; import javax.swing.JPanel; import draw_gra.Paly; import draw_gra.Wel_Panel; import tank4T.A_tank; import tank4T.D_tank; import tank4T.S_tank; import tank4T.W_tank; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList <PC_tank>list=new ArrayList<PC_tank>(); JPanel Game_Panel=new JPanel(); Game_Panel.setBounds(0, 0, 500,500); Game_Panel.setVisible(true); Game_Panel.setLayout(null); Game_Panel.setBackground(Color.black); JFrame frame=new JFrame("Tank_War"); frame.setSize(500, 500); frame.setResizable(false); frame.setLayout(new CardLayout()); //卡片 frame.setLocationRelativeTo(null); frame.getContentPane().setBackground(Color.black); JPanel Tank=new JPanel(new CardLayout()); //卡片布局 Wel_Panel wel_panel=new Wel_Panel(frame,Tank); wel_panel.setVisible(true); wel_panel.setBounds(0, 0, 500, 500); Tank.setBounds(0, 0, 30, 30); Tank.add("S",new S_tank(1)); //下 Tank.add("W",new W_tank(1)); //上 Tank.add("A",new A_tank(1)); //左 Tank.add("D",new D_tank(1)); //右 Game_Panel.add(Tank); Tank.addKeyListener(new keylistener(Game_Panel,Tank,list)); frame.setVisible(true); frame.add("Wel",wel_panel); frame.add("Game",Game_Panel); Tank.requestFocus(); //获得键盘焦点 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); while(list.size()<10) //添加电脑坦克 { PC_tank pc_tank=new PC_tank(Game_Panel,Tank); new Thread(pc_tank,"pc").start(); list.add(pc_tank); } try { new Paly(new File("./res/start.wav")); } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
A_tank.java:
package tank4T; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; @SuppressWarnings("serial") public class A_tank extends JPanel { public A_tank(int s) //s==1 for:usr s==0 for pc { if(s==1) { ImageIcon bg =new ImageIcon("./res/wa.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } else { ImageIcon bg =new ImageIcon("./res/ya.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } } /* public void paint(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.BLUE); g.fillRect(0,0,20, 7); g.fillRect(0,21,20 , 7); g.setColor(Color.GREEN); g.fillOval(3, 7, 13, 13); g.setColor(Color.RED); g.drawLine(0, 13,10, 13); }*/ }
D_tank.java
package tank4T; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; @SuppressWarnings("serial") public class A_tank extends JPanel { public A_tank(int s) //s==1 for:usr s==0 for pc { if(s==1) { ImageIcon bg =new ImageIcon("./res/wa.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } else { ImageIcon bg =new ImageIcon("./res/ya.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } } /* public void paint(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.BLUE); g.fillRect(0,0,20, 7); g.fillRect(0,21,20 , 7); g.setColor(Color.GREEN); g.fillOval(3, 7, 13, 13); g.setColor(Color.RED); g.drawLine(0, 13,10, 13); }*/ }
S_tank.java:
package tank4T; import java.awt.AWTEvent; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Label; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; @SuppressWarnings({ "unused", "serial" }) public class S_tank extends JPanel { public S_tank(int s) //s==1 for:usr s==0 for pc { if(s==1) { ImageIcon bg =new ImageIcon("./res/ws.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } else { ImageIcon bg =new ImageIcon("./res/ys.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } } /*public void paint(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.BLUE); g.fillRect(0,0,7, 20); g.fillRect(21,0,7 , 20); g.setColor(Color.GREEN); g.fillOval(7, 3, 13, 13); g.setColor(Color.RED); g.drawLine(13, 10, 13, 20); }*/ }
W_tank.java
package tank4T; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; @SuppressWarnings("serial") public class W_tank extends JPanel { public W_tank(int s) { if(s==1) { ImageIcon bg =new ImageIcon("./res/ww.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } else { ImageIcon bg =new ImageIcon("./res/yw.jpeg"); JLabel label=new JLabel(bg,JLabel.CENTER); this.setLayout(new BorderLayout()); label.setBounds(0,0,bg.getIconWidth() ,bg.getIconHeight()); this.add(label,BorderLayout.CENTER); } } /*public void paint(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.BLUE); g.fillRect(0,0,7, 20); g.fillRect(21,0,7 , 20); g.setColor(Color.GREEN); g.fillOval(7, 3, 13, 13); g.setColor(Color.RED); g.drawLine(13, 10, 13, 0); }*/ }
程序运行界面,,,简易...
天下武功,唯快不破