私は昨年このテクニックを学びましたが、まだ書いていません。今、私はテトリスゲームを書くのに時間をかけました。
単純な新しいゲームのみ、一時停止、続行、ポイント機能。それは単に古典的なロシアの機能を実現します。
もう紹介しません。興味がある場合は、自分で実行して後で写真を投稿してください。
コード:
パッケージcn.hncu; Import java.awt.color; import java.awt.font; Import java.awt.graphics; import java.awt.event.event.event.event.event.actionlistener; Import java.awt.event.keyadapter; import java.aw.aw.aw.awt.event.event.event.event.event.event.eevent; javax.swing.jframe; Import javax.swing.jmenu; Import javax.swing.jmenubar; import javax.swing.jmenuitem; Import javax.swing.joptionpane; Import javax.swing.jpanel; {tetris te = new Tetris(); te.setVisible(true); //フォーカスを押収するインターフェイスに編集ボックスを追加する場合、次のコードを使用する必要があります。 jmenuitem itempause; jmenuitem itemcontinue; public tetris(){this.setDefaultCloseoperation(exit_on_close); this.setlocation(700、200); this.setsize(220、275); this.SetSizable(false); tp = new tetrispanel(); this.getContentPane()。add(tp); //メニューの追加jmenubar menubar = new JMenubar(); this.setjmenubar(menubar); jmenu menugame = new jmenu( "game"); Menubar.Add(Menugame); jmenuitem itemnew = new jmenuitem( "new Game"); itemNew.SetActionCommand( "new"); itempause = new JMenuitem( "Pause"); itemPause.SetActionCommand( "Pause"); itemcontinue = new jmenuitem( "continue"); itemcontinue.setActionCommand( "Continue"); itemcontinue.setEnabled(false); menugame.add(itemnew); menugame.add(itempause); menugame.add(itemcontinue); menulistener menulistener = new Menulistener(); ItemNew.AddActionListener(Menulistener); itempause.addactionlistener(menulistener); itemcontinue.addactionlistener(menulistener); // jFrame全体にキーボードを追加させます。 } class menulistenerはactionlistener {@override public void actionperformed(actionevent e){//新しいゲームを再生するif(e.getActionCommand()。equals( "new")){tp.newgame(); } if(e.getActionCommand()。equals( "Pause")){Timer.Stop(); itemcontinue.setEnabled(true); itempause.setEnabled(false); } if(e.getActionCommand()。equals( "継続")){timer.restart(); itemcontinue.setEnabled(false); itempause.setEnabled(true); }}}プライベートタイマータイマー;クラスTetrispanelはjpanel {//正方形の形状://最初の寸法はブロックタイプを表します(S、Z、L、J、I、O、Tを含む//回転数// 2番目の寸法は// 3番目と4番目の寸法はブロックマトリックス//シェープを表します[タイプ] shapes [] [] [] = new int [] [] [] { / * *テンプレート{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0、0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.0,0,0,0,0,0,0. 、0,0,0,0,0,0,0,0,0,0、0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0. 、0,0,0,0,0,0,0,0,0,0、0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0. 、0,0,0,0,0,0,0,0,0,0,0、0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0. {0,0,0,0,0,0,0,0,0,0,0、 * 0,0,0,0,0,0,0,0,0,0,0}} *//////////////////////////////////////////////////////////////////////////// 0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、 0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、 0、0、0、0、0、0、0、0、1、0、0、0、1、0、0}}、// s {{0、0、0、1、1、0、1、1、0、0、0、0、0、0、0、0、0、0、0、0}、{0、1、0、0、0、0、0、1、1、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0 1、0、1、1、0、0、0、0、0、0、0、0、0、0}、{0、1、0、0、0、1、1、0、0、0、0、1、0、0、0、0、0}}、// Z {{1、1、0、0、0、0、1、1、1、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } }, // J { { 0, 1, 0, 0, 0, 1、0、0、1、1、0、0、0、0、0、0}、{1、0、0、0、1、1、1、0、0、0、0、0、0、0、0、0、0}、{1、1、0、0、0、1、0、0、0、0、1、0、0、0、0、0、0、0、0、0、0、0、1、1、1、1、1、1、1、1、1、1、1、1、1、1、 0、0、0、0、0、0、0}}、// o {{1、1、0、0、0、1、0、0、0、0、0、0、0、0、0、0}、0、0}、0、0、1、1、0、0、0、0、0、0、0、0、0、0、0、0} 0、0、0、0、0、0}、{1、1、0、0、1、0、0、0、0、0、0、0、0、0、0、0}}、// 0、0、0、0}、{1、1、0、0、0、1、0、0、0、0、0、0、0、0、0、0}、{0、0、0、0、1、1、1、0、0、0、0、0、0、0、0、0、0}}、 0、0、0}、{0、1、0、1、1、0、0、0、1、0、0、0、0、0、0}、{1、1、0、1、1、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、 Private int blocktype; //ブロックタイププライベートインターステート; //ローテーションステートプライベートイントX; //ブロック位置X - コラム位置列番号プライベートint y; //ブロック位置y - row位置y - row number private int map [] = new int [] = new int [13];国境を越えないようにするために、配列が開かれます。13列と23列のプライベートint delay = 1000。 public TimerKeyListerリスナー= new TimerKeyLister(); private int score = 0; //スコアpublic tetrispanel(){newGame(); nextblock(); //タイマー=新しいタイマー(遅延、リスナー); //timer.start(); } public void newgame(){blocktype =(int)(math.random()*1000)%7; turnState =(int)(math.random()*1000)%4; x = 4; y = 0; for(int i = 0; i <12; i ++){//(int j = 0; j <21; j ++){//ウォーキングif(i == 0 || i == 11){// 3はインターフェイスボーダーマップのグリッドです[i] [j] = 3; //本当に、0以外のインテガーを使用します。 }} map [i] [21] = 3; // 3はインターフェイス境界のグリッドです} if(timer!= null){timer.stop(); }遅延= 1000;タイマー=新しいタイマー(遅延、リスナー); Timer.start(); } private void nextblock(){blocktype =(int)(math.random()*1000)%7; turnState =(int)(math.random()*1000)%4; x = 4; y = 0; // fame over if(crash(x、y、blocktype、turnstate)== 0){timer.stop(); int option = joptionpane.showconfirmdialog(これ、「ゲームオーバー!!、まだあえて来ていますか...」); if(option == joptionpane.ok_option){newGame(); } else if(option == joptionpane.no_option){system.exit(0); }}} private void down(){if(crash(x、y+1、blocktype、turnstate)== 0){//ここでは、マップ内の対応する位置が1つの正方形で削除された後に積み重ねられたブロックまたはフレームが削除されるかどうかを判断するためにy+1が使用されることに注意してください; } else {y ++; } Repaint(); } private void left(){if(x> = 0){x - = crash(x -1、y、blocktype、turnstate); } Repaint(); } private void right(){if(x <8){x += crash(x +1、y、blocktype、turnstate); } Repaint(); } private void turn(){if(crash(x、y、blocktype、(turnstate+1)%4)== 1){turnState =(turnState+1)%4; } Repaint(); } private void add(int x、int y、int blocktype、int turnstate){for(int a = 0; a <4; a ++){for(int b = 0; b <4; b ++){if(shapes [blocktype] [turnstate] [a*4+b] == 1){map [x+b+1] [y+a] = 1; }}} trydelline(); } // private void trydelline(){for(int b = 0; b <21; b ++){int c = 1; for(int a = 0; a <12; a ++){c&= map [a] [b]; // allは1です。cの結果は1です。マップ[e] [d-1]; }} //ポイントスコアを追加 += 100; DELAY /=1.05; Timer.setDelay(遅延); }}} //パラメーターの例:4,3,2,3 //衝突プライベートINTクラッシュがあるかどうかを判断します(int x、int y、int blocktype、int turstate){for(int a = 0; a <4; a ++){for(int b = 0; b <4; b ++){// if((shapes] [blocktype] map [x+b+1] [y+a] == 1)|| //(shapes [blocktype] [ターンステート] [a*4+b] == 1 && map [x+b+1] [y+a] == 3)){//} 0; //衝突}}} return 1; // collisionなし} @Override public void paint(グラフィックスG){// blocktype = 6; // turnstate = 3; // x = 4; // y = 6; Super.Paint(g); // Afthimage G.SetColorを排除する(新しい色(153,51,205)); //(int j = 0; j <16; j ++){shapes [blocktype] [turnstate] [j] == 1){g.fillrect((j%4+x+1)*10、(j/4+y)*10、10、10); g.setcolor(color.cyan); g.drawrect((j%4+x+1)*10、(j/4+y)*10、10、10); G.SetColor(新しい色(153,51,205)); }} //インターフェイスフレームとスタックブロックを描画します---マップG.SetColor(color.red); for(int i = 0; i <12; i ++){//歩行(int j = 0; j <22; j ++){//ウォーキングif(map [i] [j] == 3){g.drawrect(i*10、j*10、10、10); } else if(map [i] [j] == 1){g.fillrect(i*10、j*10、10、10); g.setcolor(color.green); g.drawrect(i*10、j*10、10、10); g.setcolor(color.red); }}} //スコアを表示するとレイアウトが美しい、インターフェイスに何かを追加//正方形領域G.SetColor(color.red)の右側の部分を描画します。 g.setFont(new Font( "aa"、font.bold、11)); G.DrawString( "score =" + score、130、20); g.setFont(new Font( "aa"、font.plain、13)); g.setcolor(color.blue); G.DrawString( "Pirated Gamesを拒否する"、125、70); G.DrawString( "自己保護に注意してください。"、125、90); G.DrawString(「欺かれることに注意してください。」、125、110); G.DrawString(「中程度のゲームは脳に適しています」、125、130); G.DrawString(「遊びに追加することは体に有害です。」、125、150); G.DrawString( "スケジュール時間を合理的に"、125、170); G.ドローストリング(「健康的な生活をお楽しみください。」、125、190); } class TimerKeyLister extends keyAdapter explentice ActionListener {@Override public void ActionPerformed(ActionEvent e){down(); } @Override public void keypressed(keyevent e){switch(e.getkeycode()){case keyevent.vk_down:down();壊す; case keyevent.vk_left:left(); break; case keyevent.vk_right:right(); break; case keyevent.vk_up:turn(); break; case keyevent.vk_f1:plug(); case keyevent.vk_f2:time(); }} public void plug(){score+= 100; } public void time(){delay = 1000; Timer.setDelay(遅延); }}}インターフェイスの実行:
よりエキサイティングなゲームについては、特別なトピック「Java Classic Games」を参照してください
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。