android用TextView写一个2048小游戏

本文档介绍了一个简单的Android 2048游戏的实现,通过Textview构建游戏界面,并利用手势识别进行上下左右滑动操作。核心算法包括左滑、右滑、上滑和下滑的逻辑处理,实现了数字的合并和移动。同时,文中还提供了源代码链接,供读者下载学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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++;
                }

            }

        }

    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这次最后一次熬夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值