Andorid游戏2048开发(一)

最近有一款Android平台下的游戏非常火爆----2048。下面记录一下开发过程。因为笔者是Android开发的初学者,因此但愿借以此文熟悉整个Android开发的流程。java

首先建立Game2048的游戏项目。咱们选择最低平台为Android4.0(API 14),最高支持平台Android4.4(API 19),而后一路Next,建立完成以后,咱们修改activity_main.xml文件。修改默认的布局方式为LinearLayout布局方式。而后咱们在嵌套一个Linearyout布局,用户游戏分数的显示。android

 

<LinearLayout 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:orientation="vertical"
    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=".MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/score" />

        <TextView
            android:id="@+id/tvScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/gameView"
        ></GridLayout>
</LinearLayout>

而后咱们在主包中建立游戏主界面GameView类,因为咱们使用GridLayout布局方式用来主界面的显示,因此GameView类咱们继承GridLayout,而且建立所有的三个构造方法。而后在GameView类中,建立initGameView()做为游戏的起始方法。而后找到主方法的路径com.skk.game2048.GameView,在acitvity_main.xml文件中绑定咱们的主方法eclipse

    <com.skk.game2048.GameView 
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/gameView"
        ></com.skk.game2048.GameView>


主方法代码布局

package com.skk.game2048;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridLayout;

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 defStyle) {
        super(context, attrs, defStyle);
        initGameView();
    }
    
    private void initGameView(){
        
    }

}