您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > Android扫雷游戏源码分析
No.1第1章游戏源码1.1MinesweeperGamepackagecom.VertexVerveInc.Games;importjava.util.Random;importandroid.app.Activity;importandroid.graphics.Typeface;importandroid.os.Bundle;importandroid.os.Handler;importandroid.view.Gravity;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.View.OnLongClickListener;importandroid.widget.ImageButton;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.TableRow.LayoutParams;importandroid.widget.TableLayout;importandroid.widget.TableRow;importandroid.widget.TextView;importandroid.widget.Toast;publicclassMinesweeperGameextendsActivity{privateTextViewtxtMineCount;privateTextViewtxtTimer;privateImageButtonbtnSmile;privateTableLayoutmineField;//tablelayouttoaddminestoprivateBlockblocks[][];//blocksforminefieldprivateintblockDimension=24;//widthofeachblockprivateintblockPadding=2;//paddingbetweenblocksprivateintnumberOfRowsInMineField=9;privateintnumberOfColumnsInMineField=9;privateinttotalNumberOfMines=10;//timertokeeptrackoftimeelapsedprivateHandlertimer=newHandler();privateintsecondsPassed=0;privatebooleanisTimerStarted;//checkiftimeralreadystartedornotprivatebooleanareMinesSet;//checkifminesareplantedinblocksprivatebooleanisGameOver;No.2privateintminesToFind;//numberofminesyettobediscovered@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);txtMineCount=(TextView)findViewById(R.id.MineCount);txtTimer=(TextView)findViewById(R.id.Timer);//setfontstylefortimerandminecounttoLCDstyleTypefacelcdFont=Typeface.createFromAsset(getAssets(),fonts/lcd2mono.ttf);txtMineCount.setTypeface(lcdFont);txtTimer.setTypeface(lcdFont);btnSmile=(ImageButton)findViewById(R.id.Smiley);btnSmile.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewview){endExistingGame();startNewGame();}});mineField=(TableLayout)findViewById(R.id.MineField);showDialog(ClicksmileytostartNewGame,2000,true,false);}privatevoidstartNewGame(){//plantminesanddorestofthecalculationscreateMineField();//displayallblocksinUIshowMineField();minesToFind=totalNumberOfMines;isGameOver=false;secondsPassed=0;}privatevoidshowMineField(){//rememberwewillnotshow0thandlastRowandColumns//theyareusedforcalculationpurposesonlyfor(introw=1;rownumberOfRowsInMineField+1;row++){TableRowtableRow=newTableRow(this);tableRow.setLayoutParams(newLayoutParams((blockDimension+2*blockPadding)No.3*numberOfColumnsInMineField,blockDimension+2*blockPadding));for(intcolumn=1;columnnumberOfColumnsInMineField+1;column++){blocks[row][column].setLayoutParams(newLayoutParams(blockDimension+2*blockPadding,blockDimension+2*blockPadding));blocks[row][column].setPadding(blockPadding,blockPadding,blockPadding,blockPadding);tableRow.addView(blocks[row][column]);}mineField.addView(tableRow,newTableLayout.LayoutParams((blockDimension+2*blockPadding)*numberOfColumnsInMineField,blockDimension+2*blockPadding));}}privatevoidendExistingGame(){stopTimer();//stopiftimerisrunningtxtTimer.setText(000);//revertalltexttxtMineCount.setText(000);//revertminescountbtnSmile.setBackgroundResource(R.drawable.smile);//removeallrowsfrommineFieldTableLayoutmineField.removeAllViews();//setallvariablestosupportendofgameisTimerStarted=false;areMinesSet=false;isGameOver=false;minesToFind=0;}privatevoidcreateMineField(){//wetakeonerowextrarowforeachside//overalltwoextrarowsandtwoextracolumns//firstandlastrow/columnareusedforcalculationspurposesonly//x|xxxxxxxxxxxxxx|x//------------------//x||x//x||x//------------------//x|xxxxxxxxxxxxxx|x//therowandcolumnsmarkedasxarejustusedtokeepcountsofnearbyminesblocks=newBlock[numberOfRowsInMineField+2][numberOfColumnsInMineField+2];for(introw=0;rownumberOfRowsInMineField+2;row++)No.4{for(intcolumn=0;columnnumberOfColumnsInMineField+2;column++){blocks[row][column]=newBlock(this);blocks[row][column].setDefaults();//passcurrentrowandcolumnnumberasfinalint'stoeventlisteners//thiswaywecanensurethateacheventlistenerisassociatedto//particularinstanceofblockonlyfinalintcurrentRow=row;finalintcurrentColumn=column;//addClickListener//thisistreatedasLeftMouseclickblocks[row][column].setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewview){//starttimeronfirstclickif(!isTimerStarted){startTimer();isTimerStarted=true;}//setminesonfirstclickif(!areMinesSet){areMinesSet=true;setMines(currentRow,currentColumn);}//thisisnotfirstclick//checkifcurrentblockisflagged//ifflaggedthedon'tdoanything//asthatoperationishandledbyLongClick//ifblockisnotflaggedthenuncovernearbyblocks//tillwegetnumberedminesif(!blocks[currentRow][currentColumn].isFlagged()){//opennearbyblockstillwegetnumberedblocksrippleUncover(currentRow,currentColumn);//didweclickedamineif(blocks[currentRow][currentColumn].hasMine()){//Oops,gameoverfinishGame(currentRow,currentColumn);No.5}//checkifwewinthegameif(checkGameWin()){//markgameaswinwinGame();}}}});//addLongClicklistener//thisistreatedasrightmouseclicklistenerblocks[row][column].setOnLongClickListe
本文标题:Android扫雷游戏源码分析
链接地址:https://www.777doc.com/doc-6124344 .html