java簡略坦克年夜戰制造代碼。本站提示廣大學習愛好者:(java簡略坦克年夜戰制造代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java簡略坦克年夜戰制造代碼正文
應用Java說話中的聚集、Swing、線程等常識點編寫一個坦克年夜戰游戲。
(1) 畫出敵我坦克的道理:
在坦克類外面有一個布爾類型變量good。用於斷定坦克的陣營,在創立坦克對象時在Tank類的結構辦法中傳入good的值。在畫坦克的時刻斷定good的值,辨別敵我坦克的色彩;
(2) 坦克活動的道理:
在坦克類裡寫入了監聽鍵盤摁鍵的呼應事宜,對監聽到的高低閣下鍵停止記載,並分解坦克挪動的八個偏向的變量。以後對應每一個偏向的分歧對坦克坐標x,y的值做呼應的更改完成我方坦克的挪動。而敵方坦克則主動挪動,經由過程隨機數對敵方坦克挪動偏向的隨機,而且隨機出每次挪動的次數。兩個隨機值相聯合即完成了敵方坦克的挪動。
(3) 坦克發射槍彈的道理:
經由過程鍵盤監聽,檢測到發射槍彈敕令後將主類的槍彈類聚集中添加一個槍彈類。將炮筒的偏向和坦克的地位和坦克的陣營傳入給槍彈類,在主類paint畫辦法中一向輪回槍彈類聚集,假如聚集內有槍彈,就畫出來。如許就完成了發射槍彈。
(4) 坦克、槍彈、牆的碰撞道理:
在坦克類槍彈類牆類平分別getRect辦法獲得本身的規模,然後在每次畫坦克、槍彈時都邑停止響應的碰撞檢測(在坦克類裡有與牆和出本身外的坦克相撞的處置辦法、在槍彈類裡有與牆和坦克相碰撞的處置辦法。),假如本身與不應碰撞的物體的規模相重合,則代表兩物體相撞。
(5)坦克加血的道理:
在血塊類中有血塊與我方坦克相碰撞的處置辦法,假如血塊規模與坦克規模重合則血塊類逝世亡,而且坦克類的血量答復置滿。
(6)坦收復活的道理:
經由過程鍵盤監聽,檢測到我方坦收復活敕令後,假如我方坦克處於逝世亡狀況,則將我方坦克存貨狀況改成在世而且將我方坦克血量回置滿血。
編程思惟:
坦克年夜戰的編程思惟在主類開啟一個線程,沒50毫秒輪回一次畫辦法(繪制全部界面內的一切器械)。畫的器械有敵我坦克(色彩辨別)、槍彈、牆、血塊、爆炸。所以總共寫出了幾個類:Tank坦克類、Missile槍彈類、Wall牆類、Blood血塊類、TankClient主類。在每個類中均寫有畫辦法完成本類屬性的繪制功效。在主類中有鍵盤監聽事宜挪用這Tank類的鍵盤監聽事宜。經由過程鍵盤監聽斷定出對Tank做出響應的挪動,而敵方Tank則是隨機活動。而且每次刷新都有挪用各類的碰撞辦法,斷定一些不應碰撞的對象的情形時做出處置。而每一個對象的創立例如槍彈這些是在觸發發生以後將新建槍彈類參加一個槍彈類聚集當中,在繪制的時刻斷定聚集中的數目停止繪制,出界或許打逝世坦克則在聚集中刪除。其他類也均類似,不在細說。
代碼中每步都正文有響應的說明。
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; //雙緩沖內存圖片存儲 /*游戲年夜小*/ 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); //窗口初始坐標點 this.setSize(GAME_WIDTH, GAME_HEIGTH); //窗口初始年夜小 this.setTitle("TankWar"); //窗口稱號 /*窗口監聽*/ this.addWindowListener(new WindowAdapter() { @Override /*點加入叉以後運轉*/ public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); //加入 } }); this.addKeyListener(new KeyMoniton()); //設置鍵盤監聽 this.setVisible(true); //設置窗口浮現 this.setResizable(false); //設置窗口弗成轉變年夜小 this.getContentPane().setBackground(Color.green); //設置窗口遠景色為綠色 new Thread(new PaintThread()).start(); //開端運轉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); /*輪回槍彈聚集*/ for (int i = 0; i < missiles.size(); i++){ Missile m = missiles.get(i); //獲得以後槍彈 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("偏向鍵挪動偏向;E:釋放挪動血快", 10, 590); g.drawString("z:發射春風-31;a:發射春風-41;", 10, 570); g.drawString("F2:回生;F3:敵方回生(對多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(); //設置一個內存畫筆色彩為遠景圖片色彩 Color c = goffscrenn.getColor(); //照樣先保留遠景色彩 goffscrenn.setColor(Color.green); //設置內存畫筆色彩為綠色 goffscrenn.fillRect(0, 0, GAME_WIDTH, GAME_HEIGTH); //畫成圖片,年夜小為游戲年夜小 goffscrenn.setColor(c); //復原色彩 g.drawImage(OffScrennImage, 0, 0, null); //在界面畫出保留的圖片 paint(goffscrenn); //把內存畫筆挪用給paint } private class PaintThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub while(true){ repaint(); //運轉次序repaint->update->paint try{ Thread.sleep(50); //每隔50毫秒刷新畫面一次 }catch(Exception e){ e.printStackTrace(); } } } } /*鍵盤呼應*/ private class KeyMoniton extends KeyAdapter{ /*摁下鍵盤呼應*/ @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub super.keyPressed(e); myTank.KeyPressed(e); } /*抬起鍵盤呼應*/ @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 { /*坦克自己數據*/ int x, y;//坦克坐標 private int oldX, oldY; //坦克上一步坐標 public static final int Whith = 30; //坦克寬 public static final int Higth = 30; //坦克高 public static final int XSPEED = 5; //橫向挪動速度 public static final int YSPEED = 5; //縱向挪動速度 private Color color; //坦克色彩 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 boolean good; //斷定坦克的陣營 private boolean live = true; //斷定坦克能否存活 private static Random r = new Random();//設置一個隨機值變量 private static int step = r.nextInt(12)+3; //敵方坦克隨機挪動步調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; //主類權限 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; } /*設置坦克性命值*/ public void setLife(int Life) { this.Life = Life; } /*獲得坦克陣營*/ public boolean isGood() { return good; } /*設置坦克陣營*/ public void setGood(boolean good) { this.good = good; } /*獲得坦克存活狀況*/ public boolean isLive() { return live; } /*設置坦克存活狀況*/ public void setLive(boolean live) { this.live = live; } /*畫坦克*/ public void draw(Graphics g){ if(!live){ if(!good){ tc.tanks.remove(this); //敵方坦克逝世亡時在聚集中刪除 //tc.tanks.add(new Tank(r.nextInt(700),r.nextInt(500),false,Color.blue,Direction.D,this.tc)); } return; } /*先保留之前的畫筆色彩,畫完以後再復原畫筆色彩*/ Color c = g.getColor(); //獲得以後畫筆色彩 g.setColor(color); //設置畫筆色彩為白色 /*畫坦克*/ g.fillOval(x, y, Whith, Higth); /*兩種辦法繪制敵我坦克,應用之前參加的圖片或許色彩辨別*/ // 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();//挪動 } /*鍵盤監聽;摁鍵*/ public void KeyPressed(KeyEvent e){ int key = e.getKeyCode(); //將鍵盤監聽到的摁鍵以整數保留 /*鍵盤挪動坦克*/ switch(key){ /*挪動摁鍵*/ 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(); } /*鍵盤監聽;抬起鍵*/ public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); //將鍵盤監聽到的摁鍵以整數保留 /*鍵盤挪動坦克*/ 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: //單發槍彈 if(live) fire(); break; case KeyEvent.VK_F2: //我方回生 if(!this.live){ this.live=true; this.setLife(100); } break; case KeyEvent.VK_F3: //敵方回生 fuhuo(); break; case KeyEvent.VK_A: //無敵導彈 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(); //分解偏向 } /*分解挪動偏向*/ 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(){ //挪動 /*記載上一步的地位*/ 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; } /*斷定坦克挪動越界情形(游戲界限)*/ 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; /*敵方坦克主動挪動*/ if(!good){ Direction[] dirs = Direction.values(); //將偏向變量設為數組 if(step == 0){ step = r.nextInt(12)+3; //隨機挪動步調 int randomNumber = r.nextInt(dirs.length); //隨機挪動偏向 dir = dirs[randomNumber]; } step--; if(r.nextInt(40)>30) this.fire(); //隨機能否發射炮彈 } } /*敵方坦收復活*/ 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); /*假如坦克與牆重合則從新隨機地位直到不重合為止才將新坦克參加聚集*/ 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; } } } /*槍彈發射*/ public void fire(){ int x = this.x + Whith/2 - Missile.Whith/2; //掌握槍彈偏向為坦克中央 int y = this.y + Higth/2 - Missile.Higth/2; tc.missiles.add(new Missile(ptDir,color,x,y,good,tc)); //創立新的槍彈類參加到槍彈聚集中 } /*碰撞;獲得坦克的規模*/ public Rectangle getRect(){ return new Rectangle(x,y,Whith,Higth); } /*回執上一步地位*/ private void stay(){ x = oldX; y = oldY; } /*假如撞牆,挪用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; } /*帶開仗偏向的發射函數*/ 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; } /*超等射擊導彈*/ private void superFire(){ Direction[] dirs=Direction.values(); for(int i=0;i<8;i++){ fire(dirs[i]);//輪回挪用八個偏向 } } /*新增血塊類*/ 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 { /*槍彈自己數據*/ Tank.Direction dir; //槍彈偏向 Color c; //槍彈色彩 int x,y; //槍彈地位 public static final int XSPEED = 15; //橫向挪動速度 public static final int YSPEED = 15; //縱向挪動速度 public static final int Whith = 10; //槍彈寬 public static final int Higth = 10; //槍彈高 private boolean live = true; //斷定槍彈的存活 private boolean good; //斷定槍彈和陣營 private TankClient tc;//主類權限 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; } /*設置槍彈的存活*/ public void setLive(boolean live) { this.live = live; } public void draw(Graphics g){ /*假如槍彈逝世亡狀況將這個槍彈在槍彈聚集中刪除*/ if(!live){ tc.missiles.remove(this); //聚集中刪除 return; } /*先保留之前的畫筆色彩,畫完以後再復原畫筆色彩*/ Color d = g.getColor(); //獲得以後畫筆色彩 g.setColor(c); //設置畫筆色彩為白色 /*畫槍彈*/ g.fillOval(x, y, Whith, Higth); g.setColor(d); //復原畫筆色彩 move(); //挪動 } public 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_HEIGTH) live = false; } /*碰撞;獲得槍彈的規模*/ public Rectangle getRect(){ return new Rectangle(x,y,Whith,Higth); } /*槍彈與坦克碰撞進程*/ public boolean hitTank(Tank t){ /*假如槍彈與坦克在統一規模則槍彈和坦克同時逝世亡;且槍彈只能殺逝世對方坦克*/ if(this.live&&this.getRect().intersects(t.getRect())&&t.isLive()&&this.good!=t.isGood()){ if(t.isGood()){ //好坦克 /*我方坦克槍彈射中會削減性命值,性命值0的時刻會逝世亡*/ 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; } /*輪回坦克聚集分離停止斷定槍彈碰撞*/ 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 { /*牆數據*/ int x,y,w,h; //地位和寬高 private TankClient tc; //主類權限 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; //爆炸時光掌握 int [] diameter = new int[] {4, 7, 12, 18, 26, 32, 49, 56, 65, 77, 80, 50, 40, 30, 14, 6};//爆炸規模 private TankClient tc; //主類權限 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; //假如爆炸逝世亡狀況不畫停止 /*假如爆炸時光停止爆炸不存在並在聚集中刪除*/ if(step == diameter.length){ live = false; //爆炸逝世亡 step = 0; //步調時光歸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 { /*血塊數據*/ int x, y, w, h;//血塊地位和年夜小 private TankClient tc; //主類權限 private boolean live=true;//血塊的存活 private static Random r = new Random();//設置一個隨機值變量 /*獲得血塊的存活狀況*/ public boolean isLive() { return live; } /*設置血塊的存活狀況*/ public void setLive(boolean live) { this.live = live; } /*血塊地位初值隨機一個數值*/ 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); } }
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。