4. 因为游戏实时性不高,这里没有用到 SurfaceView,下次 Android 开发网给大家提供一个将对高级些
的例子。
完整工程源代码下载 http://down.android123.com/dev/android_mine_src.zip (482KB)
关键代码如下:
public class MinesweeperGame extends Activity
{
private TextView txtMineCount;
private TextView txtTimer;
private ImageButton btnSmile;
private TableLayout mineField; // table layout to add mines to
private Block blocks[][]; // blocks for mine field
private int blockDimension = 24; // width of each block
private int blockPadding = 2; // padding between blocks
private int numberOfRowsInMineField = 9;
private int numberOfColumnsInMineField = 9;
private int totalNumberOfMines = 10;
// timer to keep track of time elapsed
private Handler timer = new Handler();
private int secondsPassed = 0;
private boolean isTimerStarted; // check if timer already started or not
private boolean areMinesSet; // check if mines are planted in blocks
private boolean isGameOver;
private int minesToFind; // number of mines yet to be discovered
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMineCount = (TextView) findViewById(R.id.MineCount);
txtTimer = (TextView) findViewById(R.id.Timer);
// set font style for timer and mine count to LCD style
Typeface lcdFont = Typeface.createFromAsset(getAssets(),
"fonts/lcd2mono.ttf");
评论4
最新资源