Andorid遊戲2048開發(一)

來源:互聯網
上載者:User

最近有一款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(){            }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.