2048游戏实现的回顾

<span style="font-size:18px;">对于写2048中的一些总结:</span>
<span style="font-size:18px;">首先是主类:</span>
<span style="font-size:18px;">public class MainActivity extends Activity {
    public MainActivity() {
        mainAcitvity = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvscore = (TextView) findViewById(R.id.score);
    }

    private static MainActivity mainAcitvity = null;

    public static MainActivity getMainAcitvity() {
        return mainAcitvity;
    }

    private TextView tvscore;
    private int score = 0;

    public void clearScore() {
        score = 0;
        showScore();
    }

    public void showScore() {
        tvscore.setText(score + "");
    }

    public void addscore(int s)

    {
        score += s;
        showScore();
    }
}
</span>
主类实现了加载布局,两个TextView,和一个自定义GridView类

还有showScore和addScore 两个方法

package com.example.gg.mygame2048;

import android.content.Context;

import android.graphics.Point;
import android.util.AttributeSet;
import android.util.EventLog;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;

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


/**
 * Created by gg on 2016/10/11.
 */

public class GridView extends GridLayout {
    private Card[][] cardsMap = new Card[4][4];


    public GridView(Context context) {

        super(context);
        InitGame();
    }

    public GridView(Context context, AttributeSet attrs) {
        super(context, attrs);InitGame();
    }

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

    public void InitGame() {
        setColumnCount(4);
        setBackgroundColor(0xffE3CF57);

        setOnTouchListener(new View.OnTouchListener() {
            private float startX, startY, offsetX, offsetY;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = motionEvent.getX();
                        startY = motionEvent.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        offsetX = motionEvent.getX() - startX;
                        offsetY = motionEvent.getY() - startY;
                        if (Math.abs(offsetX) > Math.abs(offsetY)) {
                            if (offsetX < -5) {

                                swipeLeft();
                            } else if (offsetX > 5) {
                                swipeRight();
                            }
                        } else {
                            if (offsetY < -5) {
                                swipeUp();
                            } else if (offsetY > 5) {
                                swipeDown();
                            }
                        }
                }
                return true;
            }
        });

    }

    public void swipeLeft() {
        boolean merge = false;
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                for (int x1 = x + 1; x1 < 4; x1++) {
                    if (cardsMap[x1][y].getNum() > 0) {
                        if (cardsMap[x][y].getNum() <= 0) {
                            cardsMap[x][y].setNum(cardsMap[x1][y].getNum());
                            cardsMap[x1][y].setNum(0);
                            x--;
                            merge = true;
                        } else if (cardsMap[x][y].equals(cardsMap[x1][y])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x1][y].setNum(0);
                            MainActivity.getMainAcitvity().addscore(cardsMap[x][y].getNum());
                            merge = true;
                        }
                        break;
                    }
                }
            }
        }
        if (merge) {
            addRandomNum();
        }
    }

    public void swipeRight() {
        boolean merge = false;
        for (int y = 0; y < 4; y++) {
            for (int x = 3; x >= 0; x--) {

                for (int x1 = x + -1; x1 >= 0; x1--) {
                    if (cardsMap[x1][y].getNum() > 0) {
                        if (cardsMap[x][y].getNum() <= 0) {
                            cardsMap[x][y].setNum(cardsMap[x1][y].getNum());
                            cardsMap[x1][y].setNum(0);
                            x++;
                            merge = true;
                        } else if (cardsMap[x][y].equals(cardsMap[x1][y])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x][y].setNum(0);
                            MainActivity.getMainAcitvity().addscore(cardsMap[x][y].getNum());
                            merge = true;
                        }
                        break;
                    }
                }
            }
        }
        if (merge) {
            addRandomNum();
        }
    }

    public void swipeUp() {
        boolean merge = false;
        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {
                for (int y1 = y + 1; y1 < 4; y1++) {
                    if (cardsMap[x][y1].getNum() > 0) {

                        if (cardsMap[x][y].getNum() <= 0) {
                            cardsMap[x][y].setNum(cardsMap[x][y1].getNum());
                            cardsMap[x][y1].setNum(0);
                            y--;
                            merge = true;
                        } else if (cardsMap[x][y].equals(cardsMap[x][y1])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x][y].setNum(0);
                            MainActivity.getMainAcitvity().addscore(cardsMap[x][y].getNum());
                            merge = true;
                        }
                        break;
                    }
                }
            }
        }
        if (merge) {
            addRandomNum();
        }
    }

    public void swipeDown() {
        boolean merge = false;
        for (int x = 0; x < 4; x++) {
            for (int y = 3; y >= 0; y--) {
                for (int y1 = y - 1; y1 >= 0; y1--) {
                    if (cardsMap[x][y1].getNum() > 0) {

                        if (cardsMap[x][y].getNum() <= 0) {
                            cardsMap[x][y].setNum(cardsMap[x][y1].getNum());
                            cardsMap[x][y1].setNum(0);
                            y++;
                            merge = true;
                        } else if (cardsMap[x][y].equals(cardsMap[x][y1])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x][y].setNum(0);
                            MainActivity.getMainAcitvity().addscore(cardsMap[x][y].getNum());
                            merge = true;
                        }
                        break;
                    }

                }
            }
        }
        if (merge) {
            addRandomNum();
        }
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int cardWidth = (Math.min(w, h)) / 4;

        addCards(cardWidth, cardWidth);
        startGame();
    }

    public void addRandomNum() {
        emptyPoints.clear();
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                if (cardsMap[x][y].getNum() <= 0) {
                    emptyPoints.add(new Point(x,y));
                }
            }
        }
        Point p = emptyPoints.remove((int)( Math.random() * emptyPoints.size()));
        cardsMap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4);
    }

    private void startGame() {
        MainActivity.getMainAcitvity().clearScore();
        for (int y = 0; y < 0; y++) {
            for (int x = 0; x < 4; x++) {
                cardsMap[x][y].setNum(0);
            }
        }
        addRandomNum();
        addRandomNum();

    }

    private void addCards(int cardWidth, int cardHeight) {
        Card c;
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                c = new Card(getContext());
                c.setNum(0);
                addView(c, cardWidth, cardHeight);
                cardsMap[x][y] = c;
            }
        }

    }

  private List<Point> emptyPoints = new ArrayList<Point>();

}
以上是GridView的实现继承了一个GridLayout

先去定义了游戏的背景

然后:

public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {


}

}

在其中实现了onTouch方法

通过判断Action_up,Action_down

startX = motionEvent.getX();
startY = motionEvent.getY();
以及
offsetX = motionEvent.getX() - startX;
offsetY = motionEvent.getY() - startY;
分别调用四个方法
public void swipeLeft() {
    boolean merge = false;
    for (int y = 0; y < 4; y++) {
        for (int x = 0; x < 4; x++) {
            for (int x1 = x + 1; x1 < 4; x1++) {
                if (cardsMap[x1][y].getNum() > 0) {
                    if (cardsMap[x][y].getNum() <= 0) {
                        cardsMap[x][y].setNum(cardsMap[x1][y].getNum());
                        cardsMap[x1][y].setNum(0);
                        x--;
                        merge = true;
                    } else if (cardsMap[x][y].equals(cardsMap[x1][y])) {
                        cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                        cardsMap[x1][y].setNum(0);
                        MainActivity.getMainAcitvity().addscore(cardsMap[x][y].getNum());
                        merge = true;
                    }
                    break;
                }
            }
        }
    }
    if (merge) {
        addRandomNum();
    }
}
在这个方法中,开始merge为false,每一次的滑动都会使得merge为true;
他先进行了y=0,在X轴方法上的判断,从0——3,X1=x+1,即X1比x始终大;向左滑动进行了判断,如果相等就消除,如果不相等,就向左移动。
之后增加一个随机数
 
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    int cardWidth = (Math.min(w, h)) / 4;

    addCards(cardWidth, cardWidth);
    startGame();
}
进行了图形的适配
然后开始游戏startGame()
清除内部的数字
package com.example.gg.mygame2048;

import android.content.Context;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;


/**
 * Created by gg on 2016/10/11.
 */

public class Card extends FrameLayout {
    public Card(Context context) {
        super(context);
        label=new TextView(getContext());
        label.setTextSize(32);
        label.setGravity(Gravity.CENTER);
        label.setBackgroundColor(0x33ffffff);
        LayoutParams lp=new LayoutParams(-1,-1);
        lp.setMargins(10,10,0,0);
        addView(label,lp);
        setNum(0);
    }
    public int num=0;
    private TextView label;
    public int getNum()
    {
        return num;
    }

    public void setNum(int num)
    {
        this.num=num;
        if(num<=0) {
            label.setText("");
        }
        else {
            label.setText(num + "");
        }
    }

    public boolean equals(Card o)
    {
        return getNum()==o.getNum();
    }

}
初始化card
以上代码为极客学院学习得来
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值