小游戏-别踩白块儿

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)上,有兴趣的可以去看看别踩白块儿

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值