この記事の例は、参照のためのJava実装Tetrisの特定のコードを共有しています。特定のコンテンツは次のとおりです
テトリスのデザインのアイデア
テトリスは子供の頃から遊んでいます。誰もがルールを知っています。過去には、私はそれがとても楽しいと思っていましたが、勝てなかっただけでした。今、私は一人で書いて、毎日それを練習することを学びました!
キーボード操作:
左キー:左に移動します。右キー:右に移動します。
up key:形状を変更して下位キー:ドロップを加速します(問題ありません、それ以上の整流なし)
列の正方形がいっぱいの場合、この行は排除されます。一連の正方形を排除すると、10ポイントが得られます。現在、私はレベルを設定していません。それが好きな赤ちゃんは自分でレベルを設定できます。
それで、それらの正方形の形はどこから来たのでしょうか?それは私たち自身によって設計されています。一般的な形状は次のとおりです。Iタイプ、Tタイプ、Lタイプ、Tianziグリッドタイプなど、自分で追加してください!
では、何が起こったのでしょうか?実際、それはたった4*4アレイです。もちろん、あなたはn*nを喜んでデザインすることができます、あなたは最終決定権を持っています!
そのため、以下の例を挙げて、表示できる形状が事前に設計され、0が空で、1がフィラーであるため、ゲームで形状を作成できるようにします。
それを忘れてください:写真を掲載して、最初に結果を実行しているコードを見るだけです:
それが好きですか?あなたがそれが好きなら、それを直接してください。たぶん、コードは十分に書かれていないでしょう。私を許してください。振り返ってみると、さらに要約し、コードを更新し続けます。
Gamepanelクラス:ゲームインターフェイスクラス、ブロック全体がドロップされて表示され、ゲームのロジックが遅くなり、このクラスには実装されています。
パッケージTetris; Import java.awt.graphics; Import Java.awt.event.actionEvent; Import Java.awt.Event.EventListener; Import java.awt.event.keyevent; Import java.awt.event.keyListener; import Java.util. javax.swing.jpanel; Import javax.swing.timer; public class gamepanel拡張jpanel実装keylistener {private int maprow = 21; private int mapcol = 12; private int mapgame [] [] = new int [maprow] [mapcol]; // 2次元配列スペースを開いて、マップ情報プライベートタイマータイマーを保存します。 private int score = 0; //スコアを記録したランダム= new Random(); private int curshapeType = -1; private int curshapestate = -1; // private int nextshapestate = -1; //次回プライベートint posx = 0に表示されるブロックグループのタイプとステータスを設定します。 private int posy = 0;プライベートファイナルイントシェイプ[] [] [] = new int [] [] {// t glyphは反時計回りに保存されます{{0,1,0,0、1,1,1,0、0,0,0,0,0,0,0,0,0}、{0,1,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、1,1,1,1,1,0,0,0,0、0,0,0,0,0、0}、{0,1,0,0、0,1,0,0、 0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0、0、0、0、0、0、 0,0,0,0,0,0,0,0,0,0,0,0,0、0,0,0,0,0,0,0,0,0,0,0,0,0,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、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 1,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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、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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,0,0,0,0,0,0,0,0,0,0,0,. 、0,0,0,0,0,0,0,0,0,0、0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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 rowrect = 4; private int colrect = 4; //ここでは、保存された画像を4*4 2次元配列と見なします。 1次元配列を使用して上に保存していますが、プライベートint rectwidth = 10を実装するには、2次元配列と見なす必要があります。 public gamepanel()// constructor ----良いマップを作成{createrect(); initmap(); //このマップSetwall(); // set wall // createrect(); Timer = new Timer(500、new TimerListener()); Timer.start(); }クラスTimerListenerはActionListener {public void ActionPerformed(ActionEvent e){moveown(); }} public void setwall()//列0と11は両方とも壁であり、行20は壁です{for(int i = 0; i <maprow; i ++)//列を描画{mapgame [i] [0] = 2; MapGame [i] [11] = 2; } for(int j = 1; j <mapcol-1; j ++)//最後の行を描画{mapgame [20] [j] = 2; }} public void initmap()//このマップを初期化する、ウォールIDは2、スペースIDは0、ブロックIDは1 {for(int i = 0; i <maprow; i ++){for(int j = 0; j <mapcol; j ++){mapgame [i] [j] = 0; }}} public void createrect()//ブロックの作成---現在のブロックタイプと状態が存在する場合、次の場合は次の状態とタイプを設定します。 curshapestate = random.nextint(shapes [0] .length); } else {curshapeType = nextshapeType; curshapestate = nextshapestate; } nextshapeType = random.nextint(shapes.length); nextshapestate = random.nextint(shapes [0] .length); POSX = 0; posy = 1; //壁の左上隅に正方形を作成するif(gameover(posx、posy、curshapetype、curshapestate)){joptionpane.showconfirmdialog( "game end!"、 "tip"、joptionpane.ok_option); System.Exit(0); }} public boolean GameOver(int x、int y、int shapeType、int shapestate)//ゲームが終了するかどうか{if(isornomove(x、y、shapeType、shapestate)){falseを返す; } trueを返します。 } public boolean isornomove(int x、int y、int shapeType、int shapestate)//現在の数値を移動できるかどうかを判断します。ここでは、xとyの座標は、4*4の2次元配列(図を説明する配列)の2次元配列の左上ターゲットを参照していることを強調します。 MapGame [x+i] [y+j] == 1 ||。 }}} return true; } public void turn()// rotate {int temp = curshapestate; curshapestate =(curshapestate+1)%shapes [0] .length; if(isornomove(posx、posy、curshapetype、curshapestate)){} else {curshapestate = temp; } Repaint(); } public void moveown()//移動{if(isornomove(posx+1、posy、curshapetype、curshapestate)){posx ++; } else {addtomap(); //この行をマップで修正しますCheckline(); Createrect(); //新しいブロックを再作成} Repaint(); } public void moveleft()//左に移動{if(isornomove(posx、posy-1、curshapeType、curshapestate)){posy--; } Repaint(); } public void moveright()//右{if(isornomove(posx、posy+1、curshapetype、curshapestate)){posy ++; } Repaint(); } public void addtomap()//倒れた画像をマップに修正{for(int i = 0; i <rowrect; i ++){for(int j = 0; j <colrect; j ++){if(shapes [curshapeTye] [curshapestate] [i*colrect+j] {mapgame+i] shapes [curshapetype] [curshapestate] [i*colrect+j]; }}}} public void checkline()//これらの行に完全な行があるかどうかを確認{int count = 0; for(int i = maprow-2; i> = 0; i-){count = 0; for(int j = 1; j <mapcol-1; j ++){if(mapgame [i] [j] == 1){count ++; } else break; } if(count> = mapcol-2){for(int k = i; k> 0; k-){for(int p = 1; p <mapcol-1; p ++){mapgame [k] [p] = mapgame [k-1] [p]; }} score += 10; i ++; }}} public void paint(graphics g)// repaintウィンドウ{super.paint(g); for(int i = 0; i <rowRect; i ++)//落下ブロックを描画{for(int j = 0; j <colrect; j ++){if(shapes [curshapeType] [curshapestate] [i*colrect+j] == 1){g.fillrect((g.fillrect((posy+j+1)*rectwidth+i+1) rectwidth、rectwidth); }}} for(int i = 0; i <mapRow; i ++)//マップで修正されたブロック情報を描画します{for(int j = 0; j <mapcol; j ++){if(mapgame [i] == 2)//壁を描く{g.drawrect(j+1) rectwidth); } if(mapgame [i] [j] == 1)//小さな正方形{g.fillrect((j+1)*rectwidth、(i+1)*rectwidth、rectwidth、rectwidth); }}} g.drawstring( "score ="+ score、225、15); G.DrawString( "Next Square:"、225、50); for(int i = 0; j <colrect; j ++){if(shapes [nextshapetype] [nextshapestate] [i*colrect+j] == 1){g.fillrect(225+(j*rectwidth)、100+(i*rectwidth)、rectwidth、rectwidth); }}}} public void newgame()//ゲームは再び開始{score = 0; initmap(); setwall(); Createrect(); Repaint(); } public void stopgame()//ゲームは{timer.stop(); } public void contiongame(){timer.start(); } @Override public void keytyped(keyevent e){} @override public void keypressed(keyevent e){switch(e.getkeycode()){case KeyEvent.VK_UP://up--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- keyevent.vk_right://右---- moveright();壊す; }} @Override public void keyReleased(keyevent e){// todo auto-enerated method stub}}} GameFrameクラス:ゲーム全体のエントリポートは、それを率直に言うために、Main()関数を備えたクラスです。このクラスは、ゲームインターフェイスのいくつかのデザインを実装します。これは、小さなUIとして理解できます。
パッケージTetris; Import Java.awt.Event.ActionEvent; Import Java.awt.Event.Event.ActionListener; Import javax.swing.jframe; Import javax.swing.jmenu; Import javax.swing.jmenubar; Import javax.swing.jmenuitem; Import javax.swing.javax.swing.javax.swing exptionem; ActionListener {private int widthframe = 500; Private Int HeightFrame = 600;プライベートjmenuメニュー= new jmenu( "game"); //メニューの作成private jmenuitem newgame = menu.add( "restart"); //内蔵メニューオプションプライベートJmenuitem exitgame = menu.add( "game exit"); private jmenuitem stopgame = menu.add( "game pause"); private jmenuitem goongame = menu.add( "Game Continue"); private jmenu menutwo = new jmenu( "help"); // 2番目のメニューを作成しますprivate jmenuitem aboutgame = menutwo.add( "about the Game"); GamePanel GamePanel = new GamePanel(); public gameframe()// constructor {addkeylistener(gamepanel); newGame.AddActionListener(this); exitgame.addactionlistener(this); stopgame.addactionlistener(this); GOONGAME.ADDACTIONLISTENER(this); aboutgame.addactionlistener(this); this.add(gamepanel); jmenubarメニュー= new jmenubar(); menu.add(meneone); menu.add(menutwo); this.setjmenubar(メニュー); this.settitle( "tetris"); this.setbounds(50、10、widthframe、heightFrame); this.setVisible(true); this.setDefaultCloseoperation(jframe.exit_on_close); } public void ActionPerformed(actionEvent e){if(e.getSource()== newGame)//ゲームは再び開始{gamepanel.newgame(); } if(e.getSource()== exitgame)// game exit {system.exit(0); } if(e.getSource()== stopgame)// game pause {gamepanel.stopgame(); } if(e.getSource()== goongame)//ゲーム継続{gamepanel.continuegame(); } if(e.getSource()== aboutgame)//ゲーム情報{joptionpane.showmessageialog(null、 "左と右のキーを移動し、上向きに回転する"、 "prompt"、joptionpane.ok_option); }} public static void main(string [] args){new GameFrame(); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。