Java言語のコレクション、スイング、スレッド、その他の知識ポイントを使用して、タンクウォーゲームを書きます。
(1)敵と米国の戦車の原則を描く:
タンククラス内には、ブールタイプの変数が良好です。タンクのキャンプを判断するために使用されます。タンクオブジェクトを作成するとき、グッドの値はタンククラス構築方法で渡されます。戦車を描くときは、善の価値を判断し、敵と私たちの色を区別してください。
(2)タンクの動きの原則:
モニターキーボードキープレスの応答イベントは、タンククラスで記述され、監視された、下、左、および右キーが記録され、タンクの動きの8方向の変数が合成されます。次に、各方向のタンク座標xとyの値への対応する変化を作成して、タンクの動きを達成できます。敵の戦車は自動的に移動し、乱数を介して敵のタンクの動きの方向をランダムに循環させ、各動きの回数をランダムに出力します。 2つのランダム値の組み合わせにより、敵のタンクの動きが可能になります。
(3)弾丸を発射するタンクの原則:
キーボード監視により、弾丸火災コマンドを検出した後、メインクラスの弾丸クラスコレクションに弾丸クラスを追加します。銃の樽の方向、タンクの位置、タンクのキャンプを弾丸に移します。メインペイント描画方法では、弾丸コレクションが循環します。セットに弾丸がある場合は、それらを引き出します。これにより、弾丸の発売が可能になります。
(4)戦車、弾丸、壁の衝突原理:
タンクタイプの弾丸型ウォールクラスでは、独自の範囲を取得するためのgetRectメソッドを取得し、タンクと弾丸が描かれるたびに対応する衝突検出を実行します(タンク内の壁とタンクとの衝突の方法があり、弾丸には壁とタンクとの衝突方法があります)。自分自身と衝突してはならないオブジェクトの範囲が衝突した場合、2つのオブジェクトが衝突することを意味します。
(5)タンクの血液循環の原理:
血栓が私たちのタンクと衝突すると、血栓が死に、血栓が完全に回収されます。
(6)タンクの復活の原則:
キーボード監視によりタンクの復活順序を検出した後、タンクが死んだ状態にある場合、タンク在庫状態が生き生きと変化し、タンクの血液量が完全な健康状態に復元されます。
プログラミングのアイデア:
Tank Warのプログラミングのアイデアは、メインクラスにスレッドを開き、50ミリ秒でサイクリングしてメソッドを描画しません(すべてをインターフェイス全体に描画します)。描かれたものには、敵の戦車(色の区別)、弾丸、壁、血栓、爆発が含まれます。そのため、合計でいくつかのカテゴリが書かれました。タンクタンク、ミサイルの弾丸、壁の壁、血栓、タンククライアントのメインクラス。各クラスには、このクラスの属性の描画機能を実現するための描画方法が書かれています。メインクラスには、このタンククラスのキーボードリスニングイベントと呼ばれるキーボードリスニングイベントがあります。キーボードの監視により、敵のタンクがランダムに動いている間、タンクの対応する動きが行われることが判断されます。更新がリフレッシュされるたびに、さまざまな衝突方法が呼び出され、衝突してはならないオブジェクトの状況を決定します。弾丸などの各オブジェクトの作成は、トリガーされた後に弾丸クラスのコレクションに新しい弾丸クラスを追加することです。描画するときは、描画するセット内の番号を決定します。タンクが範囲外またはタンクを殺す場合、セットで削除されます。他のカテゴリも似ているので、詳細は説明しません。
コードの各ステップには、対応する説明があります。
tankclient.java
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.aw.aw.wind.Event.Event.Event.Event.Event.Event.Event.Event.Event.Event.Event.Event.Windowadapter; java.util.arraylist; import java.util.list; import javax.swing.jframe; public class tankclient拡張jframe { / ** * @param args * / image offscrennimage = null; //ダブルバッファメモリ画像ストレージ/*ゲームサイズ*/public static final int game_width = 800; //幅インターフェイスpublic static final int game_heigth = 600; //ハイインターフェイスタンクmytank = new Tank(500,400、true、color.red、tank.direction.stop、this); //私たちのタンククラスリスト<missile>ミサイル= new arraylist <missile>(); //弾丸のコレクションリスト<exprode> exploit = new arraylist <eplode>(); // Explosion set list <ank> tanks = new arraylist <tank>(); //タンクセットウォールウォール1 =新しい壁(150,200,20,300、これ); //壁1壁の壁2 =新しい壁(250,500,300,20、これ); //壁2壁の壁3 =新しい壁(650,200,20,300、これ); //壁2壁の壁4 =新しい壁(250,300,300,20、これ); //壁2壁WB =新しい壁(750,550,40,40、これ); // wall2血液b = new Blood(); //血液クラスpublic static void main(string [] args){// todo auto-fenated method stub tankclient tc = new TankClient(); tc.lauchframe(); } private void lauchframe(){//(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/*ポイントがforkを終了した後に実行*/public void windowclosing(windowevent e){// todo auto generated method stib.exit(0); // exit}}); this.addkeylistener(new keymoniton()); //キーボードを設定して、これを聞くように設定します。SetVisible(true); //これを表示するようにウィンドウを設定します。 // window.getContentPane()。setBackground(color.green)を変更しないようにウィンドウを設定します。 //前景の色を緑色に設定します新しいスレッド(new PaintThread())。start(); // PaintThreadクラスを開始します} @Override public void paint(グラフィックスG){// todo auto-enerated method stub //グラフィックスはブラシクラスの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); /*cymer bullet collection*/ for(int i = 0; i <missiles.size(); i ++){missile m = missiles.get(i); //現在の弾丸m.hittanks(タンク)を取得します。 //あなた自身の弾丸が敵のタンクを殺しますM.Hitwall(Wall1); //弾丸と壁m.hitwall(wall2); M.Hitwall(Wall3); M.Hitwall(Wall4); M.Hittank(MyTank); //敵の弾丸が独自のタンクM.Draw(g)にヒットします。 // bulletを描画} for //爆発} for(int i = 0; i <tanks.size(); i ++){tank t = tanks.get(i); T.Draw(g); //敵のタンクT.Hittanks(タンク)を描く; T.hitwall(wall1); //タンクと壁T.hitwall(Wall2); T.hitwall(wall3); T.hitwall(wall4); } //g.setFont(NewFont( "宋体"、font.bold、20)); G.DrawString( "ミサイルcount:"+missiles.size()、10、50); // G.DrawStringを表示する( "Explode count:"+Explode.size()、10、80); // G.DrawStringを表示( "タンクカウント:"+tanks.size()、10、110); G.DrawString( "Mytank Life:"+mytank.getlife()、10、130); G.DrawString( "Blood Recovery:"、750、540); G.DrawString( "Directory Key Movement Direction; E:Fast Movement Bloodを解放する"、10、590); G.DrawString( "Z:launch dongfeng-31; a:launch dongfeng-41;"、10、570); G.DrawString( "F2:Resurrection; f3:敵の復活(最も多くの20)"、10、550); G.DrawString( "R:位置復元; Q:血でいっぱい"、10、530); } @override /*repaint-> update-> paint* / public void update(graphics g){// todo auto-feenated method stub super.update(g); if(offscrennimage == null)offscrennimage = this.createimage(game_width、game_heigth);グラフィックgoffscrenn = offscrennimage.getGraphics(); //メモリブラシの色を前景に設定します。 //最初に前景の色を保存、goffscrenn.setcolor(color.green); //メモリブラシの色をgreen goffscrenn.fillrect(0、0、game_width、game_heigth)に設定します。 //ゲームサイズのサイズとして画像を描画しますgoffscrenn.setcolor(c); //色G.drawimage(offscrennimage、0、0、null)を復元します。 //インターフェイスペイント(Goffscrenn)に保存された画像を描画します。 //メモリブラシを呼び出してペイントします}プライベートクラスのペイントスレッドはrunnable {@override public void run(){// todo auto-fienatedメソッドスタブwhile(true){repaint(); //注文repaint-> update-> paint try {thread.sleep(50); // 50ミリ秒ごとに画面を更新}キャッチ(例外e){e.printstacktrace(); }}}} /*キーボード応答* /プライベートクラスKeymoniton extends keyAdapter { /*キーボード応答を押します* / @Override public void keypressed(keyevent e){// dodo auto-fuedated method stub super.keypressed(e); mytank.keypressed(e); } /*キーボード応答を上げる* / @Override public void keyReleased(keyevent e){// dodo auto-fenated method stub super.keyreleased(e); mytank.KeyreLeadead(e); }}}tank.java
java.awt.color; Import java.awt.graphics;インポートjava.awt.image;インポートjava.awt.rectangle; Import java.awt.event.keyevent; Import java.util.list; Import java.util.random; Implated.imagecon.imagecon;/public class avuling Private int Oldx、Oldyを調整します。 //タンク前の座標public static final int whith = 30; //タンク幅public static final int higth = 30; //タンクハイパブリックstatic final int xspeed = 5; //長さ方向移動速度public static final int yspeed = 5; // onlongarogueの移動速度プライベートカラー。 //タンクカラープライベートブールbl = false、bu = false、br = false、bd = false; // 4つの方向制御値列挙指向{l、lu、u、ru、r、rd、d、ld、stop}; // 8つの方向の動きは、4つの方向値で構成されていますプライベート方向dir = direction.stop; //出力方向private direction ptdir = direction.d; // Cannon Private Boolean Goodの元の方向。 //タンクのキャンププライベートブーリアンライブを判断= true; //タンクが生きているかどうかを判断します。 //敵のタンクランダムな移動ステップ3-14プライベートint life = 100; //ヘルスボリュームプライベートブラッドバーbb = new Bloodbar(); // ImageIcon Icon = new ImageIcon( "res // mytank.jpg"); // imageicon icon2 = new ImageIcon( "res // Enemytank.jpg"); // image image = icon.getimage(); // image2 = icon2.getimage();プライベートタンククライアントTC; //メインクラスの権限パブリックタンク(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; } /*tank Healthを取得* / 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; } /*draw tank* / 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)); } 戻る; } /*最初に前のブラシ色を保存し、描画後にブラシの色を復元します* / color c = g.getColor(); //現在のブラシカラーG.SetColor(色)を取得します。 //ブラシの色を赤に設定/*タンクを描画*/g.filloval(x、y、whith、high); /*敵と米国のタンクを描く2つの方法は、以前に追加された写真または色を使用して、if(good)// g.drawimage(画像、x、y、whith、high、null)を区別します。 // else // g.drawimage(image2、x、y、whith、high、null); if(good)bb.draw(g); //私たちのタンクは、血のバーG.SetColor(color.Black)を描きます。 /*銃の樽を銃の樽の方向に描きます壊す;ケースlu:G.drawline(x+tank.whith/2、y+tank.higth/2、x、y);壊す; case ru:g.drawline(x+tank.whith/2、y+tank.higth/2、x+tank.whith/2、y);壊す;ケースR:G.drawline(x+tank.whith/2、y+tank.higth/2、x+tank.whith、y+tank.higth/2);壊す;ケースrd:g.drawline(x+tank.whith/2、y+tank.higth/2、x+tank.whith、y+tank.higth);壊す;ケースD:g.drawline(x+tank.whith/2、y+tank.higth/2、x、y+tank.higth);壊す; } g.setcolor(c); //ブラシカラー移動(); //移動}/*キーボード監視を復元します。 key*/ public void keypressed(keyevent e){int key = e.getkeycode(); //キーボードで聞くキーを整数として保存/*キーボードモバイルタンク*/switch(key){/*key*/case keyevent.vk_up:bu = true;壊す; case keyevent.vk_down:bd = true;壊す; case keyevent.vk_right:br = true;壊す; case keyevent.vk_left:bl = true;壊す; } cositirection(); } /*キーボード監視; key*/ public void keyReleased(keyevent e){int key = e.getkeycode(); //キーボードが整数として聞くキーを保存/*キーボード移動タンク*/switch(key){case keyevent.vk_up:bu = false;壊す; case keyevent.vk_down:bd = false;壊す; case keyevent.vk_right:br = false;壊す; case keyevent.vk_left:bl = false;壊す; case keyevent.vk_z://単一弾丸if(live)fire();壊す; case keyevent.vk_f2://復活if(!this.live){this.live = true; this.setLife(100); } 壊す; case keyevent.vk_f3:// enemesis resurrection fuhuo();壊す; case keyevent.vk_a://無敵のミサイルsuperfire();壊す; case keyevent.vk_q:// if(this.live)this.life = 100;壊す; case keyevent.vk_e://血栓をリリースしますtc.b.fh();壊す; /*位置キーを復元*/ case keyevent.vk_r:x = 50; y = 50;壊す; } cositirection(); //構成方向}/*構図の動き方向*/void stapedirection(){if(bl &&!bu &&!br &&!bd)dir = direction.l; else if(bl && bu &&!br &&!bd)dir = direction.lu; else if(!bl && bu &&!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 &&!bd)dir = direction.stop; } void move(){// move/*前のステップの位置を記録*/oldx = x; oldy = y; switch(dir){case l:x- = xspeed;壊す; case lu:x- = xspeed; y- = yspeed;壊す;ケースu:y- = yspeed;壊す;ケースRU:x+= xspeed; y- = yspeed;壊す;ケースR:x+= xspeed;壊す; case rd:x+= xspeed; y+= yspeed;壊す;ケースD:y+= yspeed;壊す;ケースLD:X- = XSpeed; y+= yspeed;壊す;ケースLD:X- = XSpeed; y+= yspeed;壊す;ケースストップ:休憩; } /*境界のないタンクの動きを裁判官(ゲーム境界)* / 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(step == 0){step = r.nextint(12)+3; //ランダム移動ステップint randomnumber = r.nextint(dirs.length); //ランダムな動きの方向dir = dirs [randomnumber]; } ステップ - ; 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);タンクT =新しいタンク(x、y、false、color.blue、direction.d、tc); /*タンクが壁と一致する場合、新しいタンクがセットに追加されるまで再ランダムの位置*/ if(tc.wall1.getRect())|| t.getRect()。 || t.getRect()。交差(tc.wall4.getRect())){継続; } else {tc.tanks.add(t);壊す; }}}/*bullet lautle*/public void fire(){int x = this.x + whith/2 -misile.whith/2; //タンクの中央までの弾丸の方向を制御int y = this.y + higth/2 -misile.higth/2; TC.Missiles.Add(新しいミサイル(PTDIR、COLOR、X、Y、GOOD、TC)); //弾丸コレクションに参加するための新しい弾丸クラスを作成} /*衝突。タンクの範囲を取得*/ public rectangle getRect(){new rectangle(x、y、whith、higth); } /*領収書の前のステップ位置* / private void stay(){x = oldx; y = oldy; } /*壁に当たった場合、滞在方法を呼び出して前のステップ位置に戻ります* / public boolean hitwall(wall w){if(this.live && this.getRect()。 trueを返します。 } falseを返します。 } /*タンク衝突イベント* / public boolean hittanks(list <kank> tanks){for(int i = 0; i <tanks.size(); i ++){tank t = tanks.get(i); if(this!= t){//衝突することはできません/*衝突した場合、前の位置に戻ります*/if(this.live && t.islive()&& this.getRect()。 t.Stay(); trueを返します。 }} falseを返します。 } /*火の方向を備えた排出機能int x = this.x+whith/2-missile.whith/2; int y = this.y+higth/2-missile.higth/2;ミサイルM =新しいミサイル(dir、color、x、y、good、this.tc); tc.missiles.add(m); mを返します。 } /*スーパーファイアミサイル* / private void superfire(){direction [] dirs = direction.values(); for(int i = 0; i <8; i ++){fire(dirs [i]); // cycling 8 directions}}/*flood clot class*/private class bloodbar {/*draw blood bar*/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); }} /*Blood-eating Method* / public boolean eatblood(血液b){if(this.live && b.islive()&& this.isgood()&& this.getRect()。交差(b.getRect())){this.setLife(100); B.SetLive(false); trueを返します。 } if(this.getRect()。交差(tc.wb.getRect()))this.life = 100; falseを返します。 }}Missile.java
Import java.awt.color; Import java.awt.graphics; import java.awt.rectangle; Import java.util.list; public class missile { /*bullet data* / tank.direction dir; //弾丸の方向Color C; // Bullet Color 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; // Bullet High Private Boolean Live = True; //弾丸のプライベートブールの生存を判断します。 //弾丸とキャンプのプライベートタンククライアントTCを判断します; //主要なクラス許可公開ミサイル(tank.方向dir、color C、int x、int y){super(); this.dir = dir; this.x = x; this.y = y; this.c = c; } publicミサイル(tank.方向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(グラフィックスG){ /*弾丸が死んだ場合、弾丸コレクションの弾丸を削除* / if(!live){tc.missiles.remove(this); //コレクションの返品を削除します。 } /*最初に前のブラシ色を保存し、描画後にブラシの色を復元します* / color d = g.getColor(); //現在のブラシカラーg.setColor(c)を取得します。 //ブラシの色を赤に設定します/*弾丸を印刷*/g.filloval(x、y、whith、high); G.SetColor(D); //ブラシの色の動きを復元します(); // move} public void move(){/*動きの方向とタンクの位置を判断*/switch(dir){case l:x- = xspeed;壊す; case lu:x- = xspeed; y- = yspeed;壊す;ケースu:y- = yspeed;壊す;ケースRU:x+= xspeed; y- = yspeed;壊す;ケースR:x+= xspeed;壊す; case rd:x+= xspeed; y+= yspeed;壊す;ケースD:y+= yspeed;壊す;ケースLD:X- = XSpeed; y+= yspeed;壊す;ケースストップ:休憩; } /*弾丸のアウトバウンド状況を判断します。アウトバウンドの場合、弾丸が死亡すると、弾丸のコレクション*/ 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); } /*弾丸とタンク間の衝突プロセス弾丸は対戦相手のタンクのみを殺すことができます*/if(this.live && this.getRect()。 T.SetLife(T.GetLife()-20); if(t.getlife()<= 0)t.setlive(false); } else {// bad tank t.setlive(false); // death} this.live = false; // bullet death tc.explode.add(new Explode(x、y、tc)); // new Explosion add set return true; } falseを返します。 } /*弾丸の衝突衝突を判断するための崩壊タンクセット} falseを返します。 } /*弾丸と壁の間の衝突プロセス//弾丸の死はtrueを返します。 } falseを返します。 }} wall.java
Import java.awt.graphics; Import Java.awt.Rectangle; public class Wall { /*wall data* / int x、y、w、h; //位置と幅と高さのプライベートタンククライアントTC; //メインクラスの権限パブリックウォール(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(){new rectangle(x、y、w、h)を返します。 } /*wall* / public void draw(グラフィックg){g.fillrect(x、y、w、h); }}Explode.java
Import Java.awt.Color; Import Java.awt.Graphics;パブリッククラス爆発{ /*タンク爆発属性* / int x、y; //爆発位置private boolean live = true; //爆発int step = 0がありますか。 //爆発時間制御int []直径= 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(グラフィックg){if(!live)return; //爆発の死の状態が終了しない場合/*爆発時間が終了した場合、爆発は存在せず、set(step == diameter.length){live = false; //爆発死ステップ= 0; //ステップ時間は0 tc.explode.remove(this); //セットで返品を削除します。 } /*爆発を描く* / color c = g.getColor(); g.setcolor(color.orange); g.filloval(x、y、直径[ステップ]、直径[ステップ]); G.SetColor(C);ステップ++; }}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 boolean live = true; //血栓生存private 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; } /*blook blood clot* / public void draw(グラフィック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); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。