2048小游戏部分代码,仅供参考哦

这篇博客分享了2048小游戏的部分代码,包括GameView.java、activity_main.xml和Box.java三个关键文件,为安卓游戏开发爱好者提供参考。

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

2048小游戏部分代码

  • GameView.java
package com.example.d.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by duboxuan on 2017/7/7.
 */

public class GameView extends GridLayout {

    Box[][] boxarr = new Box[4][4];

    List<Point> point = new ArrayList<>();

    public GameView(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        Box b;
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                b = new Box(getContext());
                addView(b);
                boxarr[x][y] = b;
            }
        }

        random();
        random();
        random();
        random();
        random();
        random();


        this.setOnTouchListener(new OnTouchListener() {
            float x, y, offsetX, offsetY;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x = motionEvent.getX();
                        y = motionEvent.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        offsetX = motionEvent.getX() - x;
                        offsetY = motionEvent.getY() - y;
                        if (Math.abs(offsetX) > Math.abs(offsetY)) {
                            //水平
                            if (offsetX < 0) {

                                left();
                            } else if (offsetX > 0) {
                                right();
                            }
                        } else {
                            //垂直
                            if (offsetY < 0) {
                                top();
                            } else if (offsetY > 0) {
                                botton();
                            }
                        }
                        break;
                }

                return true;
            }
        });
    }
//生成随机数的方法
    private void random() {

        point.clear();

        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                if (boxarr[x][y].getNumber() <= 0) {
                    point.add(new Point(x, y));
                }
            }
        }

        Point p = point.remove((int) (Math.random() * point.size()));
        boxarr[p.x][p.y].setNumber(Math.random() > 0.2 ? 2 : 4);


    }

    private void left() {

        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                for (int x1 = x + 1; x1 < 4; x1++) {
                    if (boxarr[x1][y].getNumber() > 0) {

                        if (boxarr[x][y].getNumber() <= 0) {
                            boxarr[x][y].setNumber(boxarr[x1][y].getNumber());
                            boxarr[x1][y].setNumber(0);
                            x--;
                            break;
                        } else if (boxarr[x][y].equals(boxarr[x1][y])) {
                            boxarr[x][y].setNumber(boxarr[x][y].getNumber() *
                                    2);
                            boxarr[x1][y].setNumber(0);
                            break;

                        }
                    }
                }
            }
        }

    }

    private void top() {

    }

    private void right() {

    }

    private void botton() {

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
  android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2048"
        />

    <com.example.d.myapplication.GameView
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:columnCount="4"
        android:rowCount="4">

    </com.example.d.myapplication.GameView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

</LinearLayout>

Box.java

package com.example.d.myapplication;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/7/7.
 */

public class Box extends FrameLayout {
    private int number = 0;
    private TextView box;
    public void setNumber(int number) {
        this.number = number;
        if(number==0){
            box.setText("");
        }else{
            box.setText(""+number);
        }
    }
    public int getNumber() {
        return number;
    }
    public boolean equals(Box b){
        return b.getNumber()==getNumber();
    }
    //
    public Box(Context context){
        super(context);
        box = new TextView(getContext());
        setNumber(0);
        LayoutParams lp = new LayoutParams(-1,-1);
       lp.setMargins(10,10,0,0);
        box.setGravity(Gravity.CENTER);
        box.setTextSize(50);
        addView(box,lp);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值