Android2048 practical exercise summary

Learn GridLayout from 2048

When learning Android, I did a 2048 exercise myself, and I learned everything by doing it myself.

game interface
The structure of this game is still very simple, and the core logic is in the middle 4*4 layout.
I've written a web front end before, so I wanted the game's interface to fit on all screen sizes, so I decided to use the weighting feature of GridLayout. (step on the pit to start)


Code to add children to layout

//实例化子项,因为是练习,想着用动态添加的方法来做。子项是继承自View自己画的,因为很简单就不详细说明了。
cards[i][j] = new Card(this.context);

/**设置子项在父项的属性
*子项动态设置属性需要使用.LayoutParams()方法
*继承的GridLayout有LayoutParams可以帮助设置GridLayout独有的属性
*GridLayout.spec(int start, int size, float weight)这是其中一个方法,不过参数的名字意义是一样的。
start是指你的这个子项是要放在哪一行或者哪一列;size是指子项在这一行(或列)占几个格子的宽度;weight是指子项的比重;
*遇到的一个玄学问题就是,用了权重比的方法分配子项的空间,如果子项宽高没有设置为零就会显示不出来。因为实在是太菜了,实在弄不清这到底是为什么
*还有一个让人头大的事情就是用了权重比之后,如果再设置Margin值,再宽度上还是四等分,但是长度上就会变成math_parent的效果(太菜了也不明白)
GridLayout.Spec row = GridLayout.spec(i , 1, 1f);
GridLayout.Spec column = GridLayout.spec(j , 1, 1f);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(row, column);
params.width = 0;
params.height = 0;
cards[i][j].setLayoutParams(params);
cards[i][j].setNumber(datas[i][j]);
this.addView(cards[i][j]);

Listen for user gestures

Originally, I wanted to implement the logic in the onTouchEven() method, but I couldn't find it, so I had to use the dispatchTouchEvent() method instead.

I realized the monitoring of the sliding direction by monitoring the coordinates of the user raising and lowering the finger. It is very laborious to write the code to calculate the user's behavior.

    enum Direction{
        up, down, left, right, NULL
    }

    //方向集
    Direction direction = Direction.NULL;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //获取触控位置
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
           down_x = ev.getX();
           down_y = ev.getY();
        }

        if (ev.getAction() == MotionEvent.ACTION_UP){
           up_x = ev.getX();
           up_y = ev.getY();
        }
        if (up_x < 1 && up_y <1){
            up_x = down_x;
            up_y = down_y;
        }

        //计算用户行为
        float r_x = down_x - up_x;
        float r_y = down_y - up_y;
        float XorY = Math.abs(r_x) - Math.abs(r_y);
        if (XorY > 0){
            if (r_x > 0){
                direction = Direction.left;

            }else {
                direction = Direction.right;
            }
            mOnDirectionListener();
        }else if (XorY < 0){
            if (r_y > 0){
                direction = Direction.up;
            }else {
                direction = Direction.down;
            }
            mOnDirectionListener();
        }else {
            direction = Direction.NULL;
        }
        up_x=up_y=0;
        return true;
    }

The mOnDirectionListener() method is used to calculate the result after sliding according to different sliding directions. The core algorithm of the game is here, but this is not the core of this tutorial. There are various implementations on Baidu.


Outline Design Ideas

The theme view of this game consists of Field (a subclass of GridLayout), and Card (custom View). Field has the ability to change the data according to the user's gesture logic operation, these capabilities are run when the dispatchTouchEvent() method is called. There is a set of Cards in Field, and a corresponding set of int-type arrays. Each logic only operates on the arrays, and only modifies the values ​​corresponding to the Cards at one time after completion.
There is a game monitoring interface in Field, which is called when the game is won (2048 cards are obtained), the game is over (can no longer walk), and the score changes. When the corresponding operation is performed, the corresponding value to improve or modify the score will pop up in the Activity.
To sum up, doing the 2048 game practice, learning how to write custom layouts and controls, and understanding the process of monitoring the distribution and acquisition of click events, is quite rewarding in general.


Hope someone can point out my shortcomings in the comments.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326319262&siteId=291194637