詳細についてはメモを参照してください。ここではあまりナンセンスなことは説明しません。子供時代の古典についてだけ理解してください。
Blood.java
package com.hkm.TankWar;import java.awt.*;/** * 血栓クラス、私たちのタンクはそれを食べることで血液を回復できます。 * @author Hekangmin * */public class Blood { private int x,y,w, h; //血栓の位置、幅、高さ; private TankWarClient tc; //血栓が移動するステップ数を記録します public boolean isLive(); live; } public void setLive(boolean live) { this.live = live } /** * 血栓の位置を記録します */ private int[][] pos={{400,300},{400,320},{420,320},{440,300},{440,330},{480,400},{520,400},{540,400}}; public Blood() { x=pos[0][0] ; y=pos[0][1]; public voiddraw(Graphics g) { if(!live) return; g.setColor(Color.CYAN); g.fillOval(x, y, w, h); (c); move(); } /** * 血栓を移動します*/ public void move() { step++; if(step>=pos.length) step=0; ]; y=pos[ステップ][1]; } } public Rectangle getRect() { return new Rectangle(x,y,w,h);
Explode.java
package com.hkm.TankWar;import java.awt.*;/** * Explode class* @author Hekangmin * */public class Explode { private int x,y;//爆発が発生した場所 private boolean Live=true ; int dia[]={4,8,12,16,32,40,20,14,4}; // 円の直径を表すために円のシミュレーションを使用します。 step=0;//差分を直径に移動 private TankWarClient tc;//参照を保持 public Explode(int x,int y,TankWarClient tc) { this.x=x; =tc; } public voiddraw(Graphics g) { if(!Live) { tc.explodes.remove(this) } if(step==dia.length)//最後の直径に達すると爆発して消滅します。 .YELLOW); g .fillOval(x, y, dia[ステップ]); } }
ミサイル.java
package com.hkm.TankWar;import java.awt.*;import java.awt.Event.*;import java.awt.event.KeyEvent;import java.util.List;/** * Bullet クラス* @author Hekangmin * */ public class Missile { private int x,y;//弾丸の位置 private Tank.Direction dir;//戦車の方向 private static Final int XSPEED=10;//戦車のx方向の移動速度、private staticfinal int YSPEED=10;//戦車のy方向の移動速度、public staticfinal int WIDTH=10; HEIGHT=10; private boolean Live= true; // 弾が生きているかどうかを判断する private boolean Good // 敵の弾と自分の弾を区別する 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,TankWarClient tc) { this(x,y,dir) ); this.good=good;//タンクの良い属性と悪い属性を同じに設定します this.tc=tc; } /** * 弾を描画します* @param; g はブラシです*/ public voiddraw(Graphics g) { if(!Live) { tc.missiles.remove(this) } Color c=g.getColor() { g.setColor(カラー .BLUE); } else g.setColor(Color.ORANGE); g.setColor(c); } /** * 戦車の方向に従って弾を移動します */ private void move() { switch(dir) { case L: x-=XSPEED Break; ; ブレーク; ケース U: y-=XSPEED; ケース R: x+=XSPEED; y+=YSPEED; ケース D: y+=YSPEED; ケース LD: y+=YSPEED; } if(x<0||y<0||x> >TankWarClient.GAME_HEIGHT)//弾丸が境界を越えたら、それを消滅させます { Live=false } } public boolean isLive() { return; Live; } public Rectangle getRect()//弾丸の長方形の領域を取得します; { return new Rectangle(this.x, this.y, this.WIDTH, this.HEIGHT) } /**弾丸はタンクと衝突します。 * @param t はタンクです* @return は衝突を示すために true を返します、それ以外の場合は衝突はありません */ 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()-10); if(t.getLife()<=0) }else{ t.setLive(false); this.Live=false;///弾丸を死に至らしめる; Explode e=new Explode(x,y,tc);//爆発が発生する; } return true; } /** * 弾丸が敵の戦車と衝突したかどうかを判断します; * @param タンク 敵のタンク* @returntrue は衝突を意味し、false は衝突しません */ public boolean hitTanks(List<Tank> タンク) { for(int i=0;i<tanks.size();i++) { if(hitTank(tc.tanks.get(i))) { return true; } } return false } /** * ブレットかどうかを判断します。壁に当たる * @param w 壁 * @returntrue、ヒット、false、ヒットしない */ public boolean HistWall(Wall w) { if(this.Live&&this.getRect().intersects(w.getRect())) { Live=false } return false;
タンク.java
package com.hkm.TankWar;import java.awt.*;import java.awt.event.KeyEvent;import java.util.*;/** * Tank クラス* @author Hekangmin * */public class Tank { public static Final int XSPEED=5;//タンクの X 方向の速度 public static Final int YSPEED=5; public static Final int HEIGHT=30; BloodBar bb=new BloodBar();//ブラッドバー private int life=100; public int getLife() { return life; } public void setLife(int life) { this.life = life } private static Random r=new Random (); private static int step=r.nextInt(12)+3;//敵の戦車がランダムに移動するのにかかるステップ数を表す数値を定義します。 bL=false,bU=false,bR=false,bD=false; enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};//列挙型を使用してタンクの方向を定義します。 int x,y; private int oldX,oldY;//前のステップでタンクの位置を記録します; private boolean live=true;//それが生きているかどうかを判断します public boolean isLive() { return live } (ブール値live) { this.live = live; } private boolean good;//タンクは良いですか? public boolean isGood() { return Good; } private Direction ptDir=Direction.D;// バレルの方向を追加します。 TankWarClient tc ;//メンバ変数へのアクセスを容易にするために相手の参照を保持するため; Direction dir=Direction.STOP;//先頭に停止するタンクの方向を設定します。 int y、ブール値、方向dir,TankWarClient tc) { this.x=x; this.oldX=x; this.dir=dir; //相手の参照を保持します; } public voiddraw(Graphics g) { if(!live)//死ぬと描画されなくなります { if(!good) { tc.tanks.remove(this); if(tc.tanks.size()<5)//タンクが 5 個未満の場合はタンクを追加します。 { for(int i=0;i<10;i++) { int posX=r.nextInt(800); posY =r.nextInt(600); tc.tanks.add(new Tank(posX,posY,false,Direction.D,tc));//タンクの位置をランダムに表示する} } } return; } カラー c=g.getColor(); if(good) { g.setColor(Color.BLACK); x, y, WIDTH, HEIGHT); g.setColor(c); // バレルの方向を描画します。 g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x-10, y+Tank.HEIGHT/2);// 砲身を描画し、代わりに直線を描画します。 : g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x-7, y-7); ケース U: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y-10); ケース RU: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2、x+Tank.WIDTH+7、y-7) ケース R: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH+10, y+Tank.HEIGHT/2); RD: g.drawLine(x+Tank.HEIGHT/2); WIDTH/2、y+Tank.HEIGHT/2、x+Tank.WIDTH+7、y+Tank.HEIGHT+7); ケース D: g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y+Tank.HEIGHT+10); LD の場合: g.drawLine(x+Tank. WIDTH/2, y+Tank.HEIGHT/2, x-7, y+HEIGHT+7); } move() } public void move(); oldX=x;// タンクの以前の位置を記録します oldY=y; switch(dir) { case L: x-=XSPEED; case U: y -=YSPEED; ケース RU: x+=XSPEED; ケース R: x+=XSPEED; y+=YSPEED; ケース D: y+=YSPEED; ケース STOP: this.ptDir= this.dir; /** * 戦車が境界を越えないようにする */ if(x<0) x=0; if(x+Tank.WIDTH>TankWarClient.GAME_WIDTH) x=TankWarClient.GAME_WIDTH-30; if(y+Tank.HEIGHT>TankWarClient.GAME_HEIGHT) y=TankWarClient.GAME_HEIGHT-30; if(!good) { 方向[] =Direction.values();//列挙型を配列に変換します。 if(step==0) { step=r.nextInt(12)+3; int rn=r.nextInt(dirs.length);// length 内のランダムな整数を生成します。 } step- -; if(r.nextInt(40)>20) this.fire(); // 敵戦車に弾を発射させます * @param e キーボード イベント; public void KeyPressed(KeyEvent e) { int key=e.getKeyCode(); switch(key) { case KeyEvent.VK_LEFT: bL=true; case KeyEvent.VK_UP: bU =true; ケースKeyEvent.VK_DOWN: bD=true; } keyEvent (KeyEvent e) { int key=e.getKeyCode(); switch(key) { case KeyEvent.VK_CONTROL: fire(); case KeyEvent.VK_LEFT: bR=false; ; ブレーク; ケース KeyEvent.VK_UP: bU=false ブレーク; bD=false; 弾丸の種類を返します。 public Missile fire() { if(!live) return null; mx=this.x+Tank.WIDTH/2-Missile.WIDTH/2;//弾丸の発射位置を計算する; int my=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2; new Missile (mx,my,good,ptDir,this.tc);////銃身の方向に従って弾を発射する tc.missiles.add(m) } public Missile fire(Direction dir); { if(!live) return null; int mx=this.x+Tank.WIDTH/2-Missile.WIDTH/2; int my=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2; new Missile(mx,my,good,dir,this.tc);//戦車の方向に従って弾を発射します; } public void superFire(); Direction[] dirs=Direction.values(); for(int i=0;i<8;i++) { fire(dirs[i]) } } public void locationDir() { if(bL&&!bU&&!bR&&!bD) ) dir=方向.L; else if(bL&&bU&&!bR&&!bD) dir=方向.LU; if(!bL&&bU&&!bR&&!bD) dir=方向.U; else if(!bL&&bU&&bR&&!bD) dir=方向.RU; else if(!bL&&!bU&&bR&&!bD) dir=方向.R; !bU&&bR&&bD) dir=方向.RD; if(!bL&&!bU&&!bR&&bD) dir=方向.D; else if(bL&&!bU&&!bR&&bD) dir=方向.LD; else if(!bL&&!bU&&!bR&&!bD) dir=方向.STOP; Rectangle getRect()//タンクの長方形の領域を取得{ return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT); } /** * 戦車が壁に衝突しました* @param w Wall* @returntrue は衝突しましたが、false は衝突しませんでした */ public boolean colliedsWithWall(壁 w ) { if(this.live&&this.getRect().intersects(w.getRect())) { this.stay() return true; } /** * 戦車と戦車の衝突を処理して、互いに交差しないようにします。 * @param 戦車と敵戦車が衝突する場合は true、衝突しない場合は false を返します。タンク>タンク) { for(int i=0;i<tanks.size();i++) { タンク t=tanks.get(i); if(this!=t) { if(this.live&&this.isLive()&&this.getRect().intersects(t.getRect())) { this.stay();//前のステップの位置に戻ります;// //前のステップの位置 return true; } } return false;これは Tank の内部クラスで、血液バーはタンクの頭の上部に表示されます。 .setColor(Color.RED); g.drawRect(x,y-10,WIDTH,10); int w=WIDTH*life/100; g.fillRect(x,y-10,w,10); } } /** * 血栓を食べて血液を追加します。 * @param b 血栓* 食べた場合は true、食べなかった場合は false */ public boolean Eat; (ブラッド b) { if(this.live&&b.isLive()&&this.getRect().intersects(b.getRect())) { this.life=100; b.setLive(false); return true; } return false; } /** * 戦車は死亡後に復活します。 */ public void reBorn() { if(this.isGood()&&!this.isLive()) { this.setLive(true); this.setLife(100) } }
TankWarClient.java
package com.hkm.TankWar;import java.awt.*;import java.awt.event.*;import java.util.List;import java.util.ArrayList; /** * これはゲームの実行ウィンドウです。 * @ author Hekangmin * */public class TankWarClient extends Frame{/** * ゲーム ウィンドウの幅; */ public static Final int GAME_WIDTH=800;ゲーム ウィンドウの高さ; */ public static Final int GAME_HEIGHT=600; Tank MyTank(700,400,true,Tank.Direction.STOP,this); List< Explode> 爆発 = new ArrayList<Explode>(); List<Missile> ミサイル = new ArrayList<Missile>(); Wall(300,200,20,200,this); Wall w2=new Wall(600,300,30,150,this); /** * 仮想画像を描画します。 */ public TankWarClient( String) name)//テキストを設定 { super(name) } /** * ウィンドウを実行 */ public void launchFrame() { for(int) i=0;i<10;i++)//敵戦車を 10 両追加 { Tanks.add(new Tank(50+40*(i+1),50,false,Tank.Direction.D,this) } this) .setBounds(200,100,GAME_WIDTH,GAME_HEIGHT); this.setBackground(Color.GREEN); this.addWindowListener(new) WindowAdapter()//Anonymous class { public void windowClosing(WindowEvent e) { System.exit(0) } }); this.addKeyListener(new KeyMonitor()); // キーボード モニターを追加します。 //ウィンドウのサイズは変更できません; this.setVisible(true); new Thread(new PaintThread()).start();//新しいスレッドを作成します。 Paint(Graphics g) { g.drawString("ミサイル数: "+missiles.size(), 10, 50);//表示文字列; g.drawString("爆発数: "+explodes.size(),10 , 70); g.drawString("タンク数: "+tanks.size(),10,90); "+MyTank.getLife(),10,110); /** * 壁を描画します; */ w1.draw(g); w2.draw(g); /** * 弾丸やさまざまなイベントを検出します; */ for (int i=0;i<missiles.size();i++) { ミサイル m=missiles.get(i); m.hitsWall(w2); m.hitTanks(tanks); m.hitTank(MyTank); //if(!m.isLive()) //else m.draw(g); ; } /** * 爆発を描画します; */ for(int i=0;i<explodes.size();i++) { Explode e=explodes.get(i); } for(int i=0;i<tanks.size();i++) { タンク t=tanks.get(i) t.colliedsWithWall(w2); .draw(g); } b.draw(g); * ダブルバッファリング技術を使用してタンクのちらつき現象を排除します。 */ public void update(Graphics g) //g は画面上に描画されるブラシです。 { if(OffScreenImage==null) OffScreenImage=this.createImage(GAME_WIDTH, GAME_HEIGHT) ); gOffScreen=OffScreenImage.getGraphics(); //gOffScreen は OffScreenImage のブラシです; Color c=gOffScreen.getColor(); gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); gOffScreen.setColor(c); pattern(gOffScreen);//仮想画像上に描画; g.drawImage(OffScreenImage,0,0,null);//g ブラシを使用して画面上の仮想画像上に描画します。 private class PaintThreadimplements Runnable{ public void run() { while(true) { repaint();//ここでの repaint メソッドは Frame クラスの try{ Thread.sleep(100); }catch(InterruptedException e){ e.printStackTrace() } } } private class KeyMonitor extends KeyAdapter { public void keyReleased(KeyEvent e) { MyTank.keyPressed(e); KeyEvent e) { MyTank.KeyPressed(e) } } public static void main(String[] args) { new TankWarClient("My Tank World").launchFrame() } }
Wall.java
package com.hkm.TankWar;import java.awt.*;/** * 障害物の壁クラスを生成します; * @author Hekangmin * */ public class Wall { /** * x、y は壁の位置、w , h は幅と高さです; */ int x,y,w,h; /** * 参照を保持します*/ public Wall(int x, int y, int w, int h, TankWarClient; tc) { this.x = x; this.y = y; this.h = h; this.tc = tc; } ; g.setColor(Color.GRAY); g.fillRect(x,y,w,h); g.setColor(c); * @return */公共Rectangle getRect() { return new Rectangle(x,y,w,h) } }
以上がこの記事の全内容です。皆さんに気に入っていただければ幸いです。