1.MainActivity代码如下:
public class MainActivity extends Activity {
private GamePanel mGamePanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGamePanel = new GamePanel(this);
setContentView(mGamePanel);
}
}
2.GamePanel,通过自定义View来实现具体的操作
public class GamePanel extends View {
private int colorBlack = Color.BLACK;
private int colorWhite = Color.WHITE;
private int colorDivider = Color.GRAY;
private Paint mPaint;
private Paint mLinePaint;
private int blockWidth;
private int blockHeight;
private int dividerWidth;
private int mGroup[][] = null;
private int mCurRow = Config.ROW - 1;
private Scroller mScroller;
public GamePanel(Context context) {
super(context);
init();
}
public GamePanel(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mScroller = new Scroller(getContext());
dividerWidth = 1;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLinePaint.setColor(colorDivider);
mLinePaint.setStrokeWidth(dividerWidth);
startGame();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = AppUtils.getScreenWidth(getContext());
int height = AppUtils.getScreenHeight(getContext()) - AppUtils.getStatusHeight(getContext());
blockWidth = width / Config.SIZE;
blockHeight = (height - (Config.SIZE - 1) * dividerWidth) / Config.SIZE;
LogUtils.e("width:" + width + ",height:" + height);
LogUtils.e("blockWidth:" + blockWidth + ",blockHeight:" + blockHeight);
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
setBackgroundColor(colorWhite);
if (mCurRow == 0) {
Toast.makeText(getContext(), "游戏成功", Toast.LENGTH_SHORT).show();
startGame();
return;
}
int max = Config.ROW - 1;
int top = getHeight() - blockHeight;
for (int i = max; i >= 0; i--) {
int left = 0;
for (int j = 0; j < Config.SIZE; j++) {
if (mGroup[i][j] == Config.BLACK) {
mPaint.setColor(colorBlack);
canvas.drawRect(left, top, left + blockWidth, top + blockHeight, mPaint);
}
left += blockWidth;
canvas.drawLine(left, top, left, top + blockHeight, mLinePaint);
}
canvas.drawLine(0, top, getWidth(), top, mLinePaint);
top -= dividerWidth;
top -= blockHeight;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
float x = event.getX();
float y = event.getY();
int diff = mCurRow - 4;
int i = (int) y / blockHeight;
int j = (int) x / blockWidth;
LogUtils.e("click item i=" + i + ", j=" + j + ",diff=" + diff + ",mCurRow=" + mCurRow);
i += diff;
if (i == mCurRow - 1 && mGroup[i][j] == Config.BLACK) {
mScroller.startScroll(0, getScrollY(), 0, -(blockHeight + dividerWidth));
invalidate();
mCurRow--;
} else {
Toast.makeText(getContext(), "游戏失败", Toast.LENGTH_SHORT).show();
startGame();
}
}
return true;
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(0, mScroller.getCurrY());
invalidate();
}
}
public void startGame() {
if (mGroup == null) {
mGroup = new int[Config.ROW][Config.SIZE];
}
for (int i = 0; i < Config.ROW; i++) {
for (int j = 0; j < Config.SIZE; j++) {
mGroup[i][j] = Config.WHITE;
}
int random = AppUtils.getRandom();
mGroup[i][random] = Config.BLACK;
}
mCurRow = Config.ROW;
}
}
该项目已经放在码云(开源中国git)上,有兴趣的可以去看看别踩白块儿