これは、Javaコースの最後のコースデザインです。 Javaは初心者なので、私はそれをするときに技術専門家のいくつかのブログに紹介しました。ここで感謝の気持ちを表明したいと思います。
ここに投稿して、コミュニケーションと学習をしてください。
完全なプロジェクトファイル、ドキュメント、実行可能なJARファイルが必要な場合は、アドレスをダウンロードしてください:クリックしてリンクを開きます
RussianBlocksgame.java
RussiaBlocksGameをパッケージします。 java.awt。*; java.awt.event。*; javax.swing.border.borderをインポートします。 javax.swing.border.echedborderをインポートします。 /*** JFrameクラスから継承されたメインゲームクラスは、ゲームのグローバルな制御を担当しています。含まれる:1。Gamecanvas Canvasクラスのインスタンスオブジェクト *2。現在のアクティブブロック(RussiaBlock)インスタンスを保持するオブジェクト。 3.現在のコントロールパネル(ControlPanel)インスタンスを保持するオブジェクト。 */ public class russiablocksgameはjframeを拡張します{private static final long serialversionuid = -7332245439279674749l; / ***各行ごとにカウントされるポイント数*/ public final static int per_line_score = 100; / ***蓄積後にアップグレードできるポイントの数*/ public final static int per_level_score = per_line_score* 20; / ***最大シリーズはレベル10です*/ public final static int max_level = 10; / ** *デフォルトシリーズは2 */ public final static int default_level = 2です。プライベートGamecanvas Canvas;プライベートersblockブロック。プライベートブールプレイ= false;プライベートコントロールパネルCtrlpanel; //メニューバーの初期化プライベートjmenubar bar = new jmenubar(); Private Jmenu Mgame = new Jmenu( "Game")、McOntrol = new Jmenu( "Control")、minfo = new Jmenu( "help"); private jmenuitem minewgame = new jmenuitem( "new Game")、misetblockcolor = new Jmenuitem( "set block color ...")、misetbackcolor = new jmenuitem( "set color ...")、miturnharder = new jmenuitem( "elevate game deffory")、miturneaseier = new jmenuitem( "new jmenuitem(" new jmenuitem( ") jmenuitem( "exit")、miplay = new jmenuitem( "start")、mipause = new jmenuitem( "Pause")、miresume = new jmenuitem( "resume")、mistop = new jmenuitem(「ゲームを終了する ")、mirule = new jmenuitem("ゲーム ") / ***ウィンドウメニューを作成して設定します*/ private void creatmenu(){bar.add(mgame); bar.add(mcontrol); bar.add(minfo); mgame.add(minewgame); mgame.addseparator(); //水平方向の分割線mgame.add(misetblockcolor); mgame.add(misetblockcolor); mgame.addseparator(); //水平方向の分割線mgame.add(miturnharder); mgame.add(miturneasier); mgame.addseparator(); //水平方向の分割線mgame.add(miexit); McOntrol.Add(Miplay); miplay.setEnabled(true); mcontrol.add(mipause); mipause.setEnabled(false); mcontrol.add(miresume); miresume.setEnabled(false); McOntrol.Add(Mistop); mistop.setEnabled(false); minfo.add(mirule); minfo.add(miauthor); setjmenubar(bar); minewgame.addActionListener(new ActionListener(){@Override public void ActionPerformed(ActionEvent e){stopgame(); reset(); setLevel(default_level);}}); //ブロックColor Color MisetBlockColor.AddActionListener(new ActionListener(){@Override public void actionPerformed(actionEvent e){color newFrontColor = jcolorchooser.showdialog(russiablocksgame.this "set the block color! null){canvas.setBlockColor(NewFrontColor)}}}}}}}} //バックグラウンドを設定しますColor Color MisetBackColor.AddActionListener(new ActionListener(){@Override public void actionperformed(actionEvent e){color newbackcolor = jcolorchooser.showdialog(russiablocksgame.this、 "set the background color! canvas.setbackgroundcolor(newbackcolor)}}); //メニューバーの「AROUNT」の関数を定義すると、確認ボックスがポップアップします。 miauthor.addactionlistener(new ActionListener(){@Override public void actionperformed(actionEvent e){joptionpane.showmessageialog(null、 "class/n3115005372/nyang yujie/n©" 1); //Game rules description miRule.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Plates of different shapes composed of small squares fall from the top of the screen one after another. The /n player adjusts the position and direction of the plates to make them spell out a complete one or several pieces at the bottom ofこれらの完全な水平はすぐに消えます。 //難易度MiturnHarder.AddActionListener(new ActionListener(){@Override public void ActionPerformed(ActionEvent e){int curlevel = getLevel(); if(!playing && curlevel <max_level){setlevel(curlevel + 1);}}); //難易度を減らすmiturneasier.addactionlistener(new ActionListener(){@Override public void ActionPerformed(actionEvent e){int curlevel = getLevel(); if(!playing && curlevel> 1){setlevel(curlevel -1);}}}}}}}}}} //ボタンアクション応答miexit.addActionListener(new ActionListener(){@Override public void ActionPerformed(ActionEvent e){System.Exit(0);}}); } / ***メインゲームクラスのコンストラクターメソッド** @paramタイトル文字列、ウィンドウタイトル* / public rossiablocksgame(string title){super(title); //タイトルSetSize(500、600)を設定します。 //ウィンドウサイズを設定しますsetlocationRelativeto(null); //ウィンドウセンターcreatemenu()を設定します。コンテナコンテナ= getContentPane(); //メニューバーコンテナを作成します。SetLayout(new borderlayout(6、0)); //ウィンドウのレイアウトマネージャーを設定しますcanvas = new Gamecanvas(20、15); //新しいゲームCANVAS CTRLPANEL = new ControlPanel(This)を作成します。 //新しいコントロールパネルContainer.Add(canvas、borderlayout.center)を作成します。 // canvas container.add(ctrlpanel、borderlayout.east)を追加します。 //右にコントロールパネルを追加//ウィンドウイベントを登録します。閉じるボタンがクリックされると、ゲームが終了し、システムが終了します。 addwindowlistener(new WindowAdapter(){@Override public void windowclosing(windowevent we){stopgame(); system.exit(0);}}); //ウィンドウのサイズに従って正方形のサイズを自動的に調整するAddComponentListener(new componentAdapter(){@Override public void componentresized(componentevent ce){canvas.adjust();}}); setVisible(true); canvas.adjust(); } /***ゲームをリセット* /public void reset(){//キャンバスをリセットし、コントロールパネルctrlpanel.setPlayButtonEnable(true); ctrlpanel.setPauseButtonenable(false); ctrlpanel.setPauseButtonLabel(true); ctrlpanel.setstopbuttonenable(false); ctrlpanel.setturnleveldownbuttonenable(true); ctrlpanel.setturnlevelupbuttonenable(true); miplay.setEnabled(true); mipause.setEnabled(false); miresume.setEnabled(false); mistop.setEnabled(false); ctrlpanel.reset(); canvas.reset(); } / ***ゲームがまだ進行中であるかどうかを判断します** @return boolean、true-まだ実行されている、false has stopped* / public boolean isplaying(){playing; } / ***現在アクティブブロックを取得** @return ersblock、現在アクティブブロックへの参照* / public ersblock getCurblock(){return block; } / ***現在のキャンバスを取得** @return gamecanvas、現在のキャンバスへの参照* / public gamecanvas getcanvas(){return canvas; } / ***ゲームを開始* / public void playgame(){play(); ctrlpanel.setPlayButtonenable(false); ctrlpanel.setPauseButtonEnable(true); ctrlpanel.setPauseButtonLabel(true); ctrlpanel.setstopbuttonenable(true); ctrlpanel.setturnleveldownbuttonenable(false); ctrlpanel.setturnlevelupbuttonenable(false); mistop.setEnabled(true); miturnharder.setEnabled(false); miturneasier.setEnabled(false); ctrlpanel.requestfocus(); //コントロールパネルに焦点を合わせる}/***ゲームの一時停止*/public void pausegame(){if(block!= null){block.pausemove(); } ctrlpanel.setPlayButtonenable(false); ctrlpanel.setPauseButtonLabel(false); ctrlpanel.setstopbuttonenable(true); miplay.setEnabled(false); mipause.setEnabled(false); miresume.setEnabled(true); mistop.setEnabled(true); } / ***一時停止ゲームの続行を許可します* / public void resumegame(){if(block!= null){block.resumemove(); } ctrlpanel.setPlayButtonenable(false); ctrlpanel.setPauseButtonEnable(true); ctrlpanel.setPauseButtonLabel(true); mipause.setEnabled(true); miresume.setEnabled(false); ctrlpanel.requestfocus(); } / ***ユーザーはゲームを停止します* / public void stopgame(){playing = false; if(block!= null){block.stopmove(); } ctrlpanel.setPlayButtonenable(true); ctrlpanel.setPauseButtonenable(false); ctrlpanel.setPauseButtonLabel(true); ctrlpanel.setstopbuttonenable(false); ctrlpanel.setturnleveldownbuttonenable(true); ctrlpanel.setturnlevelupbuttonenable(true); miplay.setEnabled(true); mipause.setEnabled(false); miresume.setEnabled(false); mistop.setEnabled(false); miturnharder.setEnabled(true); miturneasier.setEnabled(true); reset(); //キャンバスとコントロールパネルのリセット}/** *プレーヤーが設定した難易度を取得 * } / ** *ユーザーはゲームの難易度を設定します * * @param level int、ゲーム難易度1-Max_Level * / public void setLevel(int level){if(level <11 && level> 0){ctrlpanel.setLevel(level); }} / ** GET GAME POINTS** @return int、Points* / public int getScore(){if(canvas!= null){return canvas.getScore(); } return 0; } /***最後のアップグレードからゲームポイントを取得します。アップグレード後、このポイントはクリアされます** @return int、ints*/ public int getScoreForLevelUpDate(){if(canvas!= null){return canvas.getScoreForLevelupDate(); } return 0; } / ** *積分が特定の値に蓄積する場合、1回アップグレード * * @return boolean、true-update success、false-update fail * / public boolean levelupdate(){int curlevel = getLevel(); if(curlevel <max_level){setlevel(curlevel + 1); canvas.resetscoreforlevelupdate(); trueを返します。 } falseを返します。 } / ***ゲーム開始* / private void play(){reset();再生= true;スレッドスレッド= newスレッド(new Game()); //ゲームスレッドを起動するthread.start(); } / ***レポートゲームはオーバー* / private void ReportGameOver(){new GameOverDialog(this、 "tetris"、 "ゲームの終了、スコアは" + canvas.getscore()); } /***実行可能なインターフェイスを実装するゲームプロセスの1つのラウンド。ゲームのラウンドは大きなループです。このループでは、100ミリ秒ごとに、ゲーム内の現在のブロックが終了に達したかどうかを確認します。そうでない場合、 *待ち続けます。最後になると、完全に満たされたラインがあるかどうかによって異なります。ある場合は、削除してプレーヤーにポイントを追加し、新しい電流ブロックをランダムに生成し、自動的に落下させます。 *新しいブロックが生成されたら、最初にキャンバスの上部のラインが占有されているかどうかを確認します。もしそうなら、あなたはゲームを判断することができます。 */プライベートクラスのゲーム実装Runnable {@Override public void run(){int col =(int)(math.random() *(canvas.getCols() - 3)); while(playing){if(block!= null){//ブロックは空ですif(block.isalive()){try {thread.currentthread(); thread.sleep(500); } catch(arturnedexception ie){ie.printstacktrace(); } 続く; }} checkfullinine(); //完全に満たされた行があるかどうかを確認します(isgameover()){reportgameover(); miplay.setEnabled(true); mipause.setEnabled(false); miresume.setEnabled(false); mistop.setEnabled(false); ctrlpanel.setPlayButtonenable(true); ctrlpanel.setPauseButtonLabel(false); ctrlpanel.setstopbuttonenable(false);戻る; } block = new ersblock(style、-1、col、getlevel()、canvas); block.start(); col =(int)(math.random() *(canvas.getCols() - 3)); style = ersblock.styles [(int)(math.random() * 7)] [(int)(math.random() * 4)]; ctrlpanel.settipstyle(style); }} //キャンバスに完全に満たされた線があるかどうかを確認します。ある場合、public it it public void checkfullinine(){for(int i = 0; i <canvas.getRows(); i ++){int row = -1; boolean fulllinecolorbox = true; for(int j = 0; j <canvas.getCols(); j ++){if(!canvas.getbox(i、j).iscolorbox()){fulllinecolorbox = false;壊す; }} if(fulllinecolorbox){row = i--; canvas.removeline(row); }}} //一番上の行が占有されているかどうかに基づいてゲームが終了したかどうかを判断しますif(box.iscolorbox()){return true; }} falseを返します。 }} /***ゲームオーバーダイアログボックスを定義します。 */ @suppresswarnings( "serial")private class gameoverdialogはjdialogを拡張します。プライベートボーダーボーダー= new etchedborder(etchedborder.raised、color.white、new Color(148、145、140)); public gameOverdialog(jframe parent、string title、string message){super(parent、title、true); if(parent!= null){setSize(240、120); this.setLocationRelativeto(親); jpanel messagepanel = new jpanel(); messagepanel.add(new Jlabel(メッセージ)); messagepanel.setborder(border);コンテナコンテナ= this.getContentPane(); container.setlayout(new gridlayout(2、0、0、10)); container.add(messagepanel); jpanel choosepanel = new jpanel(); Choosepanel.setlayout(new gridlayout(0、2、4、0)); container.add(choosepanel);繰り返しますが、Button = new JButton(「別のゲームを再生する」); ExitButton = new JButton( "Exit Game"); Choosepanel.add(new jpanel()。追加(繰り返しbutton)); Choosepanel.add(new JPanel()。追加(ExitButton)); Choosepanel.setborder(border); }再びButton.AddActionListener(this); exitbutton.addactionlistener(this); this.setVisible(true); } @Override public void ActionPerformed(actionEvent e){if(e.getSource()== gan button){this.setVisible(false); reset(); } else if(e.getSource()== exitbutton){stopgame(); System.Exit(0); }}}}Gamecanvas.java
RussiaBlocksGameをパッケージします。 java.awt.colorをインポートします。 java.awt.graphicsをインポートします。 javax.swing.jpanelをインポートします。 javax.swing.border.echedborderをインポートします。 /*** <行の数>* <列の数>正方形のクラスインスタンス。 JPanelクラスから継承されました。 ersblockスレッドクラスは、キャンバスクラスのグリッド色を動的に変更します。 Canvasクラスは *を使用してグリッドの色をチェックして、ersblockブロックの動きを反映しています。 */ public class gamecanvasはjpanelを拡張します{private static final long serialversionuid = 6732901391026089276l;プライベートカラーバックコラー= color.darkgray、frontcolor = color.white; private int rows、cols、score = 0、scoreforlevelupdate = 0;プライベートersbox [] []ボックス; Private int BoxWidth、BoxHeight; / ***キャンバスクラスのコンストラクター** @param rows int、キャンバスの行数* @param cols int、キャンバスの列と列の数は、キャンバスが持っている正方形の数を決定します*/ public gamecanvas(int rows、int cols){this.rows = rows; this.cols = cols; boxes = new ersbox [rows] [cols]; for(int i = 0; i <boxes.length; i ++){for(int j = 0; j <boxes [i] .length; j ++){boxes [i] [j] = new ersbox(false); }} setborder(new etchedborder(etchedborder.raised、color.white、new color(148、145、140));} / ** * @param rows * @param cols * @param backcolor * @param frontcolor * / color front、int rows、color、color、color、colors、color、color、これ(cols); GetBackGroundColor(){バックコラーを返す} /** @param frontcolor* /public void setblockcolor(this.frontcolor = frontcolor** @turn color* /public color) *キャンバスの正方形の数** @return*/ public getRows(){return列。 } / ***最後のアップグレードからポイントを取得** @return int、最後のアップグレードの後のポイント* / public getscoreforlevelupdate(){return scoreforlevelupdate} / ** Russiablocksgame.per_level_score; *** @return intの正方形の参照、正方形が参照される行*、正方形が参照される行* @return ersbox、列の列の参照* || col >|。 g.setcolor(frontcolor); boxwidth、true); ersboxesgame class */public synchronized void removeline(int row){for(int i = row; i> 0; i-){for(int j = 0; j <cols; j ++){boxes [i] =(ersbox)ボックス[i-1] [j]; Squares += Russiablocksgame.scoreforlocksgame.per_level_score() / ** {for(int j = 0; j <boxes .length; j ++){boxes [j] .setcolor(false)}}};controlpanel.java
RussiaBlocksGameをパッケージします。 java.awt.borderlayoutをインポートします。 java.awt.colorをインポートします。 java.awt.graphicsをインポートします。 java.awt.gridlayoutをインポートします。 java.awt.event.actioneventをインポートします。 java.awt.event.actionlistenerをインポートします。 java.awt.event.componentAdapterをインポートします。 java.awt.event.componenteventをインポートします。 java.awt.event.keyadapterをインポートします。 java.awt.event.keyeventをインポートします。 java.text.dateformatをインポートします。 java.text.simpledateformatをインポートします。 Import Java.util.date; javax.swing.jbuttonをインポートします。 javax.swing.jlabelをインポートします。 javax.swing.jpanelをインポートします。 javax.swing.jtextfieldをインポートします。 javax.swing.timerをインポートします。 javax.swing.border.borderをインポートします。 javax.swing.border.echedborderをインポートします。 /*** jPanelから継承されたコントロールパネルクラス。プレビューウィンドウ、レベル、スコア、およびコントロールボタンが配置されています。 */ class controlpanelはjpanelを拡張します{private static final long serialversionuid = 3900659640646175724l; Private jtextfield tflevel = new jtextfield( "" + russiablocksgame.default_level)、tfscore = new jtextfield( "0")、tftime = new jtextfield( ""); Private jbutton btplay = new jbutton( "start")、btpause = new jbutton( "Pause")、btstop = new jbutton( "end the game")、bturnlevelup = new jbutton( "hiffarge the hifform")、btturnleveldown = new jbutton private jpanel pltip = new JPanel(new borderlayout()); private tippanel pltipblock = new Tippanel(); private jpanel plinfo = new JPanel(new gridlayout(4、1)); Private JPanel Plbutton = new JPanel(新しいGridlayout(6、1));プライベートタイマータイマー;プライベートボーダーボーダー= new etchedborder(etchedborder.raised、color.white、new Color(148、145、140)); /***コントロールパネルクラスのコンストラクター** @ParamゲームERSBLOCKSGAME、ERSBLOCKSGAMEクラスのインスタンス参照は、ERSBLOCKSGAMEクラスの動作を直接制御するのに便利です。 */ public Controlpanel(最終的なRussiablocksgameゲーム){setLayout(new gridlayout(3、1、0、2)); pltip.add(new Jlabel( "Next Square")、borderlayout.north); // component pltip.add(pltipblock); pltip.setborder(border); plinfo.add(new Jlabel( "難易度係数")); plinfo.add(tflevel); plinfo.add(new Jlabel( "score")); plinfo.add(tfscore); plinfo.setborder(国境); plbutton.add(btplay); btplay.setEnabled(true); plbutton.add(btpause); btpause.setEnabled(false); plbutton.add(btstop); btstop.setEnabled(false); plbutton.add(btturnlevelup); plbutton.add(btturnleveldown); plbutton.add(tftime); plbutton.setborder(border); tflevel.setedable(false); tfscore.setedable(false); tftime.setedable(false);追加(pltip); add(plinfo); add(plbutton); addKeyListener(new keyAdapter(){@Override public void keypressed(keyevent ke){if(!game.isplaying()){return;} ersblock block = game.getCurblock(); switch(ke.getKeycode()){case keyevent.vk_down:break.mowed(); block.moveleft; btplay.AddActionListener(new ActionListener(){//ゲーム@Override public void actionPerformed(actionEvent ae){game.playgame();}}); btPause.AddActionListener(new ActionListener(){//ゲーム@Override public void actionperformed(actionevent ae){if(btpause.getText()。equals( "Pause")){game.pausegame();} x eride {game.resugame();}}}}}};}; btstop.AddActionListener(new ActionListener(){// Game @Override public void ActionPerformed(ActionEvent AE){game.stopgame();}}); btturnLevelup.AddActionListener(new ActionListener(){//難易度@Override public void ActionPerformed(ActionEvent AE){try {int level = integer.getText()); } catch(numberformatexception {} requestfocus()}); btturnLevelDown.AddActionListener(new ActionListener(){//ゲーム難易度@Override public void actionPerformed(actionEvent AE){try {int level = integer.parseint(tflevel.getText()); {} requestfocus(); addComponentListener(new componentAdapter(){@Override public void componentresized(componentEvent ce){pltipblock.adjust();}}); Timer = new Timer(1000、new ActionListener(){@Override public void ActionPerformed(actionEvent ae){dateformat format = new simpledateFormat( "time:hh:mm:ss"); //システムは時刻日付date = new date(); tftime.settext(date.format(); tfscore.thedtext( " + tfscore.); game.getScore()); Timer.start(); } / ***ディスプレイ前のウィンドウのスタイルを設定** @paramスタイルint。 } /***ユーザーが設定したゲームレベルを取得します。 * * @return int、難易度レベル、1-esblocksgame.max_level */ public int getLevel(){int level = 0; try {level = integer.parseint(tflevel.getText()); } catch(numberformatexception e){} return level; } /***ユーザーにゲームの難易度を変更させます。 * * @paramレベル変更ゲーム難易度レベル */ public void setLevel(int level){if(level> 0 && level <11){tflevel.settext( "" + level); }} /***「start」ボタンのステータスを設定します。 */ public void setPlayButtonenable(boolean enable){btplay.setEnabled(enable); } public void setPauseButtonenable(boolean enable){btpause.setEnabled(enable); } public void setPauseButtonLabel(boolean Pause){btpause.settext(停止? "Pause": "継続"); } public void setStopButtonEnable(boolean enable){btstop.setEnabled(enable); } public void seturnlevelupbuttonenable(boolean enable){btturnlevelup.setEnabled(enable); } public void seturnLevelDownButtonenable(boolean enable){btturnLevelDown.SetEnabled(enable); } / ***リセットコントロールパネル* / public void reset(){tfscore.settext( "0"); pltipblock.setStyle(0); } / *** tippanelのボックス[] [] []の小さなボックスのサイズを再計算* / public void assud(){pltipblock.adjust(); } /***実装の詳細クラスプレディスプレイ前のウィンドウのクラス* /パブリッククラスティッパネル拡張jpanel {// tippanelは次のブロックの形状を表示して、プライベート静的な最終的な長いシリアルバーイヴァイド= 516055536714369976166l;プライベートカラーバックコラー= color.darkgray、frontcolor = color.white; private ersbox [] [] boxes = new ersbox [esblock.boxes_rows] [esblock.boxes_cols];プライベートINTスタイル、BoxWidth、BoxHeight; private boolean iStiled = false; / ***ウィンドウクラス以前のクラスコンストラクター*/ public tippanel(){for(int i = 0; i <boxes.length; i ++){for(int j = 0; j <boxes [i] .length; j ++){boxes [i] [j] = new ersbox(false); }}} / ***ディスプレイ前のウィンドウのブロックスタイルを設定します** @paramスタイルint。 Repaint(); } /*** jComponentクラスの関数を上書きし、コンポーネントを描画します。 * * @param Gグラフィックデバイス環境 */ @Override public void paintComponent(グラフィックスG){super.paintComponent(g); if(!iStiled){assuce(); } int key = 0x8000; for(int i = 0; i <boxes.length; i ++){for(int j = 0; j <boxes [i] .length; j ++){color color =((key&style)!= 0?frontcolor:backcolor); G.SetColor(色); g.fill3drect(j * boxwidth、i * boxheight、boxwidth、boxheight、true); key >> = 1; }}} / *** gウィンドウのサイズに応じて正方形のサイズを自動的に調整します* / public void assud(){boxwidth = getsize()。width / ersblock.boxes_cols; boxheight = getsize()。height / ersblock.boxes_rows; ISTILED = true; }}}ersbox.java
RussiaBlocksGameをパッケージします。 java.awt.dimensionをインポートします。 / ***正方形のクラスはブロックを構成する基本要素であり、独自の色を使用してブロックの外観を表します*/ public class ersboxはクローン可能{private boolean iscolorを実装します。プライベートディメンションサイズ= new Dimension(); / ** *正方形のクラスのコンストラクター、 * * @param iscolorは、前景の色を使用してこの正方形の前景色、偽の使用背景色 */ public ersbox(boolean iscolor){this.iscolor = iscolor; } / ***この正方形は前景の色で表されています** @return boolean、真面目の色で表現されている真、背景色で誤って表現されています* / public boolean iscolorbox(){return iscolor; } / ** *正方形の色を設定します * * @param iscolor boolean、真上の色で表現された真、背景色で誤って表されます * / public void setcolor(boolean iscolor){this.iscolor = iscolor; } / ***この正方形のサイズを取得** @returnディメンション、正方形のサイズ* / public dimension getsize(){return size; } / ** *正方形のサイズを設定します。 * * @paramサイズ寸法、正方形のサイズ * / public void setSize(dimension size){this.size = size; } / ***オブジェクトのオブジェクトClone()を上書きし、clone** @return objectを実装します。 try {clone = super.clone(); } catch(Exception ex){ex.printstacktrace(); }クローンを返します。 }}ersblock.java
RussiaBlocksGameをパッケージします。 / ***スレッドクラス(スレッド)から継承されたブロッククラスは、4×4ブロック(ersbox)で構成され、動き、転倒、ブロックの変形などを制御します。 / ***ブロックで占める列の数は4列です*/ public final static int boxe_cols = 4; / ***アップグレードの変化を滑らかにする要因は、最後の段階の速度差をほぼ2倍にしないようにします*/ public final static int revel_flatness_gene = 3; / ** *各行のブロックの2つの同様のレベル(ミリ秒)の違い */ public final static int oter_levels_degress_time = 50; / ** *ブロックのスタイルの数は7 */ public final static int block_kind_number = 7です。 / ** *各スタイルの反転状態のブロックのタイプは4 */ public final static int block_status_number = 4です。 /*** 28それぞれ7つのモデルに対応している*/パブリック最終的な静的int [] [] styles = {//合計28状態{0x0f00、0x4444、0x0f00、0x4444}、//長いストライプの4つの状態{0x04e0、0x0464、0x00e4、0x04c44、0x04c44、0x04c44、fon 0x6c00、0x4620、0x6c00}、//逆zタイプの4つの状態{0x2640、0xc600、0x2640、0xc600}、// zタイプの4つの状態{0x6220、0x1700、0x2230、0x0740}、0x0e20e20e20e20e20 {0x0740、 0x44c0、0x8e00}、//逆の4つの状態{0x0660、0x0660、0x0660、0x0660、0x0660}、//ブロックの4つの状態};プライベートGamecanvas Canvas; private ersbox [] [] boxes = new ersbox [boxes_rows] [boxes_cols];プライベートINTスタイル、Y、X、レベル。 private boolean daping = false、moving = true; / ***コンストラクター、特定のブロックを生成します** @paramスタイルのブロックのスタイルは、スタイルの28の値の1つに対応します* @param yスタート位置、キャンバスの左上隅の座標ライン* @param xスタート位置、キャンバスの左上隅の調整列レベル、gamecanvasキャンバス){this.style = style; this.y = y; this.x = x; this.level = level; this.canvas = canvas; int key = 0x8000; for(int i = 0; i <boxes.length; i ++){for(int j = 0; j <boxes [i] .length; j ++){boolean iscolor =((style&key)!= 0);ボックス[i] [j] = new ersbox(iscolor); key >> = 1; } } 画面(); } / ***スレッドクラスのrun()関数は、ブロックが再び落ちることができないまでブロックをカバーしてドロップします* / @Override public void run(){while(moving){try {sleep(oty_levels_degress_time*(russiablocks_level -level_flatensed_gene)); } catch(arturnedexception ie){ie.printstacktrace(); } //後続の移動とは、100ミリ秒の待機中に移動が変更されていないことを意味します(!一時停止){moving =(moveto(y + 1、x)&& moving); }}}}} / ** *ブロックは1つのグリッドを左に移動します * / public void moveleft(){moveto(y、x -1); } / ** *ブロックは1つのグリッドを右に移動します * / public void moveright(){moveto(y、x + 1); } / ** *ブロックは1つのグリッドを右に移動します * / public void movedown(){moveto(y + 1、x); } / *** block variant* / public void turnnext(){for(int i = 0; i <block_kind_number; i ++){for(int j = 0; j <block_status_number; j ++){if(styles [i] [j] == style){int newstyle = style [i] [i] [j+1) turnto(newStyle);戻る; }}}} public void startmove(){dapusing = false;移動= true; } / ***ゲームの一時停止に対応するブロックの居場所を一時停止します* / public void pausemove(){pausing = true; // moving = false; } /** * Continue the whereabouts of the block, corresponding to the game continue*/ public void resumeMove() { pausing = false; moving = true; } /** * Stop the whereabouts of the block, corresponding to the game pause*/ public void stopMove() { pausing = false; moving = false; } /** * Remove the current block from the corresponding position of the canvas and will not be reflected until the next time you repaint the canvas*/ private void erase() { for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(i + y, j + x); if (box == null) { continue; } box.setColor(false); } } } } } /** * Let the current block be placed in the corresponding position of the canvas, and you have to wait until the next time you repaint the canvas*/ private void display() { for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(i + y, j + x); if (box == null) { continue; } box.setColor(true); } } } } } /** * Can the current block be moved to the location specified by newRow/newCol* * @param newRow int, the destination line* @param newCol int, destination column * @return boolean, true-can move, false-cannot move*/ public boolean isMoveAble(int newRow, int newCol) { erase(); for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(i + newRow, j + newCol); if (box == null || (box.isColorBox())) { display(); return false; } } } } 画面(); return true; } /** * Move the current block to the location specified by newRow/newCol* * @param newRow int, destination row* @param newCol int, destination column* @return boolean, true-move successfully, false-move failed*/ private synchronized boolean moveTo(int newRow, int newCol) { if (!isMoveAble(newRow, newCol) || !moving) { return false; } erase(); y = newRow; x = newCol;画面(); canvas.repaint(); return true; } /** * Can the current block become the block style specified by newStyle, mainly considering the boundary and being blocked by other blocks and cannot be moved* * @param newSytle int, the block style you want to change corresponds to one of the 28 values of STYLES* @return boolean, true-can change, false-cannot change*/ private boolean isTurnAble(int newStyle) { int key = 0x8000; erase(); for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if ((newStyle & key) != 0) { ErsBox box = canvas.getBox(i + y, j + x); if (box == null || (box.isColorBox())) { display(); return false; } } key >>= 1; } } 画面(); return true; } /** * Turn the current block into the block style specified by newStyle* * @param newStyle int, the block style you want to change, corresponding to one of the 28 values of STYLES* @return true-change succeeds, false-change fails*/ private boolean turnTo(int newStyle) { if (!isTurnAble(newStyle) || !moving) { return false; } erase(); int key = 0x8000; for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { boolean isColor = ((newStyle & key) != 0); boxes[i][j].setColor(isColor); key >>= 1; } } style = newStyle;画面(); canvas.repaint(); return true; } }Main.java
package RussiaBlocksGame; /** * 程序入口函数* * @param args String[],附带的命令行参数*/ public class Main { public static void main(String[] args) { new RussiaBlocksGame("俄罗斯方块:杨宇杰"); } }上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。