Android版2048

來源:互聯網
上載者:User

標籤:android   color   io   for   ar   art   cti   代碼   

雖然說2048是好久前比較火的小遊戲,但直到最近才有機會去研究下2048實現的源碼,這裡就簡單寫一下我(bie)的(ren)思路:

首先2048需要有十六個卡片,這個卡片可以用FrameLayout的子類來實現(繼承),裡面可以用TextView來實現設定背景顏色,顯示的數字等。這樣我們就可以簡單實現這個卡片啦:

public class Card extends FrameLayout {

    private int num = 0;
    private TextView label;

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

**********************************************

  其次,我們需要添加十六個卡片到螢幕上,應該怎樣實現這個螢幕呢?我們可以用GridLayout的子類(繼承)實現,並用一個二維數組來儲存下每個卡片的狀態:

public class GameView extends GridLayout {

    private Card cardsMap[][] = new Card[4][4];
    private List<Point> emptyPoint = new ArrayList();//記錄空的位置

***********************************************

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

    }

******************************************

然後我們就需要去判斷使用者的手勢。通過設定一個setOnTouchListener(new OnTouchListener())去監聽使用者的操作,當使用者按下螢幕時,記錄下此時的座標,當使用者手指離開螢幕時,計算座標的位移量,根據座標位移量的相對差來判斷使用者的意圖是向上,向下,還是向左,向右:

   public boolean onTouch(View arg0, MotionEvent arg1) {

switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = arg1.getX();
                    startY = arg1.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    offsetX = arg1.getX() - startX;
                    offsetY = arg1.getY() - startY;

                    if (Math.abs(offsetX) > Math.abs(offsetY)) {
                        if (offsetX < -3) {
                            slideLeft();
                        } else if (offsetX > 2) {
                            slideRight();
                        }
                    } else {
                        if (offsetY < -3) {
                            slideUp();
                        } else if (offsetY > 3) {
                            slideDown();
                        }
                    }
                    break;

                }
                return true;

}

**************************************

判斷出使用者的意圖後,我們就應該去處理相應的事件啦,這裡只貼上向左滑動的處理代碼,其他三個方向的處理相類似:

boolean flag = 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--;
                            flag = 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.getMainActivity().addMyScore(
                                    cardsMap[x][y].getNum());
                            flag = true;
                        }
                        break;
                    }
                }
            }
        }
        if (flag) {
            addRandom();//隨機地添加一個卡片(實際上設定相應位置上卡片的數字)
            checkComplete();//判斷遊戲是否結束

**************************************************

以上就是android版的2048實現的基本邏輯,可能有些不詳細。如果需要詳細的源碼,可以聯絡我哈。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.