本程序有两个类,分别为GameView.java和Main.java
GameView.java
package org.yexing.android.games.mine;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
public class Main extends Activity {
GameView gameView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
gameView = new GameView(this);
setContentView(gameView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
gameView.gameState = GameView.STATE_LOST;
super.onPause();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
gameView.onKeyDown(keyCode, event);
return super.onKeyDown(keyCode, event);
}
G
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
gameView.onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
}
}
package org.yexing.android.games.mine;
import java.util.Random;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
public class GameView extends View {
private static final String TAG = "GameView";
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
GameView.this.updateView();
GameView.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
// 游戏状态 开始 胜利 失败 暂停
public static final int STATE_PLAYING = 0;
public static final int STATE_WIN = 1;
public static final int STATE_LOST = 2;
public static final int STATE_PAUSE = 3;
public int gameState;
//随机
private static final Random random = new Random();
//画笔
private Paint paint;
//发送消息
private String message;
//格子的宽度
private static final int tileWidth = 16;
private static final int tileHeight = 16;
//格子的数量
private static final int tilesCount = 19;
private static final int margin = 16;
private static final int titleHeight = 30;
//棋子
private Bitmap[] tiles;
//表面层
private int[][] mapSky;
//底层