Andorid游戏2048开发(一)

时间:2023-03-09 03:44:59
Andorid游戏2048开发(一)

最近有一款Android平台下的游戏很是火爆----2048。下面记录一下开发过程。由于笔者是Android开发的初学者,所以希望借以此文熟悉整个Android开发的流程。

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

<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文件中绑定我们的主方法

    <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(){ } }