android用TextView写一个2048小游戏

目前已经上传到gitee上,欢迎下载: 简易2048

android用Textview制作游戏

目录结构

在这里插入图片描述

一、主要游戏界面

在这里插入图片描述

二、主要算法部分

利用手势,进行上下左右滑动

//设置手势
[video(video-jEZmYcIm-1603521845201)(type-undefined)(url-undefined)(image-https://img-blog.csdnimg.cn/editor-video.png)(title-undefined)]
[video(video-i781Ca30-1603521954364)(type-bilibili)(url-https://player.bilibili.com/player.html?aid=542606917)(image-https://ss.csdn.net/p?http://i1.hdslb.com/bfs/archive/cec4f0138ef085c58514c22cf22cfa488d8bc7fb.png)(title-android用Textview制作游戏)]


    private void initGesture() {
    
    
        gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
    
    


            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    
    
                SoundPlayUtils.play(2);
                if (e1.getX() - e2.getX() > time) {
    
    
                    for (int i = 0; i < 4; i++) {
    
    
                        for (int j = 0; j < 4; j++) {
    
    
                            if (isOver[j][i]) {
    
    
                                setLeft(j, i);
                            }
                        }
                    }
                    setNum();
                    return true;
                }
                if (e2.getX() - e1.getX() > time) {
    
    
                    for (int i = 3; i >= 0; i--) {
    
    
                        for (int j = 3; j >= 0; j--) {
    
    
                            if (isOver[j][i]) {
    
    
                                setRight(j, i);
                            }
                        }
                    }
                    setNum();
                    return true;
                }
                if (e1.getY() - e2.getY() > time) {
    
    
                    for (int i = 0; i < 4; i++) {
    
    
                        for (int j = 0; j < 4; j++) {
    
    
                            if (isOver[i][j]) {
    
    

                                setUp(i, j);

                            }
                        }
                    }
                    setNum();
                    return true;
                }
                if (e2.getY() - e1.getY() > time) {
    
    
                    for (int i = 3; i >= 0; i--) {
    
    
                        for (int j = 0; j < 4; j++) {
    
    
                            if (isOver[i][j]) {
    
    

                                setDonw(i, j);

                            }
                        }
                    }


                    setNum();
                    return true;
                }
                return false;
            }
        });
    }
//左滑
    private void setLeft(int i, int j) {
    
    
        sign1:
        for (int h = 0; h < 4; h++) {
    
    
            if (i == h && j != 0) {
    
    
                for (int w = j; w > 0; w--) {
    
    
                    TextView ahead = findViewById(name[h][w - 1]);/*前面的TextView*/
                    TextView local = findViewById(name[h][w]);/*后面的TextView*/


                    if (ahead.getText().toString() != "" && ahead.getText().toString().equals(local.getText().toString())) {
    
    
                        SoundPlayUtils.play(1);
                        int num = parse(ahead.getText().toString());
                        ahead.setText(num + num + "");

                        ahead.startAnimation(compot);
                        local.setText("");
                        local.setBackgroundResource(R.drawable.text_bg);
                        isOver[h][w - 1] = true;
                        isOver[h][w] = false;
                        ChangStyle(ahead, local);
                        setScore(ahead);
                        break sign1;

                    }

                    if (ahead.getText().toString() == "") {
    
    
                        isOver[h][w - 1] = true;
                        isOver[h][w] = false;

                        ahead.setText(local.getText().toString() + "");
                        local.setText("");
                        local.setBackgroundResource(R.drawable.text_bg);
                        ChangStyle(ahead, local);

                    }
                }
            }


        }

    }
//右滑
    private void setRight(int i, int j) {
    
    
        sign2:
        for (int h = 0; h < 4; h++) {
    
    
            if (i == h && j != 3) {
    
    
                for (int w = j; w < 3; w++) {
    
    
                    TextView ahead = findViewById(name[h][w + 1]);
                    TextView local = findViewById(name[h][w]);


                    if (ahead.getText().toString() != "" && ahead.getText().toString().equals(local.getText().toString())) {
    
    
                        SoundPlayUtils.play(1);
                        int num = parse(ahead.getText().toString());
                        ahead.setText(num + num + "");
                        local.setText("");
                        ahead.startAnimation(compot);
                        local.setBackgroundResource(R.drawable.text_bg);
                        isOver[h][w + 1] = true;
                        isOver[h][w] = false;
                        ChangStyle(ahead, local);
                        setScore(ahead);
                        break sign2;

                    }

                    if (ahead.getText().toString() == "") {
    
    

                        isOver[h][w + 1] = true;
                        isOver[h][w] = false;

                        ahead.setText(local.getText().toString() + "");
                        local.setText("");
                        local.setBackgroundResource(R.drawable.text_bg);

                        ChangStyle(ahead, local);

                    }
                }
            }


        }
    }

//上滑
    private void setUp(int i, int j) {
    
    
        sign3:
        for (int h = 0; h < 4; h++) {
    
    
            for (int w = 0; w < 4; w++) {
    
    
                if (w == j && i != 0) {
    
    
                    TextView ahead = findViewById(name[i - 1][j]);
                    TextView local = findViewById(name[i][j]);

                    if (!TextUtils.isEmpty(ahead.getText()) && ahead.getText().toString().equals(local.getText().toString())) {
    
    
                        SoundPlayUtils.play(1);
                        int num = parse(ahead.getText().toString());
                        ahead.setText(num + num + "");
                        local.setText("");
                        ahead.startAnimation(compot);
                        local.setBackgroundResource(R.drawable.text_bg);
                        isOver[i - 1][j] = true;
                        isOver[i][j] = false;
                        ChangStyle(ahead, local);
                        setScore(ahead);
                        break sign3;
                    }

                    if (TextUtils.isEmpty(ahead.getText())) {
    
    

                        isOver[i - 1][j] = true;
                        isOver[i][j] = false;

                        ahead.setText(local.getText().toString() + "");
                        local.setText("");
                        local.setBackgroundResource(R.drawable.text_bg);
                        ChangStyle(ahead, local);

                    }
                    i--;
                }

            }

        }
    }
//下滑
    private void setDonw(int i, int j) {
    
    
        sign4:
        for (int h = 0; h < 4; h++) {
    
    
            for (int w = 0; w < 4; w++) {
    
    
                if (w == j && i < 3) {
    
    
                    TextView ahead = findViewById(name[i + 1][j]);
                    TextView local = findViewById(name[i][j]);

                    if (!TextUtils.isEmpty(ahead.getText()) && ahead.getText().toString().equals(local.getText().toString())) {
    
    
                        SoundPlayUtils.play(1);
                        int num = parse(ahead.getText().toString());
                        ahead.setText(num + num + "");
                        local.setText("");
                        ahead.startAnimation(compot);
                        local.setBackgroundResource(R.drawable.text_bg);
                        isOver[i + 1][j] = true;
                        isOver[i][j] = false;
                        ChangStyle(ahead, local);
                        setScore(ahead);
                        break sign4;

                    }

                    if (TextUtils.isEmpty(ahead.getText())) {
    
    

                        isOver[i + 1][j] = true;
                        isOver[i][j] = false;

                        ahead.setText(local.getText().toString() + "");
                        local.setText("");
                        local.setBackgroundResource(R.drawable.text_bg);
                        ChangStyle(ahead, local);

                    }
                    i++;
                }

            }

        }

    }

猜你喜欢

转载自blog.csdn.net/Android_Cob/article/details/109258744