Android 小游戏 2048

昨晚看到android的编程小游戏视频,一时兴起,就看了下,觉得介绍的好简单,觉得自己也可以试试,然后就尝试嘛,也是一个不错的体验。
编程就是拿来玩的,娱乐精神,服务精神,这就是coder的使命。

activity_main.xml
布局文件:GameView就是自定义的布局控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.game2048.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="srore"
        android:id="@+id/textView"/>

    <com.game2048.GameView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gameview"
        android:focusableInTouchMode="true">
    </com.game2048.GameView>
</RelativeLayout>

GameView.java

package com.game2048;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
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 zhouchenglin on 2016/4/27.
 */
public class GameView extends GridLayout {
    public GameView(Context context) {
        super(context);
        InitGameView();
    }

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

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

    private void InitGameView() {
        //设置颜色和字体
        setColumnCount(4);
        setBackgroundColor(0xffbbada0);


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

            //返回值flase只能响应keydown事件,每次触发都会重新执行函数
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        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) {
                                swipeLeft();
                            } else if (offsetX > 5) {
                                swipeRight();
                            }
                        } else {
                            if (offsetY < -5) {
                                swipeUp();
                            } else if (offsetY > 5) {
                                swipeDown();
                            }
                        }
                        break;
                }
                return true;
            }
        });
    }

    private Card[][] cardsMap = new Card[4][4];

    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 boolean finishGame(){
        for(int i=0;i<4;i++){
            for (int y=0;y<4;y++){
                if(cardsMap[i][y].getNum()<=0){
                    return false;
                }
            }
        }
//        Toast.makeText(getContext(),"已经完成游戏",Toast.LENGTH_SHORT).show();
        return true;
    }
    public 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(3);
                addView(c, cardWidth, cardHeight);
                cardsMap[x][y] = c;
            }
        }
    }

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

    // 添加随机数
    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));
                }
            }
        }
        Point p = emptyPoints.remove((int) (Math.random() * emptyPoints.size()));
        cardsMap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4);
    }

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

    private void swipeLeft() {
        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);
                            break;
                        } else if (cardsMap[x][y].equals(cardsMap[x1][y])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x1][y].setNum(0);
//                            addRandomNum();
                            break;
                        }
                    }
                }
            }
        }
        if(finishGame())
            return;
        addRandomNum();
    }

    private void swipeRight() {
        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);
                            break;
                        } else if (cardsMap[x][y].equals(cardsMap[x1][y])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x1][y].setNum(0);
//                            addRandomNum();
                            break;
                        }
                    }
                }
            }
        }
        if(finishGame())
            return;
        addRandomNum();
    }

    private void swipeUp() {
        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);
                            break;
                        } else if (cardsMap[x][y].equals(cardsMap[x][y1])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x][y1].setNum(0);
//                          addRandomNum();
                            break;
                        }
                    }
                }
            }
        }
        if(finishGame())
            return;
        addRandomNum();

    }

    private void swipeDown() {
        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);
                            break;
                        } else if (cardsMap[x][y].equals(cardsMap[x][y1])) {
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
                            cardsMap[x][y1].setNum(0);
//                            addRandomNum();
                            break;
                        }
                    }
                }
            }
        }
        if(finishGame())
            return;
        addRandomNum();
    }

}

这里面主要是逻辑实现过程,代码拿来玩玩,主要这里面涉及得知识点:
判断滑动手势,逻辑结构,目前可能还有点问题,细节方面没有考虑很多,主要是玩玩。

Card.java
这个相当于将每个方格当成卡牌,在里面展示要显示的数据

package com.game2048;

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

/**
 * Created by zhouchenglin on 2016/4/27.
 */
public class Card extends FrameLayout {
    public Card(Context context) {
        super(context);
        initCard();
    }

    public Card(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCard();
    }

    public Card(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCard();
    }

    //显示数字
    private int num = 0;
    //展示的文本框
    private TextView label;

    public int getNum() {
        return this.num;
    }

    public void setNum(int num) {
        this.num = num;
        switch (num)
        {
            case 0:
                label.setText("");
                break;
            case 2:
                label.setTextColor(0xFFFFD700);
                label.setText(String.valueOf(num));
                break;
            case 4:
                label.setTextColor(0xffFF6347);
                label.setText(String.valueOf(num));
                break;
            case 8:
                label.setTextColor(0xffEED8AE);
                label.setText(String.valueOf(num));
                break;
            case 16:
                label.setTextColor(0xffEEC591);
                label.setText(String.valueOf(num));
                break;
            case 32:
                label.setTextColor(0xffEE9A00);
                label.setText(String.valueOf(num));
                break;
            case 64:
                label.setTextColor(0xffEE9A49);
                label.setText(String.valueOf(num));
                break;
            case 128:
                label.setTextColor(0xffEE7600);
                label.setText(String.valueOf(num));
                break;
            case 256:
                label.setTextColor(0xffEEEE00);
                label.setText(String.valueOf(num));
                break;
            case 1024:
                label.setTextColor(0xffEE2C2C);
                label.setText(String.valueOf(num));
                break;
            case 2048:
                label.setTextColor(0xffEE3B3B);
                label.setText(String.valueOf(num));
                break;
            case 4096:
                label.setTextColor(0xffDC143C);
                label.setText(String.valueOf(num));
                break;
            default:
                label.setText(String.valueOf(num));
                break;
        }
    }

    private void initCard() {
        label = new TextView(getContext());
        label.setTextSize(32);
        label.setGravity(Gravity.CENTER);
        label.setBackgroundColor(0x33ffffff);

        //显示完全在布局里面;添加布局
        LayoutParams lp = new LayoutParams(-1, -1);
        lp.setMargins(10,10,10,10);
        addView(label, lp);

        setNum(2);

    }

    //判断两张卡片是否相等
    public boolean equals(Card o) {
        return getNum() == o.getNum();
    }
}

MainActivity.java,基本没改

package com.game2048;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

这个程序主要介绍的就是将自动添加控件到布局里面,而不是通过xml文件直接布局。

可能还需要逻辑上面需要在细化下,另外可能还需要设计下Ui的更新问题和游戏结束判断问题,这个我觉得可以开个子线程来做对应的处理。

这里写图片描述

其实有时候,编程就是这么有趣。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值