java坦克大战源代码(实现多种功能)

happy熊 贡献于2014-01-05

作者 KID  创建于2012-06-06 01:30:27   修改者  修改于2012-08-19 11:20:49字数18339

文档摘要://此文件有三个.java文件组成//以包名为标志,复制时请注意
关键词:

//此文件有三个.java文件组成 //以包名为标志,复制时请注意 package com.tank4_3; import javax.imageio.ImageIO; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class MyTankGame extends JFrame implements ActionListener { MyJPanel mp = null; MyStartPanel msp=null; JMenuBar jmb=null; JMenu jm1=null; JMenu jm2=null; JMenuItem jmi1=null; JMenuItem jmi2=null; JMenuItem jmi3=null; JMenuItem jmi4=null; JMenuItem jmi5=null; JMenuItem jmi6=null; JMenuItem jmi21=null; public static void main(String[] args) { MyTankGame ytg = new MyTankGame(); } public MyTankGame() { msp=new MyStartPanel(); Thread t=new Thread(msp); t.start(); jmb=new JMenuBar(); jm1=new JMenu("游戏(G)"); jm1.setMnemonic('g'); jmi1=new JMenuItem("新游戏(N)"); jmi1.setMnemonic('n'); jmi1.addActionListener(this); jmi1.setActionCommand("newgame"); jmi2=new JMenuItem("暂停(S)"); jmi2.setMnemonic('s'); jmi2.addActionListener(this); jmi2.setActionCommand("suspend"); jmi3=new JMenuItem("继续(C)"); jmi3.setMnemonic('c'); jmi3.addActionListener(this); jmi3.setActionCommand("continue"); jmi6=new JMenuItem("读取游戏(X)"); jmi6.setMnemonic('x'); jmi6.addActionListener(this); jmi6.setActionCommand("loadGame"); jmi4=new JMenuItem("储存游戏(Q)"); jmi4.setMnemonic('q'); jmi4.addActionListener(this); jmi4.setActionCommand("save"); jmi5=new JMenuItem("退出(E)"); jmi5.setMnemonic('e'); jmi5.addActionListener(this); jmi5.setActionCommand("exit"); jm2=new JMenu("帮助(H)"); jm2.setMnemonic('h'); jmi21=new JMenuItem("操作说明(U)"); jmi21.setMnemonic('u'); jmi21.addActionListener(this); jmi21.setActionCommand("help"); jmb.add(jm1); jmb.add(jm2); jm1.add(jmi1); jm1.add(jmi2); jm1.add(jmi3); jm1.add(jmi6); jm1.add(jmi4); jm1.add(jmi5); jm2.add(jmi21); this.setJMenuBar(jmb); this.add(msp); setSize(430, 430); setResizable(false); setTitle("坦克大战4.3"); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub //按开始后显示战斗场面 if(arg0.getActionCommand().equals("newgame")) { //进入战斗后可以重新开始 if(this.mp!=null) { this.remove(mp); } this.mp = new MyJPanel("new"); add(this.mp); Thread t = new Thread(this.mp); t.start(); addKeyListener(this.mp); GameRecord.setEnNum(20); GameRecord.setMyNum(3); GameRecord.setHitNum(0); //要显示战斗场面,先要移除开始画面 this.remove(msp); //刷新 this.setVisible(true); }//战斗暂停 else if(arg0.getActionCommand().equals("suspend")) { //让子弹和坦克的速度变为0 MyTank.setSpeed(0); EnemyTank.setSpeed(0); Bullet.setSpeed(0); //让坦克不能改变方向 EnemyTank.setStrar(false); MyTank.setStrar(false); }//战斗重新开始 else if(arg0.getActionCommand().equals("continue")) { //恢复子弹和坦克的速度 MyTank.setSpeed(3); EnemyTank.setSpeed(1); Bullet.setSpeed(3); EnemyTank.setStrar(true); MyTank.setStrar(true); } //读取游戏 else if(arg0.getActionCommand().equals("loadGame")) { if(this.mp!=null) { this.remove(mp); } this.mp = new MyJPanel("load"); add(this.mp); Thread t = new Thread(this.mp); t.start(); addKeyListener(this.mp); //要显示战斗场面,先要移除开始画面 if(this.msp!=null) { this.remove(msp); } //刷新 this.setVisible(true); } //储存游戏 else if(arg0.getActionCommand().equals("save")) { //调用保存游戏函数 GameRecord.writeRecord (); //跳出保存成功窗口 ReSuccess re=new ReSuccess(); } //退出 else if(arg0.getActionCommand().equals("exit")) { System.exit(0); } //帮助 else if(arg0.getActionCommand().equals("help")) { Help h=new Help(); } } } //画出开始提示面板 class MyStartPanel extends JPanel implements Runnable { int time=0; public void paint(Graphics g) { super.paint(g); g.fillRect(0, 0, 300, 310); if(time%2==0) { g.setColor(Color.yellow); g.setFont(new Font("宋体",Font.BOLD,22)); g.drawString("Stage:1", 100, 140); } } //让字体闪动 public void run() { while(true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } time++; if(time==13) { break; } this.repaint(); } } } class MyJPanel extends JPanel implements KeyListener, Runnable { MyTank mytank = null; Image image1 = null; Image image2 = null; Image image3 = null; String flag; Vector enem = new Vector(); //Vector nodes=new Vector(); Vector bo=new Vector(); //战斗敌人坦克的数量 int enSize =4; public MyJPanel(String flag) { this.flag=flag; this.mytank = new MyTank(100, 284); //新的游戏 if(flag.equals("new")) { for (int i = 0; i < this.enSize; i++) { EnemyTank enemy = new EnemyTank(i * 90, 0); //传到EnemyTank enemy.setDirect(1); enemy.setEnems(enem); //传到GameRecord GameRecord.setEnems(enem); Thread th=new Thread(enemy); th.start(); this.enem.add(enemy); } //读取游戏 }else if(flag.equals("load")) { GameRecord.readerRecord(); System.out.print(GameRecord.nodes.size() ); for (int i = 0; i 0) { this.live -= 1; } else this.isLive = false; } } class Bullet implements Runnable { int x; int y; int direct; private static int speed=3; boolean isLive = true; public Bullet(int x, int y, int direct) { this.x = x; this.y = y; this.direct = direct; } public void run() { while (this.isLive) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } switch (this.direct) { case 0: this.y -= this.speed; break; case 1: this.y += this.speed; break; case 2: this.x -= this.speed; break; case 3: this.x += this.speed; } //控制子弹的范围 if ((this.x< 0)||(this.x>300)||(this.y<0)||(this.y>310)) { this.isLive=false; break; } } } public static int getSpeed() { return speed; } public static void setSpeed(int speed) { Bullet.speed = speed; } } class MyTank extends Tank { Vector shot = new Vector(); private static int speed=3; //控制坦克是否能上下左右 private static boolean isStrar=true; //boolean isLive=true; public void shootBullet() { switch (this.direct) { case 0: this.bullet = new Bullet(this.x + 10, this.y - 3, 0); this.shot.add(this.bullet); break; case 1: this.bullet = new Bullet(this.x + 10, this.y + 29, 1); this.shot.add(this.bullet); break; case 2: this.bullet = new Bullet(this.x - 3, this.y + 10, 2); this.shot.add(this.bullet); break; case 3: this.bullet = new Bullet(this.x + 29, this.y + 10, 3); this.shot.add(this.bullet); } Thread t = new Thread(this.bullet); t.start(); } public MyTank(int x, int y) { super(x, y); } //让自己的坦克移动 public void moveUp() { if(y>0) { this.y -= this.speed; } } public void moveDown() { if(y<284) { this.y += this.speed; } } public void moveLeft() { if(x>0) { this.x -= this.speed; } } public void moveRight() { if(x<274) { this.x += this.speed; } } public static int getSpeed() { return speed; } public static void setSpeed(int speed) { MyTank.speed = speed; } public static boolean isStrar() { return isStrar; } public static void setStrar(boolean isStrar) { MyTank.isStrar = isStrar; } } class Tank { int x = 0; int y = 0; int direct = 0; Bullet bullet = null; //int speed = 1; int color = 0; boolean isLive=true; public Tank(int x, int y) { this.x = x; this.y = y; } public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } public int getDirect() { return this.direct; } public void setDirect(int direct) { this.direct = direct; } public int getColor() { return this.color; } public void setColor(int color) { this.color = color; } /*public int getSpeed() { return this.speed; } public void setSpeed(int speed) { this.speed = speed; }*/ } class EnemyTank extends Tank implements Runnable { int color = 1; private static int speed=1; //控制坦克是否能上下左右 private static boolean isStrar=true; Vector shot=new Vector(); //从MyPanel中提取坦克; Vector enems=new Vector(); public EnemyTank(int x, int y) { super(x, y); } public void setEnems(Vector enems) { this.enems=enems; } //让敌人的的坦克移动起来 public void run() { while(true) { switch(this.direct) { case 0: for(int i=0;i<(int)(Math.random()*160);i++) { if(y>0&&this.isOverlap()) { y-=this.speed; try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }break; case 1:for(int i=0;i<(int)(Math.random()*160);i++) {if(y<284&&this.isOverlap()) { y+=this.speed; try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }break; case 2:for(int i=0;i<(int)(Math.random()*160);i++) {if(x>0&&this.isOverlap()) { x-=this.speed; try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }break; case 3:for(int i=0;i<(int)(Math.random()*160);i++) {if(x<274&&this.isOverlap()) { x+=this.speed; try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }break; } //控制敌人坦克子弹的数量····· if(EnemyTank.isStrar) { if(this.shot.size()<2) { this.shootBullet(); } this.direct=(int)(Math.random()*4); if(this.isLive==false) { break; } } } } public void shootBullet() { switch (this.direct) { case 0: this.bullet = new Bullet(this.x + 10, this.y - 3, 0); this.shot.add(this.bullet); break; case 1: this.bullet = new Bullet(this.x + 10, this.y + 29, 1); this.shot.add(this.bullet); break; case 2: this.bullet = new Bullet(this.x - 3, this.y + 10, 2); this.shot.add(this.bullet); break; case 3: this.bullet = new Bullet(this.x + 29, this.y + 10, 3); this.shot.add(this.bullet); } Thread t = new Thread(this.bullet); t.start(); } //不让敌人的坦克重叠 public boolean isOverlap() { //0表示向上 1表示向下 2表示向左 3表示向右 boolean isOver=true; switch(this.direct)//参照坦克的方向 { case 0: for(int i=0;i=et.x&&this.xet.y&&this.y=et.x&&this.x+20et.y&&this.y=et.x&&this.xet.y&&this.y=et.x&&this.x+20et.y&&this.y=et.x&&this.xet.y&&this.y+26=et.x&&this.x+20et.y&&this.y+26=et.x&&this.xet.y&&this.y+26=et.x&&this.x+20et.y&&this.y+26=et.x&&this.x+26et.y&&this.y=et.x&&this.x+26et.y&&this.y+20=et.x&&this.x+26et.y&&this.y=et.x&&this.x+26et.y&&this .y+20=et.x&&this.x+26et.y&&this.y=et.x&&this.x+26et.y&&this.y+20=et.x&&this.x+26et.y&&this.y=et.x&&this.x+26et.y&&this.y+20 enems=new Vector(); public static Vector nodes=new Vector(); public static void writeRecord() { FileWriter fw=null; BufferedWriter bw=null; try { fw=new FileWriter("F:/java/tankGame/tankrecorde.txt"); bw=new BufferedWriter(fw); bw.write( getHitNum()+"\r\n"); bw.write( getEnNum()+"\r\n"); bw.write( getMyNum()+"\r\n"); for(int i=0;i getEnems() { return enems; } public static void setEnems(Vector enems) { GameRecord.enems = enems; } public static Vector getNodes() { return nodes; } public static void setNodes(Vector nodes) { GameRecord.nodes = nodes; } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档