码迷,mamicode.com
首页 > 移动开发 > 详细

android 模拟2048

时间:2015-01-03 17:09:59      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

利用节日休息时间在ANDROID上进行学习并模拟2048游戏。

效果如下图:

技术分享

技术分享

制作思路:

1、画出2048游戏主界面,根据手机屏幕宽高度进行计算并画出每个方块的大小。

 1 @Override
 2     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 3         super.onSizeChanged(w, h, oldw, oldh);
 4         int cW = (Math.min(w, h) - 10) / 4;
 5 
 6         addCards(cW, cW);
 7 
 8         startGame();
 9     }
10 
11     private void startGame() {
12         MainActivity aty = MainActivity.getMainActivity();
13         aty.clearScore();
14 
15         for (int y = 0; y < 4; y++) {
16             for (int x = 0; x < 4; x++) {
17                 cardsMap[x][y].setNum(0);
18             }
19         }
20         addRandomNum();
21         addRandomNum();
22     }

 

2、建立一个二维数组用于存储卡面上的值

3、建立一个list<point>用于存储每个卡片的位置

    private Card[][] cardsMap = new Card[4][4];
    private List<Point> emptyPoints = new ArrayList<Point>();

    private void addCards(int cardWidth, int cardHeight) {
        Card c;
        LinearLayout line;
        LinearLayout.LayoutParams lineLp;

        for (int y = 0; y < 4; y++) {
            line = new LinearLayout(getContext());
            lineLp = new LayoutParams(-1, cardHeight);
            addView(line, lineLp);

            for (int x = 0; x < 4; x++) {
                c = new Card(getContext());
                line.addView(c, cardWidth, cardHeight);

                cardsMap[x][y] = c;
            }

        }
    }

  

4、游戏开始时进行,添加随机卡片数据。

 

private 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));
                }
            }
        }
        if (emptyPoints.size() > 0) {
            Point p = emptyPoints.remove((int) (Math.random() * emptyPoints.size()));
            cardsMap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4);
        }
    }

 

5、利用android touch事件进行记录手指在屏幕滑动方向。

private void initGameView() {
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(0xffbbada0);

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

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: //当屏幕点击时进行记录点击的X和Y值
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        offsetX = event.getX() - startX;//进行计算点击偏移值
                        offsetY = event.getY() - startY;

                        if (Math.abs(offsetX) > Math.abs(offsetY)) {
                            if (offsetX < -5) {
                                //left
                                swipeLeft();
                            } else if (offsetX > 5) {
                                //right
                                swipeRight();
                            }
                        } else {
                            if (offsetY < -5) {
                                //up
                                swipeUp();
                            } else if (offsetY > 5) {
                                //down
                                swipeDown();
                            }
                        }
                        break;
                }
                return true;
            }
        });
    }

  

6、根据滑动的方向进行循环每个方向的卡片 进行卡片的位置移动及相同的卡片相加

    private 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.getMainActivity().addScore(cardsMap[x][y].getNum());
                            merge = true;
                        }

                        break;
                    }

                }
            }

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

  

 

android 模拟2048

标签:

原文地址:http://www.cnblogs.com/tom-zhu/p/4199688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!