当前位置: 首页 > news >正文

Android案例手册 - 实现一个华容道拼图游戏

往期文章分享
  • 点击跳转=>《导航贴》- Unity手册,系统实战学习
  • 点击跳转=>《导航贴》- Android手册,重温移动开发

👉关于作者

众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单

在这里插入图片描述

本文约5千字,新手阅读需要7分钟,复习需要3分钟收藏随时查阅不再迷路

文章目录

    • 👉关于作者
    • 👉实践过程
      • 😜使用
      • 😜子View的移动
      • 😜ViewGroup创建棋子
      • 😜全部源码
    • 👉其他

👉实践过程

Hello,大家好啊,我是小空,前两天我们学习了很多基础内容,今天我们学习点稍微复杂的,提升下自己,刺激一下。

九宫格游戏,或者说华容道游戏,是传承了很久的游戏了,今天我们就用Android实现一下吧。
先看效果图

在这里插入图片描述

从图中我们做出如下分析:

  1. 展示N*N-1的数字列表,并且是随机排序的,还要有一个空置的view也要创建

  2. 每个数字都拥有点击事件,所以每个数字应该都是独立的View,然后所有数字嵌套在ViewGroup中

  3. 每个数字View都有移动动画并且有上下左右的区分,且移动距离为自身View的宽度

  4. 每个View记录自己正确的位置,对外提供方法移动该view位置,当移动的位置和记录的位置一直表示正确,回调出去

  5. ViewGroup进行for循环创建指定数量的子View,并且设置好随机位置

  6. 子View回调出来的点击事件记录正确个数,满足条件则再次回调更上层做业务处理。

😜使用

我们先来看看使用方式:

<com.akitaka.math.ui.widget.KlotskiViewGroup
        android:id="@+id/pv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#dddddd" />

😜子View的移动

在这里插入图片描述

😜ViewGroup创建棋子

在这里插入图片描述

😜全部源码

/**
 * 华容道的实体类
 */
public class ModelKlotskiPosition {

    /**
     * X轴定位
     */
    private int x;

    /**
     * Y轴定位
     */
    private int y;

    public ModelKlotskiPosition(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (!(obj instanceof ModelKlotskiPosition)) {
            return false;
        }
        ModelKlotskiPosition o = (ModelKlotskiPosition) obj;
        return x == o.x && y == o.y;
    }
}
@SuppressLint("ViewConstructor")
public class KlotskiChildView extends View {

    /**
     * 当前数字
     */
    private int intNumber;

    /**
     * 正确的定位
     */
    private ModelKlotskiPosition positionCorrect;

    /**
     * 当前的定位
     */
    private ModelKlotskiPosition positionCurrent;

    /**
     * 当前View的长款
     */
    private int length;

    /**
     * 画笔
     */
    private Paint paint;

    /**
     * 位置更改监听
     */
    private OnPositionChangedListener listener;

    /**
     * 当前定位是否正确
     */
    private boolean correct = true;

    /**
     * 初始化
     *
     * @param context    上下文对象
     * @param intDifficulty 难度系数
     * @param intNumber     数字
     */
    public KlotskiChildView(Context context, int intDifficulty, int intNumber, OnPositionChangedListener listener) {
        super(context);
        init(intDifficulty, intNumber, listener);
    }

    private void init(int intDifficulty, int intNumber, OnPositionChangedListener listener) {
        this.intNumber = intNumber;
        //根据难度系数与棋子的数字,得出棋子的正确坐标
        int x = intNumber % intDifficulty == 0 ? intDifficulty : intNumber % intDifficulty;
        int y = (intNumber - 1) / intDifficulty + 1;

        positionCorrect = new ModelKlotskiPosition(x, y);
        positionCurrent = new ModelKlotskiPosition(x, y);

        //初始化 画笔
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(100f * (4f / intDifficulty));
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);

        this.listener = listener;
    }


    /**
     * 设置棋子当前位置
     */
    public void setCurrentPosition(int x, int y) {
        if (positionCurrent == null) {
            positionCurrent = new ModelKlotskiPosition(positionCorrect.getX(), positionCorrect.getY());
        }
        positionCurrent.setX(x);
        positionCurrent.setY(y);
        verifyCorrect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //将画布坐标系移到中间
        canvas.translate(length / 2f, length / 2f);
        //将数字画在画布上
        @SuppressLint("DrawAllocation") Rect rect = new Rect();
        String s = String.valueOf(intNumber);
        paint.getTextBounds(s, 0, s.length(), rect);
        canvas.drawText(s, -rect.width() / 2f, rect.height() / 2f, paint);
    }

    /**
     * X轴移动
     *
     * @param left 是否向左移动
     */
    public void moveX(boolean left) {
        final int currX = getLeft();
        //目标X位置
        int targetX = currX + (left ? -length : length);
        ValueAnimator valueAnimator = ValueAnimator.ofInt(currX, targetX);
        valueAnimator.addUpdateListener(animation -> {
            int value = (int) animation.getAnimatedValue();
            //重新布局自己
            layout(value, getTop(), value + length, getBottom());
        });
        valueAnimator.setDuration(200);
        valueAnimator.start();
        positionCurrent.setX(positionCurrent.getX() + (left ? -1 : 1));
        verifyCorrect();
    }

    /**
     * Y轴移动
     *
     * @param top 是否向上移动
     */
    public void moveY(boolean top) {
        final int currY = getTop();
        int targetY = currY + (top ? -length : length);
        ValueAnimator valueAnimator = ValueAnimator.ofInt(currY, targetY);
        valueAnimator.addUpdateListener(animation -> {
            int value = (int) animation.getAnimatedValue();
            layout(getLeft(), value, getRight(), value + length);
        });
        valueAnimator.setDuration(200);
        valueAnimator.start();
        positionCurrent.setY(positionCurrent.getY() + (top ? -1 : 1));
        verifyCorrect();
    }

    /**
     * 验证当前位置是否正确,并根据逻辑调用改变监听
     */
    private void verifyCorrect() {
        if (correct != (positionCorrect.equals(positionCurrent))) {
            correct = !correct;
            listener.onPositionChanged(correct);
        }
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public ModelKlotskiPosition getCorrectPosition() {
        return positionCorrect;
    }

    public ModelKlotskiPosition getCurrentPosition() {
        return positionCurrent;
    }

    public interface OnPositionChangedListener {
        /**
         * 位置调动时调用
         *
         * @param correct 位置是否正确
         */
        void onPositionChanged(boolean correct);
    }
}
/**
 * @author 芝麻粒儿
 */
public class KlotskiViewGroup extends ViewGroup implements KlotskiChildView.OnPositionChangedListener {

    /**
     * 难度系数
     * 默认为3
     */
    private int intDifficulty = 3;

    /**
     * 棋子正确的数量
     */
    private int intCorrectCount = 0;

    /**
     * 空闲位置
     */
    private ModelKlotskiPosition idlePosition;

    /**
     * 游戏结束监听
     */
    private OnPlayOverListener listener;

    public KlotskiViewGroup(Context context) {
        super(context);
    }

    public KlotskiViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public KlotskiViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        intCorrectCount = intDifficulty * intDifficulty - 1;
        //初始化空格定位,默认为最后一个格子
        idlePosition = new ModelKlotskiPosition(intDifficulty, intDifficulty);
        //清空所有child 如果不清空,会出现底层背景重叠的问题
        removeAllViews();
        for (int i = 1; i < intDifficulty * intDifficulty; i++) {
            //创建棋子
            KlotskiChildView pieceView = new KlotskiChildView(getContext(), intDifficulty, i, this);
            //给棋子添加布局属性
            pieceView.setBackgroundResource(R.drawable.bg_piece);
            LayoutParams layoutParams = new LayoutParams(pieceView.getLength(), pieceView.getLength());
            pieceView.setLayoutParams(layoutParams);
            //将棋子添加有序临时数组中
            addView(pieceView);
            //设置棋子的点击事件
            pieceView.setOnClickListener(v -> processPieceClick((KlotskiChildView) v));
        }
        upsetPieces();
    }

    /**
     * 打乱棋子
     * 之前使用的随机数打乱法,会导致最终无解
     */
    public void upsetPieces() {
        //打乱次数,难度系数的平方
        int count = intDifficulty * intDifficulty * 10;
        do {
            //随机数,模拟点击位置
            int round = (int) Math.floor(Math.random() * getChildCount());
            //根据难度系数与模拟点击的位置数字,得出棋子的正确坐标
            int randomX = round % intDifficulty == 0 ? intDifficulty : round % intDifficulty;
            int randomY = (round - 1) / intDifficulty + 1;
            //空格坐标
            int idleX = idlePosition.getX();
            int idleY = idlePosition.getY();
            if (randomX == idleX && randomY != idleY) {
                count--;
                idlePosition.setY(randomY);
                for (int i = 0; i < getChildCount(); i++) {
                    KlotskiChildView pieceView = (KlotskiChildView) getChildAt(i);
                    int currX = pieceView.getCurrentPosition().getX();
                    int currY = pieceView.getCurrentPosition().getY();
                    if (currX == idleX) {
                        if (currY <= randomY && currY > idleY) {
                            pieceView.setCurrentPosition(currX, currY - 1);
                        } else if (currY >= randomY && currY < idleY) {
                            pieceView.setCurrentPosition(currX, currY + 1);
                        }
                    }
                }

            } else if (randomY == idleY && randomX != idleX) {
                count--;
                idlePosition.setX(randomX);
                for (int i = 0; i < getChildCount(); i++) {
                    KlotskiChildView pieceView = (KlotskiChildView) getChildAt(i);
                    int currY = pieceView.getCurrentPosition().getY();
                    int currX = pieceView.getCurrentPosition().getX();
                    if (currY == idleY) {
                        if (currX <= randomX && currX > idleX) {
                            pieceView.setCurrentPosition(currX - 1, currY);
                        } else if (currX >= randomX && currX < idleX) {
                            pieceView.setCurrentPosition(currX + 1, currY);
                        }
                    }
                }
            }
        } while (count >= 0);

    }

    /**
     * 处理棋子的点击事件
     *
     */
    private void processPieceClick(KlotskiChildView pieceView) {
        ModelKlotskiPosition clickPosition = pieceView.getCurrentPosition();
        int idleX = idlePosition.getX();
        int idleY = idlePosition.getY();
        //若点击的棋子与空格在X轴交汇,则为上下移动
        if (clickPosition.getX() == idleX) {
            //将空格定位到点击的棋子位置
            idlePosition.setY(clickPosition.getY());
            //遍历棋子,找出被点击的棋子与原空格位置之间的所有棋子,一起移动
            for (int i = 0; i < getChildCount(); i++) {
                KlotskiChildView pv = (KlotskiChildView) getChildAt(i);
                int currX = pv.getCurrentPosition().getX();
                int currY = pv.getCurrentPosition().getY();
                if (currX == idleX) {
                    if (clickPosition.getY() > idleY && currY <= clickPosition.getY() && currY > idleY) {
                        pv.moveY(true);
                    } else if (clickPosition.getY() < idleY && currY >= clickPosition.getY() && currY < idleY) {
                        pv.moveY(false);
                    }
                }
            }
        }
        //此处逻辑与上面类似,不重复
        else if (clickPosition.getY() == idleY) {
            idlePosition.setX(clickPosition.getX());
            for (int i = 0; i < getChildCount(); i++) {
                KlotskiChildView pv = (KlotskiChildView) getChildAt(i);
                int currY = pv.getCurrentPosition().getY();
                int currX = pv.getCurrentPosition().getX();
                if (currY == idleY) {
                    if (clickPosition.getX() > idleX && currX <= clickPosition.getX() && currX > idleX) {
                        pv.moveX(true);
                    } else if (clickPosition.getX() < idleX && currX >= clickPosition.getX() && currX < idleX) {
                        pv.moveX(false);
                    }
                }

            }
        }
    }

    /**
     * 设置难度系数
     */
    public void setDifficulty(int dif) {
        intDifficulty = dif;
        init();
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //将宽高设置为统一长度,取最小值
        int defaultWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        int defaultHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        int minSpec = defaultWidth < defaultHeight ? widthMeasureSpec : heightMeasureSpec;
        super.onMeasure(minSpec, minSpec);
        int childLength = Math.min(defaultWidth, defaultHeight) / intDifficulty;
        for (int i = 0; i < getChildCount(); i++) {
            ((KlotskiChildView) getChildAt(i)).setLength(childLength);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //遍历所有的child,将子View依次排开
        for (int i = 0; i < getChildCount(); i++) {
            KlotskiChildView child = (KlotskiChildView) getChildAt(i);
            ModelKlotskiPosition cp = child.getCurrentPosition();
            int cX = (cp.getX() - 1) * child.getLength();
            int cY = (cp.getY() - 1) * child.getLength();
            child.layout(cX, cY, cX + child.getLength(), cY + child.getLength());
        }
    }

    @Override
    public void onPositionChanged(boolean correct) {
        if (correct) {
            intCorrectCount++;
        } else {
            intCorrectCount--;
        }
        if (intCorrectCount >= ((intDifficulty * intDifficulty) - 1)) {
            if (listener != null) {
                listener.onPlayOver(0);
            }
        }
    }

    public void setListener(OnPlayOverListener listener) {
        this.listener = listener;
    }

    public interface OnPlayOverListener {
        /**
         * 游戏结束回调
         *
         * @param state 结束状态,目前默认为0
         *              预留后面添加超时失败等状态
         */
        void onPlayOver(int state);
    }
}

👉其他

📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://zhima.blog.csdn.net/
📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。

温馨提示点击下方卡片获取更多意想不到的资源。
空名先生

相关文章:

  • 软件设计师笔记-----系统安全分析与设计
  • 「Nature领衔」8月BIOTREE成功助力发表文章17篇,总IF:190+!
  • 高分二号卫星影像下载
  • JMeter与Selenium WebDriver集成的价值
  • 数据湖浅析(以hudi为例)
  • 嵌入式分享合集60
  • Mac Vue 项目上传到阿里云服务器及 nginx
  • 51单片机学习:ADC模数转换实验--光敏电阻AD采集
  • postgresql源码学习(十五)—— 行锁③-死锁检测
  • 【linux】shell 编程之流程控制语句详解
  • [PAT练级笔记] 34 Basic Level 1034 有理数四则运算
  • 【探花交友】保存用户信息、上传用户头像、用户信息管理
  • ElasticSearch Client问题整理2
  • Python必知必会 os 模块详解
  • Promise系列学习
  • 「译」Node.js Streams 基础
  • avalon2.2的VM生成过程
  • java概述
  • js作用域和this的理解
  • mysql外键的使用
  • Odoo domain写法及运用
  • Spring核心 Bean的高级装配
  • vagrant 添加本地 box 安装 laravel homestead
  • Vue ES6 Jade Scss Webpack Gulp
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 用 Swift 编写面向协议的视图
  • 用jQuery怎么做到前后端分离
  • 阿里云重庆大学大数据训练营落地分享
  • ​比特币大跌的 2 个原因
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • # 飞书APP集成平台-数字化落地
  • ###STL(标准模板库)
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)jQuery 基础
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • (转载)从 Java 代码到 Java 堆
  • ***监测系统的构建(chkrootkit )
  • .chm格式文件如何阅读
  • .net CHARTING图表控件下载地址
  • .net core 的缓存方案
  • .Net7 环境安装配置
  • .NET开发不可不知、不可不用的辅助类(三)(报表导出---终结版)
  • .NET开源、简单、实用的数据库文档生成工具
  • @ 代码随想录算法训练营第8周(C语言)|Day57(动态规划)
  • @Autowired 和 @Resource 区别的补充说明与示例
  • @GlobalLock注解作用与原理解析
  • []Telit UC864E 拨号上网
  • [2010-8-30]
  • [C#]winform利用seetaface6实现C#人脸检测活体检测口罩检测年龄预测性别判断眼睛状态检测
  • [C#学习笔记]注释
  • [C++进阶篇]STL中vector的使用