日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java坦克大战项目

發布時間:2023/12/31 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java坦克大战项目 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java簡單的編程小項目,對坦克大戰小游戲做了一些改動,有圖形化界面,
計時器,血條,技能條,音樂播放,雙人模式和單人模式,地圖設計等等。

Freee類:界面設計


package tank; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.*; public class Freee {static JFrame jf = new JFrame("坦克大戰"); private static void firtPage() {// 1.設置窗體大小和標題jf.setPreferredSize(new Dimension(500, 300));// 2.設置關閉窗口就是關閉程序jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 最精準的布局模式空布局jf.setLayout(null);// 設置定位JLabel jl = new JLabel("坦克大戰", JLabel.CENTER);jl.setPreferredSize(new Dimension(680, 30));jf.add(jl);jl.setBounds(0, 0, 480, 30);jl.setFont(new Font("宋體", Font.BOLD, 24));jl.setForeground(Color.decode("#375a7f"));// 菜單欄// 新建一個菜單條JMenuBar jb = new JMenuBar();jf.add(jb);jb.setBounds(0, 40, 690, 30);jb.setBackground(Color.decode("#65991a"));// 新建一個菜單選項JMenu jmenu = new JMenu("開始游戲");jmenu.setPreferredSize(new Dimension(100, 30));jmenu.setForeground(Color.white);jb.add(jmenu);JMenuItem j = new JMenuItem("開始游戲");jmenu.add(j);// 新建一個菜單項JMenu jmenu0 = new JMenu("游戲模式");jmenu0.setPreferredSize(new Dimension(100, 30));jmenu0.setForeground(Color.white);jb.add(jmenu0);// 新建一個菜單項JMenuItem jm = new JMenuItem("單人模式");JMenuItem jmi = new JMenuItem("雙人模式");jmenu0.add(jm);jmenu0.add(jmi);// 以下是顯示位移的地方// 放置圖片JLabel jl3 = new JLabel(new ImageIcon("E:/java程序/新建文件夾/TankWarL/src/image/124.png"));jf.add(jl3);jl3.setBounds(0, 70, 480, 200);//開始監聽事件j.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//銷毀當前頁面closeThis();//打開一個新的頁面new TankClient2().lauchFrame();}});jm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//銷毀當前頁面closeThis();//打開一個新的頁面new TankClient2().lauchFrame();}});jmi.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//銷毀當前頁面closeThis();//打開一個新的頁面new TankClient().lauchFrame();}});// 3.設置窗體可見jf.pack();jf.setVisible(true);jf.setResizable(false); } public static void closeThis(){jf.dispose();} public static void main(String[] args) {firtPage(); }}

Tank類
我方坦克設計


package tank; import java.awt.*; import java.awt.event.*; import java.util.*; public class Tank { public static final int XSPEED = 5; public static final int YSPEED = 5;public static final int WIDTH = 10; public static final int HEIGHT = 10;private boolean live = true; private BloodBar bb = new BloodBar();private int life = 100; private int skill=20; private SkillBar cb=new SkillBar(); TankClient tc;private boolean good;private int x, y; private int oldX, oldY;private static Random r = new Random();private boolean bL=false, bU=false, bR=false, bD = false; enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};private Direction dir = Direction.STOP; private Direction ptDir = Direction.D;private int step = r.nextInt(12) + 3;public Tank(int x, int y, boolean good) {this.x = x;this.y = y;this.oldX = x;this.oldY = y;this.good = good; }public Tank(int x, int y, boolean good, Direction dir, TankClient tc) {this(x, y, good);this.dir = dir;this.tc = tc; }public void draw(Graphics g) {if(!live) {if(!good) {tc.tanks.remove(this);}return;}Color c = g.getColor();if(good) g.setColor(Color.RED);else g.setColor(Color.BLUE);switch(ptDir) {case L:case R:g.fill3DRect(x-6, y-5, 30, 5,false );g.fill3DRect(x-6, y+10, 30, 5, false);g.fill3DRect(x-2, y, 20, 10, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;case LU:case RU:case U:case RD:case D:case LD:g.fill3DRect(x-4, y-10, 5, 30,false );g.fill3DRect(x+11, y-10, 5, 30, false);g.fill3DRect(x+1, y-5, 10, 20, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;}if(good) { bb.draw(g);cb.draw(g);}switch(ptDir) {case L:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);break;case LU:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);break;case U:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);break;case RU:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y);break;case R:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);break;case RD:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);break;case D:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);break;case LD:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);break;}move(); }void move() {this.oldX = x;this.oldY = y;switch(dir) {case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;}if(this.dir != Direction.STOP) {this.ptDir = this.dir;}if(x < 0) x = 0;if(y < 30) y = 30;if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;if(!good) {Direction[] dirs = Direction.values();if(step == 0) {step = r.nextInt(12) + 3;int rn = r.nextInt(dirs.length);dir = dirs[rn];} step --;if(r.nextInt(40) > 38) this.fire();} }private void stay() {x = oldX;y = oldY; }public void keyPressed(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_LEFT :bL = true;break;case KeyEvent.VK_UP :bU = true;break;case KeyEvent.VK_RIGHT :bR = true;break;case KeyEvent.VK_DOWN :bD = true;break;}locateDirection(); } public void keyPressed2(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_A :bL = true;break;case KeyEvent.VK_W :bU = true;break;case KeyEvent.VK_D :bR = true;break;case KeyEvent.VK_S :bD = true;break;}locateDirection(); }void locateDirection() {if(bL && !bU && !bR && !bD) dir = Direction.L;else if(bL && bU && !bR && !bD) dir = Direction.LU;else if(!bL && bU && !bR && !bD) dir = Direction.U;else if(!bL && bU && bR && !bD) dir = Direction.RU;else if(!bL && !bU && bR && !bD) dir = Direction.R;else if(!bL && !bU && bR && bD) dir = Direction.RD;else if(!bL && !bU && !bR && bD) dir = Direction.D;else if(bL && !bU && !bR && bD) dir = Direction.LD;else if(!bL && !bU && !bR && !bD) dir = Direction.STOP; }public void keyReleased(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_CONTROL:fire();break;case KeyEvent.VK_LEFT :bL = false;break;case KeyEvent.VK_UP :bU = false;break;case KeyEvent.VK_RIGHT :bR = false;break;case KeyEvent.VK_DOWN :bD = false;break;case KeyEvent.VK_L :superFire();break;}locateDirection(); } public void keyReleased2(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_ENTER:fire();break;case KeyEvent.VK_A :bL = false;break;case KeyEvent.VK_W :bU = false;break;case KeyEvent.VK_D :bR = false;break;case KeyEvent.VK_S :bD = false;break;case KeyEvent.VK_J :superFire();break;}locateDirection(); } public Missile fire() {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, good, ptDir, this.tc);tc.missiles.add(m);return m; }public Missile fire(Direction dir) {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, good, dir, this.tc);tc.missiles.add(m);return m; }public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT); }public boolean isLive() {return live; }public void setLive(boolean live) {this.live = live; }public boolean isGood() {return good; }public boolean collidesWithWall(Wall w) {if(this.live && this.getRect().intersects(w.getRect())) {this.stay();return true;}return false; }public boolean collidesWithTanks(java.util.List<Tank> tanks) {for(int i=0; i<tanks.size(); i++) {Tank t = tanks.get(i);if(this != t) {if(this.live && t.isLive() && this.getRect().intersects(t.getRect())) {this.stay();t.stay();return true;}}}return false; }private void superFire() {Direction[] dirs = Direction.values();for(int i=0; i<8; i++) {fire(dirs[i]);}skill--; }public int getLife() {return life; }public void setLife(int life) {this.life = life; }private class BloodBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.RED);g.drawRect(x-4, y-20, WIDTH, 5);int w = WIDTH * life/100 ;g.fillRect(x-4, y-20, w, 5);g.setColor(c);} } private class SkillBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.BLUE);g.drawRect(x-4, y-30, WIDTH, 5);int w = WIDTH * skill/20 ;g.fillRect(x-4, y-30, w, 5);g.setColor(c);} } public boolean eat(Blood b) {if(this.live && b.isLive() && this.getRect().intersects(b.getRect())) {this.life = 100;this.skill=20;b.setLive(false);return true;}return false; }}

tank2
敵方坦克設計


package tank; import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List;import javax.swing.JOptionPane; public class Tank2 { public static final int XSPEED = 5; public static final int YSPEED = 5;public static final int WIDTH = 10; public static final int HEIGHT = 10;private boolean live = true; private BloodBar bb = new BloodBar();private int life = 100; private int skill=20; private SkillBar cb=new SkillBar(); TankClient2 tc2;private boolean good;private int x, y; private int oldX, oldY;private static Random r = new Random();private boolean bL=false, bU=false, bR=false, bD = false; enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};private Direction dir = Direction.STOP; private Direction ptDir = Direction.D;private int step = r.nextInt(12) + 3;public Tank2(boolean good, Direction dir, TankClient2 tc2) { x = r.nextInt(tc2.GAME_WIDTH-WIDTH+1);//初始化時在窗口范圍內的隨機坐標生成坦克y = 40+r.nextInt(tc2.GAME_HEIGHT-HEIGHT-40+1);//減去窗口的標題欄高度40this.good = good;oldX = x;oldY = y;this.tc2 = tc2;// 初始化tcthis.dir = dir;// 指定坦克對象的方向 }public void draw(Graphics g) {if(!live) {if(!good) {tc2.tanks2.remove(this);}return;}Color c = g.getColor();if(good) g.setColor(Color.RED);else g.setColor(Color.BLUE);switch(ptDir) {case L:case R:g.fill3DRect(x-6, y-5, 30, 5,false );g.fill3DRect(x-6, y+10, 30, 5, false);g.fill3DRect(x-2, y, 20, 10, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;case LU:case RU:case U:case RD:case D:case LD:g.fill3DRect(x-4, y-10, 5, 30,false );g.fill3DRect(x+11, y-10, 5, 30, false);g.fill3DRect(x+1, y-5, 10, 20, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;}switch(ptDir) {case L:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y + Tank2.HEIGHT/2);break;case LU:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y);break;case U:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH/2, y);break;case RU:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y);break;case R:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y + Tank2.HEIGHT/2);break;case RD:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y + Tank2.HEIGHT);break;case D:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH/2, y + Tank2.HEIGHT);break;case LD:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y + Tank2.HEIGHT);break;}g.setColor(c);if(good) { bb.draw(g);cb.draw(g);}move(); }void move() {this.oldX = x;this.oldY = y;switch(dir) {case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;}if(this.dir != Direction.STOP) {this.ptDir = this.dir;}if(x < 0) x = 0;if(y < 30) y = 30;if(x + Tank2.WIDTH > TankClient2.GAME_WIDTH) x = TankClient2.GAME_WIDTH - Tank2.WIDTH;if(y + Tank2.HEIGHT > TankClient2.GAME_HEIGHT) y = TankClient2.GAME_HEIGHT - Tank2.HEIGHT;if(!good) {Direction[] dirs = Direction.values();if(step == 0) {step = r.nextInt(12) + 3;int rn = r.nextInt(dirs.length);dir = dirs[rn];} step --;if(r.nextInt(40) > 38) this.fire();} }private void stay() {x = oldX;y = oldY; }public void keyPressed(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_LEFT :bL = true;break;case KeyEvent.VK_UP :bU = true;break;case KeyEvent.VK_RIGHT :bR = true;break;case KeyEvent.VK_DOWN :bD = true;break;case KeyEvent.VK_F2 :if(!this.live) {this.live = true;this.life = 100;this.skill=20;}break; }locateDirection(); }void locateDirection() {if(bL && !bU && !bR && !bD) dir = Direction.L;else if(bL && bU && !bR && !bD) dir = Direction.LU;else if(!bL && bU && !bR && !bD) dir = Direction.U;else if(!bL && bU && bR && !bD) dir = Direction.RU;else if(!bL && !bU && bR && !bD) dir = Direction.R;else if(!bL && !bU && bR && bD) dir = Direction.RD;else if(!bL && !bU && !bR && bD) dir = Direction.D;else if(bL && !bU && !bR && bD) dir = Direction.LD;else if(!bL && !bU && !bR && !bD) dir = Direction.STOP; }public void keyReleased(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_CONTROL:fire();TankClient2.playMusic2();break;case KeyEvent.VK_LEFT :bL = false;break;case KeyEvent.VK_UP :bU = false;break;case KeyEvent.VK_RIGHT :bR = false;break;case KeyEvent.VK_DOWN :bD = false;break;case KeyEvent.VK_J :superFire();TankClient2.playMusic2();break;case KeyEvent.VK_ESCAPE:JOptionPane.showMessageDialog(null,"是否退出","Tank",JOptionPane.PLAIN_MESSAGE );System.exit(0);;break; }locateDirection(); }public Missile2 fire() {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile2 m2 = new Missile2(x, y, good, ptDir, this.tc2);tc2.missiles2.add(m2);return m2; }public Missile2 fire(Direction dir) {if(!live) return null;int x = this.x + Tank2.WIDTH/2 - Missile2.WIDTH/2;int y = this.y + Tank2.HEIGHT/2 - Missile2.HEIGHT/2;Missile2 m2 = new Missile2(x, y, good, dir, this.tc2);tc2.missiles2.add(m2);return m2; }public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT); }public boolean isLive() {if (!live && !this.isGood() && r.nextInt(5) > 1) {// 如果敵方坦克死亡就有概率掉落血塊int x = this.x + WIDTH / 2 - Blood2.WIDTH / 2;// 血塊左上角橫坐標int y = this.y + HEIGHT / 2 - Blood2.HEIGHT / 2;// 血塊左上角縱坐標Blood2 b = new Blood2(x, y, tc2);tc2.bloods.add(b);}return live;}public void setLive(boolean live) {this.live = live; }public boolean isGood() {return good; }public boolean collidesWithWall2(Wall2 w) {if(this.live && this.getRect().intersects(w.getRect())) {this.stay();return true;}return false; }public boolean collidesWithTanks2(java.util.List<Tank2> tanks2) {for(int i=0; i<tanks2.size(); i++) {Tank2 t2 = tanks2.get(i);if(this != t2) {if(this.live && t2.isLive() && this.getRect().intersects(t2.getRect())) {this.stay();t2.stay();return true;}}}return false; }private void superFire() {if(skill>0) {Direction[] dirs = Direction.values();for(int i=0; i<8; i++) {fire(dirs[i]);}}skill--; }public int getLife() {return life; }public void setLife(int life) {this.life = life; }private class BloodBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.RED);g.drawRect(x-4, y-20, WIDTH, 5);int w = WIDTH * life/100 ;g.fillRect(x-4, y-20, w, 5);g.setColor(c);} } private class SkillBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.BLUE);g.drawRect(x-4, y-30, WIDTH, 5);int w = WIDTH * skill/20 ;g.fillRect(x-4, y-30, w, 5);g.setColor(c);} } public boolean eat(List<Blood2> bloods) {for (int i = 0; i < bloods.size(); i++) {Blood2 b = bloods.get(i);if (Blood2.isLive() && this.live && this.getRect().intersects(Blood2.getRect2())) {// 當血塊和我方坦克都活著且碰撞時this.life = 100;this.skill=20;b.setLive(false);return true;}}return false; } private void resurrect() {//滿血復活,復活地點隨機if (this.live == false) {this.live = true;this.life = life;if(tc2.score> 0)//復活一次扣1分,到零為止tc2.score -= 1;do{randomLocate();}while(collidesWithTanks2(tc2.tanks2));//如果復活點存在其他坦克,就換個地點,避免兩輛坦克卡住} } private void randomLocate() {//在窗口范圍內隨機生成坐標位置x = r.nextInt(tc2.GAME_WIDTH-WIDTH+1);y = 40+r.nextInt(tc2.GAME_HEIGHT-HEIGHT-40+1);//減去窗口的標題欄高度40 }}

TankClient
雙人模式


package tank; import java.awt.*; import java.awt.event.*; import java.util.List; import java.util.ArrayList; public class TankClient extends Frame { public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600;Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this); Tank myTank2 = new Tank(600, 50, true, Tank.Direction.STOP, this); Wall w1 = new Wall(200, 200, 50, 50, this), w2 = new Wall(300, 200, 50, 50, this),w3 = new Wall(400, 200, 50, 50, this),w4=new Wall(500,200,50,50,this); //Map Wall w5 = new Wall(350, 90, 50, 50, this),w6=new Wall(350,500,50,50,this),w7=new Wall(350,400,50,50,this),w8=new Wall(350,300,50,50,this); Wall w9 = new Wall(50, 400, 50, 50, this),w10=new Wall(650,400,50,50,this);List<Explode> explodes = new ArrayList<Explode>(); List<Missile> missiles = new ArrayList<Missile>(); List<Tank> tanks = new ArrayList<Tank>(); Image offScreenImage = null; Blood b = new Blood(); public void paint(Graphics g) {g.drawString("missiles count:" + missiles.size(), 10, 50);g.drawString("explodes count:" + explodes.size(), 10, 70);g.drawString("tanks count:" + tanks.size(), 10, 90);g.drawString("tanks life:" + myTank.getLife(), 10, 110);g.drawString("tanks2 life:" + myTank2.getLife(), 10, 130);if(tanks.size() <=8)for(int i=tanks.size(); i<10; i++) {tanks.add(new Tank(50 + 50*(i+1), 50, false, Tank.Direction.D, this));} for(int i=0; i<missiles.size(); i++) {Missile m = missiles.get(i);m.hitTanks(tanks);m.hitTank(myTank);m.hitTank(myTank2);m.hitWall(w1);m.hitWall(w2);m.hitWall(w3);m.hitWall(w4);m.hitWall(w5);m.hitWall(w6);m.hitWall(w7);m.hitWall(w8);m.hitWall(w9);m.hitWall(w10);m.draw(g);}for(int i=0; i<explodes.size(); i++) {Explode e = explodes.get(i);e.draw(g);}for(int i=0; i<tanks.size(); i++) {Tank t = tanks.get(i);t.collidesWithWall(w1);t.collidesWithWall(w2);t.collidesWithWall(w3);t.collidesWithWall(w4);t.collidesWithWall(w5);t.collidesWithWall(w6);t.collidesWithWall(w7);t.collidesWithWall(w8);t.collidesWithWall(w9);t.collidesWithWall(w10);t.collidesWithTanks(tanks);t.draw(g);}myTank.draw(g);myTank.eat(b);myTank2.draw(g);myTank2.eat(b);w1.draw(g);w2.draw(g);w3.draw(g);w4.draw(g);w5.draw(g);w6.draw(g);w7.draw(g);w8.draw(g);w9.draw(g);w10.draw(g);b.draw(g); }public void update(Graphics g) {if(offScreenImage == null) {offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);}Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScreen.getColor();gOffScreen.setColor(Color.orange);gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage, 0, 0, null); }public void lauchFrame() {for(int i=0; i<10; i++) {tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, this));}this.setSize(GAME_WIDTH, GAME_HEIGHT);this.setTitle("TankWar");this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});this.setResizable(false);this.setBackground(Color.WHITE);this.addKeyListener(new KeyMonitor());setVisible(true);new Thread(new PaintThread()).start(); }public static void main(String[] args) {TankClient tc = new TankClient();tc.lauchFrame(); }private class PaintThread implements Runnable {public void run() {while(true) {repaint();try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}} }private class KeyMonitor extends KeyAdapter {public void keyReleased(KeyEvent e) {myTank.keyReleased(e);myTank2.keyReleased2(e);}public void keyPressed(KeyEvent e) {myTank.keyPressed(e);myTank2.keyPressed2(e);}}}

TankClient2
單人模式
package tank;

import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import tank.Tank2.Direction; import java.util.ArrayList; public class TankClient2 extends Frame { public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; public List<Blood2> bloods = new ArrayList<Blood2>(); public int score = 0;// 初始得分 public int time = 2 * 60 * 1000;// 游戲剩余時間(毫秒) private boolean play = true;// 控制游戲暫停/開始 private static boolean restart = false;// 控制游戲重新開始 Tank2 myTank2 = new Tank2(true, Direction.STOP, this);Wall2 w1 = new Wall2(200, 200, 50, 50, this), w2 = new Wall2(300, 200, 50, 50, this),w3 = new Wall2(400, 200, 50, 50, this),w4=new Wall2(500,200,50,50,this); Wall2 w5 = new Wall2(350, 90, 50, 50, this),w6=new Wall2(350,500,50,50,this),w7=new Wall2(350,400,50,50,this),w8=new Wall2(350,300,50,50,this); Wall2 w9 = new Wall2(50, 400, 50, 50, this),w10=new Wall2(650,400,50,50,this);List<Explode2> explodes2 = new ArrayList<Explode2>(); List<Missile2> missiles2 = new ArrayList<Missile2>(); List<Tank2> tanks2 = new ArrayList<Tank2>(); Image offScreenImage = null; private void init() {// 用于重新開始時初始化數據score = 0;time = 2 * 60 * 1000;play = true;restart = false;myTank2 = new Tank2( true, Direction.STOP, this);tanks2.clear();missiles2.clear();//為了避免數據的疊加。就需要在加載前 用 數組.clear()清除數據 explodes2.clear();bloods.clear(); } public void paint(Graphics g) {if (time > 0)play1(g);// 游戲運行界面elsegameOver(g);// 游戲結束界面 } private void play1(Graphics g) {// TODO Auto-generated method stubg.drawString("missiles count:" + missiles2.size(), 10, 50);g.drawString("explodes count:" + explodes2.size(), 10, 70);g.drawString("tanks count:" + tanks2.size(), 10, 90);g.drawString("tanks life:" + myTank2.getLife(), 10, 110);//if(tanks.size()<=0) pation( g);Color c = g.getColor();// 取出當前顏色g.setColor(Color.black);// 畫筆設為白色g.drawString("YOUR SCORE: " + score, 10, 130);// 在合適坐標寫出當前得分g.drawString("YOUR TIME: " + new Time(time).timeToString(), 10, 150);// 在合適坐標寫出所剩時間for(int i=0; i<missiles2.size(); i++) {Missile2 m2 = missiles2.get(i);m2.hitTanks2(tanks2);m2.hitTank2(myTank2);m2.hitWall2(w1);m2.hitWall2(w2);m2.hitWall2(w3);m2.hitWall2(w4);m2.hitWall2(w5);m2.hitWall2(w6);m2.hitWall2(w7);m2.hitWall2(w8);m2.hitWall2(w9);m2.hitWall2(w10);m2.draw(g);//if(!m.isLive()) missiles.remove(m);//else m.draw(g);}for (int i = 0; i < bloods.size(); i++) {// 遍歷所有血塊并畫出Blood2 b = bloods.get(i);b.draw(g);}for(int i=0; i<explodes2.size(); i++) {Explode2 e2 = explodes2.get(i);e2.draw(g);}if (tanks2.size() == 0)for (int i = 0; i < 10; i++) {// 每當敵方坦克數量為0時就生成十輛tanks2.add(new Tank2( false, Direction.D, this));}for(int i=0; i<tanks2.size(); i++) {Tank2 t2 = tanks2.get(i);t2.collidesWithWall2(w1);t2.collidesWithWall2(w2);t2.collidesWithWall2(w3);t2.collidesWithWall2(w4);t2.collidesWithWall2(w5);t2.collidesWithWall2(w6);t2.collidesWithWall2(w7);t2.collidesWithWall2(w8);t2.collidesWithWall2(w9);t2.collidesWithWall2(w10);t2.collidesWithTanks2(tanks2);t2.draw(g);}myTank2.draw(g);myTank2.eat(bloods);w1.draw(g);w2.draw(g);w3.draw(g);w4.draw(g);w5.draw(g);w6.draw(g);w7.draw(g);w8.draw(g);w9.draw(g);w10.draw(g);//b.draw(g);} private void gameOver(Graphics g) {// TODO Auto-generated method stubColor c = g.getColor();g.setColor(Color.WHITE);g.setFont(new Font("宋體", Font.BOLD, 120));g.drawString("GAME OVER", 90, 280);g.setFont(new Font("宋體", Font.BOLD, 50));g.drawString("your score: " + score, 200, 450);g.setFont(new Font("宋體", Font.CENTER_BASELINE, 20));g.drawString("press R to play again", 270, 500);g.setColor(c); } public void update(Graphics g) {if(offScreenImage == null) {offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);}Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScreen.getColor();gOffScreen.setColor(Color.orange);gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage, 0, 0, null); }public void lauchFrame() {this.setSize(GAME_W.setTitle("TankWar");this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});this.setResizable(false);this.setBackground(Color.WHITE);this.addKeyListener(new KeyMonitor());setVisible(true);new Thread(new PaintThread()).start();} static void playMusic(){//背景音樂播放try {URL cb;File f = new File("E:\\java程序\\新建文件夾\\TankWarL\\src\\snd\\start.wav"); // 引號里面的是音樂文件所在的路徑cb = f.toURL();AudioClip aau;aau = Applet.newAudioClip(cb);aau.play(); //aau.loop();//循環播放//System.out.println("可以播放");// 循環播放 aau.play()//單曲 aau.stop()停止播放} catch (MalformedURLException e) {e.printStackTrace();} } static void playMusic2(){//背景音樂播放try {URL cb;File f = new File("E:\\java程序\\新建文件夾\\TankWarL\\src\\snd\\fire.wav"); // 引號里面的是音樂文件所在的路徑cb = f.toURL();AudioClip aau;aau = Applet.newAudioClip(cb);aau.play(); aau.loop();//循環播放//System.out.println("可以播放");// 循環播放 aau.play()//單曲 aau.stop()停止播放} catch (MalformedURLException e) {e.printStackTrace();} } static void playMusic3(){//背景音樂播放try {URL cb;File f = new File("E:\\java程序\\新建文件夾\\TankWarL\\src\\snd\\blast.wav"); // 引號里面的是音樂文件所在的路徑cb = f.toURL();AudioClip aau;aau = Applet.newAudioClip(cb);aau.play(); aau.loop();//循環播放//System.out.println("可以播放");// 循環播放 aau.play()//單曲 aau.stop()停止播放} catch (MalformedURLException e) {e.printStackTrace();} }public static void main(String[] args) {TankClient2 tc2 = new TankClient2();tc2.lauchFrame();playMusic(); }private class PaintThread implements Runnable {public void run() {while(true) {//while(true)出現的時候,就是表示程序一直執行{}中的語句,因為while()中始終為“真”。if (play) {// 游戲狀態if (time < 0 && restart)// 如果時間到了,若選擇重新開始則將所有數據初始化init();repaint();try {Thread.sleep(100);time -= 100;} catch (InterruptedException e) {e.printStackTrace();}}else try {Thread.sleep(100);//當前線程休眠} catch (InterruptedException e) {e.printStackTrace();//在命令行打印異常信息在程序中出錯的位置及原因。}} } }privat=e class KeyMonitor extends KeyAdapter {// 設置鍵盤監聽事件public void keyReleased(KeyEvent e) {myTank2.keyReleased(e);// 釋放按鍵時觸發事件int key = e.getKeyCode();if (key == KeyEvent.VK_SPACE && time >0) {// 游戲時間內如果按下空格鍵,游戲暫停/開始if (play)play = false;elseplay = true;}if (key == KeyEvent.VK_R && time < 0) // 如果游戲結束,按R鍵重新開始restart = true;}public void keyPressed(KeyEvent e) {// 當按下按鍵時觸發事件myTank2.keyPressed(e);// 調用對象方法,通過按鍵控制坦克移動方向}}}

Tank
我方坦克

package tank; import java.awt.*; import java.awt.event.*; import java.util.*; public class Tank { public static final int XSPEED = 5; public static final int YSPEED = 5;public static final int WIDTH = 10; public static final int HEIGHT = 10;private boolean live = true; private BloodBar bb = new BloodBar();private int life = 100; private int skill=20; private SkillBar cb=new SkillBar(); TankClient tc;private boolean good;private int x, y; private int oldX, oldY;private static Random r = new Random();private boolean bL=false, bU=false, bR=false, bD = false; enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};private Direction dir = Direction.STOP; private Direction ptDir = Direction.D;private int step = r.nextInt(12) + 3;public Tank(int x, int y, boolean good) {this.x = x;this.y = y;this.oldX = x;this.oldY = y;this.good = good; }public Tank(int x, int y, boolean good, Direction dir, TankClient tc) {this(x, y, good);this.dir = dir;this.tc = tc; }public void draw(Graphics g) {if(!live) {if(!good) {tc.tanks.remove(this);}return;}Color c = g.getColor();if(good) g.setColor(Color.RED);else g.setColor(Color.BLUE);switch(ptDir) {case L:case R:g.fill3DRect(x-6, y-5, 30, 5,false );g.fill3DRect(x-6, y+10, 30, 5, false);g.fill3DRect(x-2, y, 20, 10, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;case LU:case RU:case U:case RD:case D:case LD:g.fill3DRect(x-4, y-10, 5, 30,false );g.fill3DRect(x+11, y-10, 5, 30, false);g.fill3DRect(x+1, y-5, 10, 20, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;}if(good) { bb.draw(g);cb.draw(g);}switch(ptDir) {case L:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);break;case LU:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);break;case U:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);break;case RU:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y);break;case R:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);break;case RD:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);break;case D:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);break;case LD:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);break;}move(); }void move() {this.oldX = x;this.oldY = y;switch(dir) {case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;}if(this.dir != Direction.STOP) {this.ptDir = this.dir;}if(x < 0) x = 0;if(y < 30) y = 30;if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;if(!good) {Direction[] dirs = Direction.values();if(step == 0) {step = r.nextInt(12) + 3;int rn = r.nextInt(dirs.length);dir = dirs[rn];} step --;if(r.nextInt(40) > 38) this.fire();} }private void stay() {x = oldX;y = oldY; }public void keyPressed(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_LEFT :bL = true;break;case KeyEvent.VK_UP :bU = true;break;case KeyEvent.VK_RIGHT :bR = true;break;case KeyEvent.VK_DOWN :bD = true;break;}locateDirection(); } public void keyPressed2(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_A :bL = true;break;case KeyEvent.VK_W :bU = true;break;case KeyEvent.VK_D :bR = true;break;case KeyEvent.VK_S :bD = true;break;}locateDirection(); }void locateDirection() {if(bL && !bU && !bR && !bD) dir = Direction.L;else if(bL && bU && !bR && !bD) dir = Direction.LU;else if(!bL && bU && !bR && !bD) dir = Direction.U;else if(!bL && bU && bR && !bD) dir = Direction.RU;else if(!bL && !bU && bR && !bD) dir = Direction.R;else if(!bL && !bU && bR && bD) dir = Direction.RD;else if(!bL && !bU && !bR && bD) dir = Direction.D;else if(bL && !bU && !bR && bD) dir = Direction.LD;else if(!bL && !bU && !bR && !bD) dir = Direction.STOP; }public void keyReleased(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_CONTROL:fire();break;case KeyEvent.VK_LEFT :bL = false;break;case KeyEvent.VK_UP :bU = false;break;case KeyEvent.VK_RIGHT :bR = false;break;case KeyEvent.VK_DOWN :bD = false;break;case KeyEvent.VK_L :superFire();break;}locateDirection(); } public void keyReleased2(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_ENTER:fire();break;case KeyEvent.VK_A :bL = false;break;case KeyEvent.VK_W :bU = false;break;case KeyEvent.VK_D :bR = false;break;case KeyEvent.VK_S :bD = false;break;case KeyEvent.VK_J :superFire();break;}locateDirection(); } public Missile fire() {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, good, ptDir, this.tc);tc.missiles.add(m);return m; }public Missile fire(Direction dir) {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, good, dir, this.tc);tc.missiles.add(m);return m; }public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT); }public boolean isLive() {return live; }public void setLive(boolean live) {this.live = live; }public boolean isGood() {return good; }public boolean collidesWithWall(Wall w) {if(this.live && this.getRect().intersects(w.getRect())) {this.stay();return true;}return false; }public boolean collidesWithTanks(java.util.List<Tank> tanks) {for(int i=0; i<tanks.size(); i++) {Tank t = tanks.get(i);if(this != t) {if(this.live && t.isLive() && this.getRect().intersects(t.getRect())) {this.stay();t.stay();return true;}}}return false; }private void superFire() {Direction[] dirs = Direction.values();for(int i=0; i<8; i++) {fire(dirs[i]);}skill--; }public int getLife() {return life; }public void setLife(int life) {this.life = life; } private class =BloodBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.RED);g.drawRect(x-4, y-20, WIDTH, 5);int w = WIDTH * life/100 ;g.fillRect(x-4, y-20, w, 5);g.setColor(c);} } private class SkillBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.BLUE);g.drawRect(x-4, y-30, WIDTH, 5);int w = WIDTH * skill/20 ;g.fillRect(x-4, y-30, w, 5);g.setColor(c);} } public boolean eat(Blood b) {if(this.live && b.isLive() && this.getRect().intersects(b.getRect())) {this.life = 100;this.skill=20;b.setLive(false);return true;}return false; }}

Tank2
地方坦克配置

package tank; import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List;import javax.swing.JOptionPane; public class Tank2 { public static final int XSPEED = 5; public static final int YSPEED = 5;public static final int WIDTH = 10; public static final int HEIGHT = 10;private boolean live = true; private BloodBar bb = new BloodBar();private int life = 100; private int skill=20; private SkillBar cb=new SkillBar(); TankClient2 tc2;private boolean good;private int x, y; private int oldX, oldY;private static Random r = new Random();private boolean bL=false, bU=false, bR=false, bD = false; enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};private Direction dir = Direction.STOP; private Direction ptDir = Direction.D;private int step = r.nextInt(12) + 3;public Tank2(boolean good, Direction dir, TankClient2 tc2) { x = r.nextInt(tc2.GAME_WIDTH-WIDTH+1);//初始化時在窗口范圍內的隨機坐標生成坦克y = 40+r.nextInt(tc2.GAME_HEIGHT-HEIGHT-40+1);//減去窗口的標題欄高度40this.good = good;oldX = x;oldY = y;this.tc2 = tc2;// 初始化tcthis.dir = dir;// 指定坦克對象的方向 }public void draw(Graphics g) {if(!live) {if(!good) {tc2.tanks2.remove(this);}return;}Color c = g.getColor();if(good) g.setColor(Color.RED);else g.setColor(Color.BLUE);switch(ptDir) {case L:case R:g.fill3DRect(x-6, y-5, 30, 5,false );g.fill3DRect(x-6, y+10, 30, 5, false);g.fill3DRect(x-2, y, 20, 10, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;case LU:case RU:case U:case RD:case D:case LD:g.fill3DRect(x-4, y-10, 5, 30,false );g.fill3DRect(x+11, y-10, 5, 30, false);g.fill3DRect(x+1, y-5, 10, 20, false);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);break;}switch(ptDir) {case L:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y + Tank2.HEIGHT/2);break;case LU:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y);break;case U:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH/2, y);break;case RU:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y);break;case R:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y + Tank2.HEIGHT/2);break;case RD:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y + Tank2.HEIGHT);break;case D:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH/2, y + Tank2.HEIGHT);break;case LD:g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y + Tank2.HEIGHT);break;}g.setColor(c);if(good) { bb.draw(g);cb.draw(g);}move(); }void move() {this.oldX = x;this.oldY = y;switch(dir) {case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;}if(this.dir != Direction.STOP) {this.ptDir = this.dir;}if(x < 0) x = 0;if(y < 30) y = 30;if(x + Tank2.WIDTH > TankClient2.GAME_WIDTH) x = TankClient2.GAME_WIDTH - Tank2.WIDTH;if(y + Tank2.HEIGHT > TankClient2.GAME_HEIGHT) y = TankClient2.GAME_HEIGHT - Tank2.HEIGHT;if(!good) {Direction[] dirs = Direction.values();if(step == 0) {step = r.nextInt(12) + 3;int rn = r.nextInt(dirs.length);dir = dirs[rn];} step --;if(r.nextInt(40) > 38) this.fire();} }private void stay() {x = oldX;y = oldY; }public void keyPressed(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_LEFT :bL = true;break;case KeyEvent.VK_UP :bU = true;break;case KeyEvent.VK_RIGHT :bR = true;break;case KeyEvent.VK_DOWN :bD = true;break;case KeyEvent.VK_F2 :if(!this.live) {this.live = true;this.life = 100;this.skill=20;}break; }locateDirection(); }void locateDirection() {if(bL && !bU && !bR && !bD) dir = Direction.L;else if(bL && bU && !bR && !bD) dir = Direction.LU;else if(!bL && bU && !bR && !bD) dir = Direction.U;else if(!bL && bU && bR && !bD) dir = Direction.RU;else if(!bL && !bU && bR && !bD) dir = Direction.R;else if(!bL && !bU && bR && bD) dir = Direction.RD;else if(!bL && !bU && !bR && bD) dir = Direction.D;else if(bL && !bU && !bR && bD) dir = Direction.LD;else if(!bL && !bU && !bR && !bD) dir = Direction.STOP; }public void keyReleased(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_CONTROL:fire();TankClient2.playMusic2();break;case KeyEvent.VK_LEFT :bL = false;break;case KeyEvent.VK_UP :bU = false;break;case KeyEvent.VK_RIGHT :bR = false;break;case KeyEvent.VK_DOWN :bD = false;break;case KeyEvent.VK_J :superFire();TankClient2.playMusic2();break;case KeyEvent.VK_ESCAPE:JOptionPane.showMessageDialog(null,"是否退出","Tank",JOptionPane.PLAIN_MESSAGE );System.exit(0);;break; }locateDirection(); }public Missile2 fire() {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile2 m2 = new Missile2(x, y, good, ptDir, this.tc2);tc2.missiles2.add(m2);return m2; }public Missile2 fire(Direction dir) {if(!live) return null;int x = this.x + Tank2.WIDTH/2 - Missile2.WIDTH/2;int y = this.y + Tank2.HEIGHT/2 - Missile2.HEIGHT/2;Missile2 m2 = new Missile2(x, y, good, dir, this.tc2);tc2.missiles2.add(m2);return m2; }public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT); }public boolean isLive() {if (!live && !this.isGood() && r.nextInt(5) > 1) {// 如果敵方坦克死亡就有概率掉落血塊int x = this.x + WIDTH / 2 - Blood2.WIDTH / 2;// 血塊左上角橫坐標int y = this.y + HEIGHT / 2 - Blood2.HEIGHT / 2;// 血塊左上角縱坐標Blood2 b = new Blood2(x, y, tc2);tc2.bloods.add(b);}return live;}public void setLive(boolean live) {this.live = live; }public boolean isGood() {return good; }public boolean collidesWithWall2(Wall2 w) {if(this.live && this.getRect().intersects(w.getRect())) {this.stay();return true;}return false; }public boolean collidesWithTanks2(java.util.List<Tank2> tanks2) {for(int i=0; i<tanks2.size(); i++) {Tank2 t2 = tanks2.get(i);if(this != t2) {if(this.live && t2.isLive() && this.getRect().intersects(t2.getRect())) {this.stay();t2.stay();return true;}}}return false; }private void superFire() {if(skill>0) {Direction[] dirs = Direction.values();for(int i=0; i<8; i++) {fire(dirs[i]);}}skill--; }public int getLife() {return life; }public void setLife(int life) {this.life = life; }private class BloodBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.RED);g.drawRect(x-4, y-20, WIDTH, 5);int w = WIDTH * life/100 ;g.fillRect(x-4, y-20, w, 5);g.setColor(c);} } private class SkillBar {public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.BLUE);g.drawRect(x-4, y-30, WIDTH, 5);int w = WIDTH * skill/20 ;g.fillRect(x-4, y-30, w, 5);g.setColor(c);} } public boolean eat(List<Blood2> bloods) {for (int i = 0; i < bloods.size(); i++) {Blood2 b = bloods.get(i);if (Blood2.isLive() && this.live && this.getRect().intersects(Blood2.getRect2())) {// 當血塊和我方坦克都活著且碰撞時this.life = 100;this.skill=20;b.setLive(false);return true;}}return false; } private void resurrect() {//滿血復活,復活地點隨機if (this.live == false) {this.live = true;this.life = life;if(tc2.score> 0)//復活一次扣1分,到零為止tc2.score -= 1;do{randomLocate();}while(collidesWithTanks2(tc2.tanks2));//如果復活點存在其他坦克,就換個地點,避免兩輛坦克卡住} }private void randomLocate() {//在窗口范圍內隨機生成坐標位置x = r.nextInt(tc2.GAME_WIDTH-WIDTH+1);y = 40+r.nextInt(tc2.GAME_HEIGHT-HEIGHT-40+1);//減去窗口的標題欄高度40 }}

Blood
我方血塊

package tank;import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import tank.Tank2.Direction;import java.util.ArrayList;public class TankClient2 extends Frame { public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; public List<Blood2> bloods = new ArrayList<Blood2>(); public int score = 0;// 初始得分 public int time = 2 * 60 * 1000;// 游戲剩余時間(毫秒) private boolean play = true;// 控制游戲暫停/開始 private static boolean restart = false;// 控制游戲重新開始 Tank2 myTank2 = new Tank2(true, Direction.STOP, this);Wall2 w1 = new Wall2(200, 200, 50, 50, this), w2 = new Wall2(300, 200, 50, 50, this),w3 = new Wall2(400, 200, 50, 50, this),w4=new Wall2(500,200,50,50,this); //Map Wall2 w5 = new Wall2(350, 90, 50, 50, this),w6=new Wall2(350,500,50,50,this),w7=new Wall2(350,400,50,50,this),w8=new Wall2(350,300,50,50,this); Wall2 w9 = new Wall2(50, 400, 50, 50, this),w10=new Wall2(650,400,50,50,this);List<Explode2> explodes2 = new ArrayList<Explode2>(); List<Missile2> missiles2 = new ArrayList<Missile2>(); List<Tank2> tanks2 = new ArrayList<Tank2>(); Image offScreenImage = null; private void init() {// 用于重新開始時初始化數據score = 0;time = 2 * 60 * 1000;play = true;restart = false;myTank2 = new Tank2( true, Direction.STOP, this);tanks2.clear();missiles2.clear();//為了避免數據的疊加。就需要在加載前 用 數組.clear()清除數據 explodes2.clear();bloods.clear(); } public void paint(Graphics g) {//if(tanks.size()<=0) pation( g);if (time > 0)play1(g);// 游戲運行界面elsegameOver(g);// 游戲結束界面}private void play1(Graphics g) {// TODO Auto-generated method stubg.drawString("missiles count:" + missiles2.size(), 10, 50);g.drawString("explodes count:" + explodes2.size(), 10, 70);g.drawString("tanks count:" + tanks2.size(), 10, 90);g.drawString("tanks life:" + myTank2.getLife(), 10, 110);//if(tanks.size()<=0) pation( g);Color c = g.getColor();// 取出當前顏色g.setColor(Color.black);// 畫筆設為白色g.drawString("YOUR SCORE: " + score, 10, 130);// 在合適坐標寫出當前得分g.drawString("YOUR TIME: " + new Time(time).timeToString(), 10, 150);// 在合適坐標寫出所剩時間for(int i=0; i<missiles2.size(); i++) {Missile2 m2 = missiles2.get(i);m2.hitTanks2(tanks2);m2.hitTank2(myTank2);m2.hitWall2(w1);m2.hitWall2(w2);m2.hitWall2(w3);m2.hitWall2(w4);m2.hitWall2(w5);m2.hitWall2(w6);m2.hitWall2(w7);m2.hitWall2(w8);m2.hitWall2(w9);m2.hitWall2(w10);m2.draw(g);//if(!m.isLive()) missiles.remove(m);//else m.draw(g);}for (int i = 0; i < bloods.size(); i++) {// 遍歷所有血塊并畫出Blood2 b = bloods.get(i);b.draw(g);}for(int i=0; i<explodes2.size(); i++) {Explode2 e2 = explodes2.get(i);e2.draw(g);}if (tanks2.size() == 0)for (int i = 0; i < 10; i++) {// 每當敵方坦克數量為0時就生成十輛tanks2.add(new Tank2( false, Direction.D, this));}for(int i=0; i<tanks2.size(); i++) {Tank2 t2 = tanks2.get(i);t2.collidesWithWall2(w1);t2.collidesWithWall2(w2);t2.collidesWithWall2(w3);t2.collidesWithWall2(w4);t2.collidesWithWall2(w5);t2.collidesWithWall2(w6);t2.collidesWithWall2(w7);t2.collidesWithWall2(w8);t2.collidesWithWall2(w9);t2.collidesWithWall2(w10);t2.collidesWithTanks2(tanks2);t2.draw(g);}myTank2.draw(g);myTank2.eat(bloods);w1.draw(g);w2.draw(g);w3.draw(g);w4.draw(g);w5.draw(g);w6.draw(g);w7.draw(g);w8.draw(g);w9.draw(g);w10.draw(g);//b.draw(g);} private void gameOver(Graphics g) {// TODO Auto-generated method stubColor c = g.getColor();g.setColor(Color.WHITE);g.setFont(new Font("宋體", Font.BOLD, 120));g.drawString("GAME OVER", 90, 280);g.setFont(new Font("宋體", Font.BOLD, 50));g.drawString("your score: " + score, 200, 450);g.setFont(new Font("宋體", Font.CENTER_BASELINE, 20));g.drawString("press R to play again", 270, 500);g.setColor(c); } public void update(Graphics g) {if(offScreenImage == null) {offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);}Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScreen.getColor();gOffScreen.setColor(Color.orange);gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage, 0, 0, null); }public void lauchFrame() {//this.setLocation(400, 300);this.setSize(GAME_WIDTH, GAME_HEIGHT);this.setTitle("TankWar");this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});this.setResizable(false);this.setBackground(Color.WHITE);this.addKeyListener(new KeyMonitor());setVisible(true);new Thread(new PaintThread()).start();} static void playMusic(){//背景音樂播放try {URL cb;File f = new File("E:\\java程序\\新建文件夾\\TankWarL\\src\\snd\\start.wav"); // 引號里面的是音樂文件所在的路徑cb = f.toURL();AudioClip aau;aau = Applet.newAudioClip(cb);aau.play(); //aau.loop();//循環播放//System.out.println("可以播放");// 循環播放 aau.play()//單曲 aau.stop()停止播放} catch (MalformedURLException e) {e.printStackTrace();} } static void playMusic2(){//背景音樂播放try {URL cb;File f = new File("E:\\java程序\\新建文件夾\\TankWarL\\src\\snd\\fire.wav"); // 引號里面的是音樂文件所在的路徑cb = f.toURL();AudioClip aau;aau = Applet.newAudioClip(cb);aau.play(); aau.loop();//循環播放//System.out.println("可以播放");// 循環播放 aau.play()//單曲 aau.stop()停止播放} catch (MalformedURLException e) {e.printStackTrace();} } static void playMusic3(){//背景音樂播放try {URL cb;File f = new File("E:\\java程序\\新建文件夾\\TankWarL\\src\\snd\\blast.wav"); // 引號里面的是音樂文件所在的路徑cb = f.toURL();AudioClip aau;aau = Applet.newAudioClip(cb);aau.play(); aau.loop();//循環播放//System.out.println("可以播放");// 循環播放 aau.play()//單曲 aau.stop()停止播放} catch (MalformedURLException e) {e.printStackTrace();} }public static void main(String[] args) {TankClient2 tc2 = new TankClient2();tc2.lauchFrame();playMusic(); }private class PaintThread implements Runnable {public void run() {while(true) {//while(true)出現的時候,就是表示程序一直執行{}中的語句,因為while()中始終為“真”。if (play) {// 游戲狀態if (time < 0 && restart)// 如果時間到了,若選擇重新開始則將所有數據初始化init();repaint();try {Thread.sleep(100);time -= 100;} catch (InterruptedException e) {e.printStackTrace();}}else try {Thread.sleep(100);//當前線程休眠} catch (InterruptedException e) {e.printStackTrace();//在命令行打印異常信息在程序中出錯的位置及原因。}} } }private class KeyMonitor extends KeyAdapter {// 設置鍵盤監聽事件public void keyReleased(KeyEvent e) {myTank2.keyReleased(e);// 釋放按鍵時觸發事件int key = e.getKeyCode();if (key == KeyEvent.VK_SPACE && time >0) {// 游戲時間內如果按下空格鍵,游戲暫停/開始if (play)play = false;elseplay = true;}if (key == KeyEvent.VK_R && time < 0) // 如果游戲結束,按R鍵重新開始restart = true;}public void keyPressed(KeyEvent e) {// 當按下按鍵時觸發事件myTank2.keyPressed(e);// 調用對象方法,通過按鍵控制坦克移動方向}}}

Blood2
我方血塊
package tank;
import java.awt.*;

import tank.TankClient2;

public class Blood2 {

public static final int WIDTH = 10;// 寬度 public static final int HEIGHT = 10;// 高度 static int x; static int y; TankClient2 tc2; private static boolean live2 = true;// 默認活著 private int time = 500;// 存活時間,一段時間后血塊自動消失public Blood2(int x, int y, TankClient2 tc2) {this.x = x;this.y = y;this.tc2 = tc2; }public static boolean isLive() {return live2; }public void setLive( boolean live2) {this.live2 = live2; }public void draw(Graphics g) {if (time == 0)// 存活時間為0時血塊死亡live2 = false;if (!live2) {tc2.bloods.remove(this);// 將死亡的血塊移出容器,不畫出return;}Color c = g.getColor();g.setColor(Color.RED);g.fillRect(x, y, WIDTH, HEIGHT);// 畫出血塊g.setColor(c);time--;// 每畫一次,血塊存活時間減少 }/*private void move() {step ++;if(step == pos.length){step = 0;}x = pos[step][0];y = pos[step][1];}*/public static Rectangle getRect2() {return new Rectangle(x, y,WIDTH, HEIGHT); }}

Missle類
我方子彈

package tank;import java.awt.*; import java.util.List;public class Missile { public static final int XSPEED = 10; public static final int YSPEED = 10;public static final int WIDTH = 10; public static final int HEIGHT = 10;int x, y; Tank.Direction dir;private boolean good; private boolean live = true;private TankClient tc;public Missile(int x, int y, Tank.Direction dir) {this.x = x;this.y = y;this.dir = dir; }public Missile(int x, int y, boolean good, Tank.Direction dir, TankClient tc) {this(x, y, dir);this.good = good;this.tc = tc; }public void draw(Graphics g) {if(!live) {tc.missiles.remove(this);return;}Color c = g.getColor();if(good) {g.setColor(Color.white);}else { g.setColor(Color.red);}g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);move(); }private void move() {switch(dir) {case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;}if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {live = false;} }public boolean isLive() {return live; }public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT); }public boolean hitTank(Tank t) {if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {if(t.isGood()) {t.setLife(t.getLife()-20);if(t.getLife() <= 0) t.setLive(false);} else {t.setLive(false);}this.live = false;Explode e = new Explode(x, y, tc);tc.explodes.add(e);return true;}return false; }public boolean hitTanks(List<Tank> tanks) {for(int i=0; i<tanks.size(); i++) {if(hitTank(tanks.get(i))) {return true;}}return false; }public boolean hitWall(Wall w) {if(this.live && this.getRect().intersects(w.getRect())) {this.live = false;return true;}return false; }}

Missle2類
package tank;

import java.awt.*; import java.util.List;public class Missile2 { public static final int XSPEED = 10; public static final int YSPEED = 10;public static final int WIDTH = 10; public static final int HEIGHT = 10;int x, y; Tank2.Direction dir;private boolean good; private boolean live = true;private TankClient2 tc2;public Missile2(int x, int y, Tank2.Direction dir) {this.x = x;this.y = y;this.dir = dir; }public Missile2(int x, int y, boolean good, Tank2.Direction dir, TankClient2 tc2) {this(x, y, dir);this.good = good;this.tc2 = tc2; }public void draw(Graphics g) {if(!live) {tc2.missiles2.remove(this);return;}Color c = g.getColor();if(good) {g.setColor(Color.BLACK);}else { g.setColor(Color.red);}g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);move(); }private void move() {switch(dir) {case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;}if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {live = false;} }public boolean isLive() {return live; }public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT); }public boolean hitTank2(Tank2 t) {if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {if(t.isGood()) {t.setLife(t.getLife()-20);if(t.getLife() <= 0) t.setLive(false);} else {t.setLive(false);}if (this.good)// 如果我方子彈擊中敵方,我方得分tc2.score++;this.live = false;// 子彈死亡Explode2 e = new Explode2(x, y, tc2);// 擊中坦克時產生爆炸tc2.explodes2.add(e);return true;// 判定子彈擊中坦克}return false; }public boolean hitTanks2(List<Tank2> tanks2) {for(int i=0; i<tanks2.size(); i++) {if(hitTank2(tanks2.get(i))) {return true;}}return false; }public boolean hitWall2(Wall2 w) {if(this.live && this.getRect().intersects(w.getRect())) {this.live = false;return true;}return false; }}

Explore類
爆炸

package tank;import java.awt.*; public class Explode { int x, y; private boolean live = true;private TankClient tc ; private static Toolkit tk = Toolkit.getDefaultToolkit();private static Image[] imgs = {tk.getImage(Explode.class.getClassLoader().getResource("image/blast_1.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/blast_2.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/blast_3.gif")),tk.getImage(Explode.class.getClassLoader().getResource("image/blast_4.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/blast_5.gif")), }; int step = 0; private static boolean init=false; public Explode(int x, int y, TankClient tc) {this.x = x;this.y = y;this.tc = tc; }public void draw(Graphics g) {if(!init){for (int i = 0; i < imgs.length; i++) {g.drawImage(imgs[i], 50, 50, null);}init=true;}if(!live) {tc.explodes.remove(this);return;}if(step == imgs.length) {live = false;step = 0;return;}g.drawImage(imgs[step], x-64, y-64, null);step ++; }}

Explore2
package tank;

import java.awt.*;

public class Explode2 {
int x, y;
private boolean live = true;

private TankClient2 tc2 ;//int[] diameter = {4, 7, 12, 18, 26, 32, 49, 30, 14, 6}; int step = 0; private static Toolkit tk = Toolkit.getDefaultToolkit(); // 加載圖片,使用到了反射機制private static Image[] imgs = {tk.getImage(Explode.class.getClassLoader().getResource("image/blast_1.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/blast_2.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/blast_3.gif")),tk.getImage(Explode.class.getClassLoader().getResource("image/blast_4.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/blast_5.gif")), };private static boolean init=false; public Explode2(int x, int y, TankClient2 tc2) {this.x = x;this.y = y;this.tc2 = tc2; }public void draw(Graphics g) {if(!init){for (int i = 0; i < imgs.length; i++) {g.drawImage(imgs[i], 50, 50, null);}init=true;}if(!live) {tc2.explodes2.remove(this);return;}if(step == imgs.length) {live = false;step = 0;return;}g.drawImage(imgs[step], x-64, y-64, null);step ++; }}

Time類
計時

package tank;public class Time {// 顯示游戲倒計時 int time;// 剩余時間(毫秒)public Time(int time) {this.time = time; }int minute;// 剩余分鐘 int second;// 減去剩余分鐘后的剩余秒public String timeToString() {// 返回剩余時間的字符串表示minute = time / 1000 / 60;second = time / 1000 % 60;if (second > 9)// 剩余的秒數為個位數時十位補零return minute + ": " + second;return minute + ": 0" + second; }}

Wall類 墻壁
package tank;

import java.awt.*;

public class Wall {
int x, y, w, h;
TankClient tc ;
Image n=Toolkit.getDefaultToolkit().getImage("");
private static Toolkit tk = Toolkit.getDefaultToolkit();
// 加載圖片,使用到了反射機制
private static Image[] imgs = {
tk.getImage(Explode.class.getClassLoader().getResource(“image/grass.gif”)),
tk.getImage(Explode.class.getClassLoader().getResource(“image/water.gif”)),
tk.getImage(Explode.class.getClassLoader().getResource(“image/steel.gif”)),

}; public Wall(int x, int y, int w, int h, TankClient tc) {this.x = x;this.y = y;this.w = w;this.h = h;this.tc = tc; }public void draw(Graphics g) {int m=w/10;int n1=h/10;g.setColor(Color.red);g.fillRect(x, y, w, h);g.setColor(Color.BLACK);g.drawRect(x, y, w, h);g.drawLine(x, y, x+w, y);g.drawLine(x, y+n1, x+w, y+n1);g.drawLine(x+2*m, y, x+2*m, y+n1);g.drawLine(x+4*m, y, x+4*m, y+n1);g.drawLine(x+6*m, y, x+6*m, y+n1);g.drawLine(x+8*m, y, x+8*m, y+n1);g.drawLine(x+10*m, y, x+10*m, y+n1);g.drawLine(x, y+2*n1, x+w, y+2*n1);g.drawLine(x+m, y+n1, x+m, y+2*n1);g.drawLine(x+3*m, y+n1, x+3*m, y+2*n1);g.drawLine(x+5*m, y+n1, x+5*m, y+2*n1);g.drawLine(x+7*m, y+n1, x+7*m, y+2*n1);g.drawLine(x+9*m, y+n1, x+9*m, y+2*n1);g.drawLine(x, y+3*n1, x+10*m, y+3*n1);g.drawLine(x+2*m, y+2*n1, x+2*m, y+3*n1);g.drawLine(x+4*m, y+2*n1, x+4*m, y+3*n1);g.drawLine(x+6*m, y+2*n1, x+6*m, y+3*n1);g.drawLine(x+8*m, y+2*n1, x+8*m, y+3*n1);g.drawLine(x+10*m, y+2*n1, x+10*m, y+3*n1);g.drawLine(x, y+4*n1, x+10*m, y+4*n1);g.drawLine(x+m, y+3*n1, x+m, y+4*n1);g.drawLine(x+3*m, y+3*n1, x+3*m, y+4*n1);g.drawLine(x+5*m, y+3*n1, x+5*m, y+4*n1);g.drawLine(x+7*m, y+3*n1, x+7*m, y+4*n1);g.drawLine(x+9*m, y+3*n1, x+9*m, y+4*n1);g.drawLine(x, y+5*n1, x+10*m, y+5*n1);g.drawLine(x+2*m, y+4*n1, x+2*m, y+5*n1);g.drawLine(x+4*m, y+4*n1, x+4*m, y+5*n1);g.drawLine(x+6*m, y+4*n1, x+6*m, y+5*n1);g.drawLine(x+8*m, y+4*n1, x+8*m, y+5*n1);g.drawLine(x+10*m, y+4*n1, x+10*m, y+5*n1);g.drawLine(x, y+6*n1, x+10*m, y+6*n1);g.drawLine(x+m, y+5*n1, x+m, y+6*n1);g.drawLine(x+3*m, y+5*n1, x+3*m, y+6*n1);g.drawLine(x+5*m, y+5*n1, x+5*m, y+6*n1);g.drawLine(x+7*m, y+5*n1, x+7*m, y+6*n1);g.drawLine(x+9*m, y+5*n1, x+9*m, y+6*n1);g.drawLine(x, y+7*n1, x+10*m, y+7*n1);g.drawLine(x+2*m, y+6*n1, x+2*m, y+7*n1);g.drawLine(x+4*m, y+6*n1, x+4*m, y+7*n1);g.drawLine(x+6*m, y+6*n1, x+6*m, y+7*n1);g.drawLine(x+8*m, y+6*n1, x+8*m, y+7*n1);g.drawLine(x+10*m, y+6*n1, x+10*m, y+7*n1);g.drawLine(x, y+8*n1, x+10*m, y+8*n1);g.drawLine(x+m, y+7*n1, x+m, y+8*n1);g.drawLine(x+3*m, y+7*n1, x+3*m, y+8*n1);g.drawLine(x+5*m, y+7*n1, x+5*m, y+8*n1);g.drawLine(x+7*m, y+7*n1, x+7*m, y+8*n1);g.drawLine(x+9*m, y+7*n1, x+9*m, y+8*n1);g.drawLine(x, y+9*n1, x+10*m, y+9*n1);g.drawLine(x+2*m, y+8*n1, x+2*m, y+9*n1);g.drawLine(x+4*m, y+8*n1, x+4*m, y+9*n1);g.drawLine(x+6*m, y+8*n1, x+6*m, y+9*n1);g.drawLine(x+8*m, y+8*n1, x+8*m, y+9*n1);g.drawLine(x+10*m, y+8*n1, x+10*m, y+9*n1); if(x>80) g.drawImage(imgs[0], x-10*m, y, w,h,null); if(x>340&&y<310&&y>290) for(int i=0;i<3;i++) g.drawImage(imgs[1], x+10*m+10*m*i, y, w,h,null); if(x>340&&y<310&&y>290) for(int i=0;i<3;i++) g.drawImage(imgs[1], x-20*m-10*m*i, y, w,h,null);if (y>200&&x>100) {g.drawImage(imgs[1], x-20*m, y, w,h,null);}if (y>200&&x<100)for(int i=0;i<3;i++) g.drawImage(imgs[1], x+10*m+10*m*i, y, w,h,null);if(x>350&&x<700&&y>350) for(int i=0;i<3;i++) g.drawImage(imgs[0], x-30*m-10*i*m, y, w,h,null); } public Rectangle getRect() {return new Rectangle(x, y, w, h); }}

Wall2

package tank;import java.awt.*; public class Wall2 { int x, y, w, h; TankClient2 tc2 ; Image n=Toolkit.getDefaultToolkit().getImage(""); private static Toolkit tk = Toolkit.getDefaultToolkit(); // 加載圖片,使用到了反射機制private static Image[] imgs = {tk.getImage(Explode.class.getClassLoader().getResource("image/grass.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/water.gif")), tk.getImage(Explode.class.getClassLoader().getResource("image/steel.gif")), }; public Wall2(int x, int y, int w, int h, TankClient2 tc2) {this.x = x;this.y = y;this.w = w;this.h = h;this.tc2 = tc2; }public void draw(Graphics g) {int m=w/10;int n1=h/10;g.setColor(Color.red);g.fillRect(x, y, w, h);g.setColor(Color.BLACK);g.drawRect(x, y, w, h);g.drawLine(x, y, x+w, y);g.drawLine(x, y+n1, x+w, y+n1);g.drawLine(x+2*m, y, x+2*m, y+n1);g.drawLine(x+4*m, y, x+4*m, y+n1);g.drawLine(x+6*m, y, x+6*m, y+n1);g.drawLine(x+8*m, y, x+8*m, y+n1);g.drawLine(x+10*m, y, x+10*m, y+n1);g.drawLine(x, y+2*n1, x+w, y+2*n1);g.drawLine(x+m, y+n1, x+m, y+2*n1);g.drawLine(x+3*m, y+n1, x+3*m, y+2*n1);g.drawLine(x+5*m, y+n1, x+5*m, y+2*n1);g.drawLine(x+7*m, y+n1, x+7*m, y+2*n1);g.drawLine(x+9*m, y+n1, x+9*m, y+2*n1);g.drawLine(x, y+3*n1, x+10*m, y+3*n1);g.drawLine(x+2*m, y+2*n1, x+2*m, y+3*n1);g.drawLine(x+4*m, y+2*n1, x+4*m, y+3*n1);g.drawLine(x+6*m, y+2*n1, x+6*m, y+3*n1);g.drawLine(x+8*m, y+2*n1, x+8*m, y+3*n1);g.drawLine(x+10*m, y+2*n1, x+10*m, y+3*n1);g.drawLine(x, y+4*n1, x+10*m, y+4*n1);g.drawLine(x+m, y+3*n1, x+m, y+4*n1);g.drawLine(x+3*m, y+3*n1, x+3*m, y+4*n1);g.drawLine(x+5*m, y+3*n1, x+5*m, y+4*n1);g.drawLine(x+7*m, y+3*n1, x+7*m, y+4*n1);g.drawLine(x+9*m, y+3*n1, x+9*m, y+4*n1);g.drawLine(x, y+5*n1, x+10*m, y+5*n1);g.drawLine(x+2*m, y+4*n1, x+2*m, y+5*n1);g.drawLine(x+4*m, y+4*n1, x+4*m, y+5*n1);g.drawLine(x+6*m, y+4*n1, x+6*m, y+5*n1);g.drawLine(x+8*m, y+4*n1, x+8*m, y+5*n1);g.drawLine(x+10*m, y+4*n1, x+10*m, y+5*n1);g.drawLine(x, y+6*n1, x+10*m, y+6*n1);g.drawLine(x+m, y+5*n1, x+m, y+6*n1);g.drawLine(x+3*m, y+5*n1, x+3*m, y+6*n1);g.drawLine(x+5*m, y+5*n1, x+5*m, y+6*n1);g.drawLine(x+7*m, y+5*n1, x+7*m, y+6*n1);g.drawLine(x+9*m, y+5*n1, x+9*m, y+6*n1);g.drawLine(x, y+7*n1, x+10*m, y+7*n1);g.drawLine(x+2*m, y+6*n1, x+2*m, y+7*n1);g.drawLine(x+4*m, y+6*n1, x+4*m, y+7*n1);g.drawLine(x+6*m, y+6*n1, x+6*m, y+7*n1);g.drawLine(x+8*m, y+6*n1, x+8*m, y+7*n1);g.drawLine(x+10*m, y+6*n1, x+10*m, y+7*n1);g.drawLine(x, y+8*n1, x+10*m, y+8*n1);g.drawLine(x+m, y+7*n1, x+m, y+8*n1);g.drawLine(x+3*m, y+7*n1, x+3*m, y+8*n1);g.drawLine(x+5*m, y+7*n1, x+5*m, y+8*n1);g.drawLine(x+7*m, y+7*n1, x+7*m, y+8*n1);g.drawLine(x+9*m, y+7*n1, x+9*m, y+8*n1);g.drawLine(x, y+9*n1, x+10*m, y+9*n1);g.drawLine(x+2*m, y+8*n1, x+2*m, y+9*n1);g.drawLine(x+4*m, y+8*n1, x+4*m, y+9*n1);g.drawLine(x+6*m, y+8*n1, x+6*m, y+9*n1);g.drawLine(x+8*m, y+8*n1, x+8*m, y+9*n1);g.drawLine(x+10*m, y+8*n1, x+10*m, y+9*n1); if(x>80) g.drawImage(imgs[0], x-10*m, y, w,h,null); if(x>340&&y<310&&y>290) for(int i=0;i<3;i++) g.drawImage(imgs[1], x+10*m+10*m*i, y, w,h,null); if(x>340&&y<310&&y>290) for(int i=0;i<3;i++) g.drawImage(imgs[1], x-20*m-10*m*i, y, w,h,null);if (y>200&&x>100) {g.drawImage(imgs[1], x-20*m, y, w,h,null);}if (y>200&&x<100)for(int i=0;i<3;i++) g.drawImage(imgs[1], x+10*m+10*m*i, y, w,h,null);if(x>350&&x<700&&y>350) for(int i=0;i<3;i++) g.drawImage(imgs[0], x-30*m-10*i*m, y, w,h,null); }public Rectangle getRect() {return new Rectangle(x, y, w, h); }}

總結

以上是生活随笔為你收集整理的java坦克大战项目的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。