java簡(jiǎn)單坦克大戰(zhàn)制作代碼
利用Java語(yǔ)言中的集合、Swing、線程等知識(shí)點(diǎn)編寫一個(gè)坦克大戰(zhàn)游戲。
(1) 畫出敵我坦克的原理:
在坦克類里面有一個(gè)布爾類型變量good。用于判斷坦克的陣營(yíng),在創(chuàng)建坦克對(duì)象時(shí)在Tank類的構(gòu)造方法中傳入good的值。在畫坦克的時(shí)候判斷good的值,區(qū)分?jǐn)澄姨箍说念伾?br />
(2) 坦克運(yùn)動(dòng)的原理:
在坦克類里寫入了監(jiān)聽鍵盤摁鍵的響應(yīng)事件,對(duì)監(jiān)聽到的上下左右鍵進(jìn)行記錄,并合成坦克移動(dòng)的八個(gè)方向的變量。之后對(duì)應(yīng)每個(gè)方向的不同對(duì)坦克坐標(biāo)x,y的值做響應(yīng)的更改實(shí)現(xiàn)我方坦克的移動(dòng)。而敵方坦克則自動(dòng)移動(dòng),通過隨機(jī)數(shù)對(duì)敵方坦克移動(dòng)方向的隨機(jī),并且隨機(jī)出每次移動(dòng)的次數(shù)。兩個(gè)隨機(jī)值相結(jié)合即實(shí)現(xiàn)了敵方坦克的移動(dòng)。
(3) 坦克發(fā)射子彈的原理:
通過鍵盤監(jiān)聽,檢測(cè)到發(fā)射子彈命令后將主類的子彈類集合中添加一個(gè)子彈類。將炮筒的方向以及坦克的位置以及坦克的陣營(yíng)傳入給子彈類,在主類paint畫方法中一直循環(huán)子彈類集合,如果集合內(nèi)有子彈,就畫出來。這樣就實(shí)現(xiàn)了發(fā)射子彈。
(4) 坦克、子彈、墻的碰撞原理:
在坦克類子彈類墻類中分別getRect方法獲取自身的范圍,然后在每次畫坦克、子彈時(shí)都會(huì)進(jìn)行相應(yīng)的碰撞檢測(cè)(在坦克類里有與墻和出自己外的坦克相撞的處理方法、在子彈類里有與墻和坦克相碰撞的處理方法。),如果自身與不該碰撞的物體的范圍相重合,則代表兩物體相撞。
(5)坦克加血的原理:
在血塊類中有血塊與我方坦克相碰撞的處理方法,如果血塊范圍與坦克范圍重合則血塊類死亡,并且坦克類的血量回復(fù)置滿。
(6)坦克復(fù)活的原理:
通過鍵盤監(jiān)聽,檢測(cè)到我方坦克復(fù)活命令后,如果我方坦克處于死亡狀態(tài),則將我方坦克存貨狀態(tài)改為活著并且將我方坦克血量回置滿血。
編程思想:
坦克大戰(zhàn)的編程思想在主類開啟一個(gè)線程,沒50毫秒循環(huán)一次畫方法(繪制整個(gè)界面內(nèi)的所有東西)。畫的東西有敵我坦克(顏色區(qū)分)、子彈、墻、血塊、爆炸。所以總共寫出了幾個(gè)類:Tank坦克類、Missile子彈類、Wall墻類、Blood血塊類、TankClient主類。在每一個(gè)類中均寫有畫方法實(shí)現(xiàn)本類屬性的繪制功能。在主類中有鍵盤監(jiān)聽事件調(diào)用這Tank類的鍵盤監(jiān)聽事件。通過鍵盤監(jiān)聽判斷出對(duì)Tank做出相應(yīng)的移動(dòng),而敵方Tank則是隨機(jī)運(yùn)動(dòng)。并且每次刷新都有調(diào)用各類的碰撞方法,判斷一些不該碰撞的對(duì)象的情況時(shí)做出處理。而每個(gè)對(duì)象的創(chuàng)建例如子彈這些是在觸發(fā)產(chǎn)生之后將新建子彈類加入一個(gè)子彈類集合之中,在繪制的時(shí)候判斷集合中的數(shù)量進(jìn)行繪制,出界或者打死坦克則在集合中刪除。其他類也均相似,不在細(xì)說。
代碼中每步都注釋有相應(yīng)的解釋。
TankClient.java
import java.awt.Color; import java.awt.Font; 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.util.ArrayList; import java.util.List; import javax.swing.JFrame; public class TankClient extends JFrame{ /** * @param args */ Image OffScrennImage = null; //雙緩沖內(nèi)存圖片存儲(chǔ) /*游戲大小*/ public static final int GAME_WIDTH = 800; //界面寬 public static final int GAME_HEIGTH = 600; //界面高 Tank myTank = new Tank(500,400,true,Color.red,Tank.Direction.STOP, this);//我方坦克類 List<Missile> missiles = new ArrayList<Missile>();//子彈的集合 List<Explode> explode = new ArrayList<Explode>();//爆炸集合 List<Tank> tanks = new ArrayList<Tank>(); //坦克集合 Wall wall1 = new Wall(150,200,20,300,this); //墻1 Wall wall2 = new Wall(250,500,300,20,this); //墻2 Wall wall3 = new Wall(650,200,20,300,this); //墻2 Wall wall4 = new Wall(250,300,300,20,this); //墻2 Wall wb = new Wall(750,550,40,40,this); //墻2 Blood b = new Blood(); //血類 public static void main(String[] args) { // TODO Auto-generated method stub TankClient tc=new TankClient(); tc.lauchFrame(); } private void lauchFrame() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++){ tanks.add(new Tank(50+40*(i+1), 50, false,Color.blue,Tank.Direction.D, this)); } this.setLocation(100, 100); //窗口初始坐標(biāo)點(diǎn) this.setSize(GAME_WIDTH, GAME_HEIGTH); //窗口初始大小 this.setTitle("TankWar"); //窗口名稱 /*窗口監(jiān)聽*/ this.addWindowListener(new WindowAdapter() { @Override /*點(diǎn)退出叉之后運(yùn)行*/ public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); //退出 } }); this.addKeyListener(new KeyMoniton()); //設(shè)置鍵盤監(jiān)聽 this.setVisible(true); //設(shè)置窗口顯現(xiàn) this.setResizable(false); //設(shè)置窗口不可改變大小 this.getContentPane().setBackground(Color.green); //設(shè)置窗口前景色為綠色 new Thread(new PaintThread()).start(); //開始運(yùn)行PaintThread類run } @Override public void paint(Graphics g) { // TODO Auto-generated method stub //Graphics為畫筆類 super.paint(g); myTank.draw(g); wall1.draw(g); wall2.draw(g); wall3.draw(g); wall4.draw(g); wb.draw(g); b.draw(g); myTank.eatBlood(b); myTank.hitWall(wall1); myTank.hitWall(wall2); myTank.hitWall(wall3); myTank.hitWall(wall4); /*循環(huán)子彈集合*/ for (int i = 0; i < missiles.size(); i++){ Missile m = missiles.get(i); //獲取當(dāng)前子彈 m.hitTanks(tanks); //自己子彈打死敵方坦克 m.hitWall(wall1); //子彈與墻 m.hitWall(wall2); m.hitWall(wall3); m.hitWall(wall4); m.hitTank(myTank);//敵人子彈打擊自己的坦克 m.draw(g); //畫子彈 } for (int i = 0; i < explode.size(); i++){ explode.get(i).draw(g); //畫爆炸 } for (int i = 0; i < tanks.size(); i++){ Tank t = tanks.get(i); t.draw(g); //畫敵方坦克 t.hitTanks(tanks); t.hitWall(wall1); //坦克與墻 t.hitWall(wall2); t.hitWall(wall3); t.hitWall(wall4); } //g.setFont(new Font("宋體",Font.BOLD,20)); g.drawString("missiles count:"+missiles.size(), 10, 50);//顯示 g.drawString("explode count:"+explode.size(), 10, 80);//顯示 g.drawString("tanks count:"+tanks.size(),10, 110); g.drawString("myTank Life:"+myTank.getLife(), 10, 130); g.drawString("回血:", 750, 540); g.drawString("方向鍵移動(dòng)方向;E:釋放移動(dòng)血快", 10, 590); g.drawString("z:發(fā)射東風(fēng)-31;a:發(fā)射東風(fēng)-41;", 10, 570); g.drawString("F2:復(fù)活;F3:敵方復(fù)活(對(duì)多20)", 10, 550); g.drawString("R:位置還原;Q:血量加滿", 10, 530); } @Override /*repaint-〉update->paint*/ public void update(Graphics g) { // TODO Auto-generated method stub super.update(g); if(OffScrennImage == null) OffScrennImage = this.createImage(GAME_WIDTH, GAME_HEIGTH); Graphics goffscrenn = OffScrennImage.getGraphics(); //設(shè)置一個(gè)內(nèi)存畫筆顏色為前景圖片顏色 Color c = goffscrenn.getColor(); //還是先保存前景顏色 goffscrenn.setColor(Color.green); //設(shè)置內(nèi)存畫筆顏色為綠色 goffscrenn.fillRect(0, 0, GAME_WIDTH, GAME_HEIGTH); //畫成圖片,大小為游戲大小 goffscrenn.setColor(c); //還原顏色 g.drawImage(OffScrennImage, 0, 0, null); //在界面畫出保存的圖片 paint(goffscrenn); //把內(nèi)存畫筆調(diào)用給paint } private class PaintThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub while(true){ repaint(); //運(yùn)行順序repaint->update->paint try{ Thread.sleep(50); //每隔50毫秒刷新畫面一次 }catch(Exception e){ e.printStackTrace(); } } } } /*鍵盤響應(yīng)*/ private class KeyMoniton extends KeyAdapter{ /*摁下鍵盤響應(yīng)*/ @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub super.keyPressed(e); myTank.KeyPressed(e); } /*抬起鍵盤響應(yīng)*/ @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub super.keyReleased(e); myTank.keyReleased(e); } } }
Tank.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.KeyEvent; import java.util.List; import java.util.Random; import javax.swing.ImageIcon; public class Tank { /*坦克本身數(shù)據(jù)*/ int x, y;//坦克坐標(biāo) private int oldX, oldY; //坦克上一步坐標(biāo) public static final int Whith = 30; //坦克寬 public static final int Higth = 30; //坦克高 public static final int XSPEED = 5; //橫向移動(dòng)速度 public static final int YSPEED = 5; //縱向移動(dòng)速度 private Color color; //坦克顏色 private boolean bL=false, bU=false, bR=false, bD=false; //四個(gè)方向控制值 enum Direction {L, LU, U, RU, R, RD, D, LD, STOP}; //由四個(gè)方向值合成八個(gè)方向的移動(dòng) private Direction dir = Direction.STOP; //出場(chǎng)方向 private Direction ptDir = Direction.D; //炮筒初始方向 private boolean good; //判斷坦克的陣營(yíng) private boolean live = true; //判斷坦克是否存活 private static Random r = new Random();//設(shè)置一個(gè)隨機(jī)值變量 private static int step = r.nextInt(12)+3; //敵方坦克隨機(jī)移動(dòng)步驟3-14步 private int Life = 100; //血量 private BloodBar bb = new BloodBar(); //血塊類 // ImageIcon icon = new ImageIcon("res\\myTank.jpg"); // ImageIcon icon2 = new ImageIcon("res\\enemyTank.jpg"); // Image image = icon.getImage(); // Image image2 = icon2.getImage(); private TankClient tc; //主類權(quán)限 public Tank(int x, int y, boolean good, Color color) { super(); this.x = x; this.y = y; this.color = color; this.good = good; } public Tank(int x, int y, boolean good,Color color,Direction dir,TankClient tc){ this(x,y,good,color); this.dir = dir; this.tc = tc; } /*獲取坦克生命值*/ public int getLife() { return Life; } /*設(shè)置坦克生命值*/ public void setLife(int Life) { this.Life = Life; } /*獲取坦克陣營(yíng)*/ public boolean isGood() { return good; } /*設(shè)置坦克陣營(yíng)*/ public void setGood(boolean good) { this.good = good; } /*獲取坦克存活狀態(tài)*/ public boolean isLive() { return live; } /*設(shè)置坦克存活狀態(tài)*/ public void setLive(boolean live) { this.live = live; } /*畫坦克*/ public void draw(Graphics g){ if(!live){ if(!good){ tc.tanks.remove(this); //敵方坦克死亡時(shí)在集合中刪除 //tc.tanks.add(new Tank(r.nextInt(700),r.nextInt(500),false,Color.blue,Direction.D,this.tc)); } return; } /*先保存之前的畫筆顏色,畫完之后再還原畫筆顏色*/ Color c = g.getColor(); //獲取當(dāng)前畫筆顏色 g.setColor(color); //設(shè)置畫筆顏色為紅色 /*畫坦克*/ g.fillOval(x, y, Whith, Higth); /*兩種方法繪制敵我坦克,運(yùn)用之前加入的圖片或者顏色區(qū)分*/ // if(good) // g.drawImage(image, x, y,Whith,Higth,null); // else // g.drawImage(image2, x, y, Whith, Higth, null); if(good) bb.draw(g); //我方坦克畫血條 g.setColor(Color.black); /*通過炮筒方向畫出炮筒*/ switch(ptDir){ case L: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x, y+Tank.Higth/2); break; case LU: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x, y); break; case U: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x+Tank.Whith/2, y); break; case RU: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x+Tank.Whith, y); break; case R: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x+Tank.Whith, y+Tank.Higth/2); break; case RD: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x+Tank.Whith, y+Tank.Higth); break; case D: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x+Tank.Whith/2, y+Tank.Higth); break; case LD: g.drawLine(x+Tank.Whith/2, y+Tank.Higth/2, x, y+Tank.Higth); break; } g.setColor(c); //還原畫筆顏色 move();//移動(dòng) } /*鍵盤監(jiān)聽;摁鍵*/ public void KeyPressed(KeyEvent e){ int key = e.getKeyCode(); //將鍵盤監(jiān)聽到的摁鍵以整數(shù)保存 /*鍵盤移動(dòng)坦克*/ switch(key){ /*移動(dòng)摁鍵*/ case KeyEvent.VK_UP: bU=true; break; case KeyEvent.VK_DOWN: bD=true; break; case KeyEvent.VK_RIGHT: bR=true; break; case KeyEvent.VK_LEFT: bL=true; break; } locateDirection(); } /*鍵盤監(jiān)聽;抬起鍵*/ public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); //將鍵盤監(jiān)聽到的摁鍵以整數(shù)保存 /*鍵盤移動(dòng)坦克*/ switch(key){ case KeyEvent.VK_UP: bU=false; break; case KeyEvent.VK_DOWN: bD=false; break; case KeyEvent.VK_RIGHT: bR=false; break; case KeyEvent.VK_LEFT: bL=false; break; case KeyEvent.VK_Z: //單發(fā)子彈 if(live) fire(); break; case KeyEvent.VK_F2: //我方復(fù)活 if(!this.live){ this.live=true; this.setLife(100); } break; case KeyEvent.VK_F3: //敵方復(fù)活 fuhuo(); break; case KeyEvent.VK_A: //無敵導(dǎo)彈 superFire(); break; case KeyEvent.VK_Q: //回血 if(this.live) this.Life = 100; break; case KeyEvent.VK_E: //釋放血塊 tc.b.fh(); break; /*還原位置鍵*/ case KeyEvent.VK_R: x = 50; y = 50; break; } locateDirection(); //合成方向 } /*合成移動(dòng)方向*/ 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; } void move(){ //移動(dòng) /*記錄上一步的位置*/ oldX = x; 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; } /*判斷坦克移動(dòng)越界情況(游戲邊界)*/ if(x < 5) x = 5; if(y < 25) y = 25; if(x+Whith > tc.GAME_WIDTH-5) x = tc.GAME_WIDTH-Whith-5; if(y+Higth > tc.GAME_HEIGTH-5) y = tc.GAME_HEIGTH-Higth-5; if(dir != Direction.STOP) //如果坦克不靜止就改變炮筒方向 ptDir = dir; /*敵方坦克自動(dòng)移動(dòng)*/ if(!good){ Direction[] dirs = Direction.values(); //將方向變量設(shè)為數(shù)組 if(step == 0){ step = r.nextInt(12)+3; //隨機(jī)移動(dòng)步驟 int randomNumber = r.nextInt(dirs.length); //隨機(jī)移動(dòng)方向 dir = dirs[randomNumber]; } step--; if(r.nextInt(40)>30) this.fire(); //隨機(jī)是否發(fā)射炮彈 } } /*敵方坦克復(fù)活*/ public void fuhuo(){ if(tc.tanks.size() < 20) while(true){ int x = r.nextInt(700); int y = r.nextInt(500); Tank t = new Tank(x,y,false,Color.blue,Direction.D,tc); /*如果坦克與墻重合則重新隨機(jī)位置直到不重合為止才將新坦克加入集合*/ if(t.getRect().intersects(tc.wall1.getRect())||t.getRect().intersects(tc.wall2.getRect()) ||t.getRect().intersects(tc.wall3.getRect()) ||t.getRect().intersects(tc.wall4.getRect())){ continue; } else{ tc.tanks.add(t); break; } } } /*子彈發(fā)射*/ public void fire(){ int x = this.x + Whith/2 - Missile.Whith/2; //控制子彈方向?yàn)樘箍酥虚g int y = this.y + Higth/2 - Missile.Higth/2; tc.missiles.add(new Missile(ptDir,color,x,y,good,tc)); //創(chuàng)建新的子彈類加入到子彈集合中 } /*碰撞;獲取坦克的范圍*/ public Rectangle getRect(){ return new Rectangle(x,y,Whith,Higth); } /*回執(zhí)上一步位置*/ private void stay(){ x = oldX; y = oldY; } /*如果撞墻,調(diào)用stay方法,返回上一步位置*/ public boolean hitWall(Wall w){ if(this.live&&this.getRect().intersects(w.getRect())){ this.stay(); return true; } return false; } /*坦克互相撞擊事件*/ public boolean hitTanks(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; } /*帶開火方向的發(fā)射函數(shù)*/ public Missile fire(Direction dir){ if(!live) return null; int x=this.x+Whith/2-Missile.Whith/2; int y=this.y+Higth/2-Missile.Higth/2; Missile m=new Missile(dir,color,x, y,good, this.tc); tc.missiles.add(m); return m; } /*超級(jí)射擊導(dǎo)彈*/ private void superFire(){ Direction[] dirs=Direction.values(); for(int i=0;i<8;i++){ fire(dirs[i]);//循環(huán)調(diào)用八個(gè)方向 } } /*新增血塊類*/ private class BloodBar{ /*畫血條*/ public void draw(Graphics g){ Color c=g.getColor(); g.setColor(Color.red); g.drawRect(x, y-10, Whith, 10); int w=Whith*Life/100; g.fillRect(x, y-10, w, 10); g.setColor(c); } } /*吃血方法*/ public boolean eatBlood(Blood b){ if(this.live&&b.isLive()&&this.isGood()&&this.getRect().intersects(b.getRect())){ this.setLife(100); b.setLive(false); return true; } if(this.getRect().intersects(tc.wb.getRect())) this.Life = 100; return false; } }
Missile.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.util.List; public class Missile { /*子彈本身數(shù)據(jù)*/ Tank.Direction dir; //子彈方向 Color c; //子彈顏色 int x,y; //子彈位置 public static final int XSPEED = 15; //橫向移動(dòng)速度 public static final int YSPEED = 15; //縱向移動(dòng)速度 public static final int Whith = 10; //子彈寬 public static final int Higth = 10; //子彈高 private boolean live = true; //判斷子彈的存活 private boolean good; //判斷子彈和陣營(yíng) private TankClient tc;//主類權(quán)限 public Missile(Tank.Direction dir,Color c, int x, int y) { super(); this.dir = dir; this.x = x; this.y = y; this.c = c; } public Missile(Tank.Direction dir,Color c, int x, int y,boolean good,TankClient tc){ this(dir,c,x,y); this.good = good; this.tc = tc; } /*獲取子彈的存活*/ public boolean isLive() { return live; } /*設(shè)置子彈的存活*/ public void setLive(boolean live) { this.live = live; } public void draw(Graphics g){ /*如果子彈死亡狀態(tài)將這個(gè)子彈在子彈集合中刪除*/ if(!live){ tc.missiles.remove(this); //集合中刪除 return; } /*先保存之前的畫筆顏色,畫完之后再還原畫筆顏色*/ Color d = g.getColor(); //獲取當(dāng)前畫筆顏色 g.setColor(c); //設(shè)置畫筆顏色為紅色 /*畫子彈*/ g.fillOval(x, y, Whith, Higth); g.setColor(d); //還原畫筆顏色 move(); //移動(dòng) } public void move(){ /*判斷移動(dòng)方向移動(dòng)坦克位置*/ 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_HEIGTH) live = false; } /*碰撞;獲取子彈的范圍*/ public Rectangle getRect(){ return new Rectangle(x,y,Whith,Higth); } /*子彈與坦克碰撞過程*/ public boolean hitTank(Tank t){ /*如果子彈與坦克在同一范圍則子彈和坦克同時(shí)死亡;且子彈只能殺死對(duì)方坦克*/ if(this.live&&this.getRect().intersects(t.getRect())&&t.isLive()&&this.good!=t.isGood()){ if(t.isGood()){ //好坦克 /*我方坦克子彈射中會(huì)減少生命值,生命值0的時(shí)候會(huì)死亡*/ t.setLife(t.getLife()-20); if(t.getLife()<=0) t.setLive(false); }else{ //壞坦克 t.setLive(false);//死亡 } this.live=false;//子彈死亡 tc.explode.add(new Explode(x, y, tc));//新建爆炸加入集合 return true; } return false; } /*循環(huán)坦克集合分別進(jìn)行判斷子彈碰撞*/ 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; } }
Wall.java
import java.awt.Graphics; import java.awt.Rectangle; public class Wall { /*墻數(shù)據(jù)*/ int x,y,w,h; //位置和寬高 private TankClient tc; //主類權(quán)限 public Wall(int x, int y, int w, int h, TankClient tc) { super(); this.x = x; this.y = y; this.w = w; this.h = h; this.tc = tc; } /*獲取墻的范圍*/ public Rectangle getRect(){ return new Rectangle(x,y,w,h); } /*畫墻*/ public void draw(Graphics g){ g.fillRect(x, y, w, h); } }
Explode.java
import java.awt.Color; import java.awt.Graphics; public class Explode { /*坦克爆炸屬性*/ int x,y; //爆炸位置 private boolean live = true; //爆炸是否存在 int step = 0; //爆炸時(shí)間控制 int [] diameter = new int[] {4, 7, 12, 18, 26, 32, 49, 56, 65, 77, 80, 50, 40, 30, 14, 6};//爆炸范圍 private TankClient tc; //主類權(quán)限 public Explode(int x, int y, TankClient tc) { super(); this.x = x; this.y = y; this.tc = tc; } /*畫爆炸*/ public void draw(Graphics g){ if(!live) return; //如果爆炸死亡狀態(tài)不畫結(jié)束 /*如果爆炸時(shí)間結(jié)束爆炸不存在并在集合中刪除*/ if(step == diameter.length){ live = false; //爆炸死亡 step = 0; //步驟時(shí)間歸0 tc.explode.remove(this); //集合中刪除 return; } /*畫爆炸*/ Color c = g.getColor(); g.setColor(Color.orange); g.fillOval(x, y, diameter[step], diameter[step]); g.setColor(c); step++; } }
Blood.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.util.Random; public class Blood { /*血塊數(shù)據(jù)*/ int x, y, w, h;//血塊位置和大小 private TankClient tc; //主類權(quán)限 private boolean live=true;//血塊的存活 private static Random r = new Random();//設(shè)置一個(gè)隨機(jī)值變量 /*獲取血塊的存活狀態(tài)*/ public boolean isLive() { return live; } /*設(shè)置血塊的存活狀態(tài)*/ public void setLive(boolean live) { this.live = live; } /*血塊位置初值隨機(jī)一個(gè)數(shù)值*/ public Blood(){ x=r.nextInt(600)+100; y=r.nextInt(400)+100; w=h=15; } /*畫血塊*/ public void draw(Graphics g){ if(!live) return; Color c=g.getColor(); g.setColor(Color.magenta); g.fillRect(x, y, w, h); g.setColor(c); } /*釋放血塊*/ public void fh(){ if(!live){ x = r.nextInt(600)+100; y = r.nextInt(400)+100; live = true; } } /*獲取血塊范圍*/ public Rectangle getRect(){ return new Rectangle(x, y, w, h); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入學(xué)習(xí)Hibernate持久化對(duì)象的三個(gè)狀態(tài)
Hibernate中的對(duì)象有3中狀態(tài),瞬時(shí)對(duì)象(TransientObjects)、持久化對(duì)象(PersistentObjects)和離線對(duì)象(DetachedObjects也叫做脫管對(duì)象),下面通過本文給大家分享Hibernate持久化對(duì)象的三個(gè)狀態(tài),一起看看吧2017-09-09MYSQL批量插入數(shù)據(jù)的實(shí)現(xiàn)代碼
非常的實(shí)現(xiàn)原理,代碼較多,建議大家仔細(xì)看看。2008-10-10