一、游戏界面效果显示
二、布局Activity.xml代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="得分"
android:textSize="20sp" />
<TextView
android:id="@+id/tvSorce"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="145dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最高分"
android:textSize="20sp" />
<TextView
android:id="@+id/maxSorce"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#FFCC99"
android:text="宇琴的2048"
android:textSize="30sp"
android:gravity="center"/>
<com.example.hp.yq2048.GameView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/gameView" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_cx"
android:text="重来"
android:background="#FFFFCC"/>
三、GramView.java代码
public class GameView extends GridLayout {
//我们需要定义一个二维数组来记录GameView初始化时生成的16个卡片类
private Card[][] cardsMap = new Card[4][4];
private static GameView gameView = null;
public static GameView getGameView() {
return gameView;
}
private List<Point> points = new ArrayList<Point>();
public GameView(Context context) {
super(context);
gameView = this;
initGameView();
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
gameView = this;
initGameView();
}
public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
gameView = this;
initGameView();
}
/**
* 初始化界面
*/
private void initGameView(){
Log.d("233","0");
setColumnCount(4); //指名是4列的
setBackgroundColor(0xffbbada0);
addCards(getCardWitch(),getCardWitch());
startGame();
setOnTouchListener(new OnTouchListener() {
private float startX,startY;//初始的位置
private float 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;
i