1. 程式人生 > >android遊戲開發入門: 貪吃蛇 原始碼分析

android遊戲開發入門: 貪吃蛇 原始碼分析

貪吃蛇是一款足夠經典的遊戲。它的經典,在於使用者操作的簡單,在於技術實現的簡介,在於他的經久不衰。

這裡的貪吃蛇的android實現,是SDK Samples中的開源例程。可能各位都有看過~介面如下圖啦~


作為一個剛入門或者還沒入門的新手,著實花了我一些力氣來理解這段程式碼。

對於各種不懂的地方,慢慢查詢資料,對於新的方法,通過修改程式碼嘗試效果。到現在終於能算個一知半解。

在程式碼中,對於自己有所收穫的地方,我都做了相應的註釋。

回過頭來,覺得從這段程式碼中,能學到不少東西~~

包括android應用的基本架構,他的面向物件的思想,以及程式碼的簡潔明瞭。

於是,我想到,何不將這些東西分享出來,如果碰巧對感興趣的朋友們有搜幫助,那就更好了~

好了,閒話不說~程式碼和註釋如下(處於對原始碼的敬意,原本的英文註釋部分都沒有刪去~大家可以配合理解):

PS:最近我正在寫自己的“貪吃蛇”,說事貪吃蛇,其實完全顛覆了這個經典版本的設計理念和操作方式。具體細節先賣一個關子,作品準備參加這次第二屆大學生android應用開發大賽。

應該一個月內能完成,到時候也會開源出程式碼來~歡迎大家討論指正·~

************************************************************************************************************************************

Snake工程中,總共有三個檔案: *TileView是基於Android的View類實現的方塊圖類,用來支撐上層類的呼叫,繪製方塊圖的顯示介面。通過這些程式碼,能打之瞭解如何 擴充套件View,實現特色的介面效果。 *SnakeView呼叫了TileView,實現了遊戲邏輯 和 具體的顯示。 *Snake為主Activity類。

建議大家按照上面的順序看三個檔案,可能邏輯上更舒服一點~~

下面貼上程式碼和註釋。

PS:  除錯版本為android2.2。 其他版本應該也沒問題吧,不過得用虛擬機器。因為它是上下左右按鍵操作,現在大多數android機是沒有方向鍵的吧。

TileView.java

package com.example.android.snake;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;


/**
 * TileView: a View-variant designed for handling arrays of "icons" or other
 * drawables.
 * 
 */

public class TileView extends View {

    /**
     * Parameters controlling the size of the tiles and their range within view.
     * Width/Height are in pixels, and Drawables will be scaled to fit to these
     * dimensions. X/Y Tile Counts are the number of tiles that will be drawn.
     */

    protected static int mTileSize; //每個tile的邊長的畫素數量

    protected static int mXTileCount; //螢幕內能容納的 X方向上方塊的總數量
    protected static int mYTileCount;//螢幕內能容納的 Y方向上方塊的總數量

    private static int mXOffset; //原點座標,按pixel計。
    private static int mYOffset;


    /**
     * A hash that maps integer handles specified by the subclasser to the
     * drawable that will be used for that reference
     * 儲存著不同種類的bitmap圖。通過resetTiles,loadTile,將遊戲中的方塊載入到這個陣列。
     * 可以理解為 磚塊字典
     */
    private Bitmap[] mTileArray;    

    /**
     * A two-dimensional array of integers in which the number represents the
     * index of the tile that should be drawn at that locations
     * 儲存整個介面內每個tile位置應該繪製的tile。
     * 可看作是我們直接操作的畫布。
     * 通過setTile、clearTile 進行圖形顯示的修改操作。 
     * 
     */
    private int[][] mTileGrid; 

    //畫筆,canvas的圖形繪製,需要畫筆Paint實現。
    private final Paint mPaint = new Paint();

    
    public TileView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //使用TypedArray,獲取在attrs.xml中為TileView定義的新屬性tileSize 。參考: http://weizhulin.blog.51cto.com/1556324/311453
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
        mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
        a.recycle();
    }

    public TileView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
        mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
        a.recycle();
    }

    
    
    /**
     * Rests the internal array of Bitmaps used for drawing tiles, and
     * sets the maximum index of tiles to be inserted
     * 重置清零mTileArray,在遊戲初始的時候使用。
     * 即清空磚塊字典
     * @param tilecount
     */
    public void resetTiles(int tilecount) {
    	mTileArray = new Bitmap[tilecount];
    }

    
    /*
     * 當改變螢幕大小尺寸時,同時修改tile的相關計數指標。
     */
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mXTileCount = (int) Math.floor(w / mTileSize);
        mYTileCount = (int) Math.floor(h / mTileSize);

        //mXOffset mYOffset是繪圖的起點座標。
        mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
        mYOffset = ((h - (mTileSize * mYTileCount)) / 2);

        mTileGrid = new int[mXTileCount][mYTileCount];
        clearTiles();
    }

    
    /**
     * Function to set the specified Drawable as the tile for a particular
     * integer key.
     * 載入具體的磚塊圖片 到 磚塊字典。
     * 即將對應的磚塊的圖片 對應的載入到 mTileArray陣列中
     * @param key
     * @param tile
     */
    public void loadTile(int key, Drawable tile) {
        //這裡做了一個 Drawable 到 bitmap 的轉換。由於外部程式使用的時候是直接讀取資原始檔中的圖片,
    	//是drawable格式,而我們的陣列是bitmap格式,方便最終的繪製。所以,需要進行一次到 bitmap的轉換。
    	Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        tile.setBounds(0, 0, mTileSize, mTileSize);
        tile.draw(canvas);
        
        mTileArray[key] = bitmap;
    }

    /**
     * Used to indicate that a particular tile (set with loadTile and referenced
     * by an integer) should be drawn at the given x/y coordinates during the
     * next invalidate/draw cycle.
     * 在相應的座標位置繪製相應的磚塊
     * 記得哦,mTileGrid其實就是我們直接操作的畫布。
     * @param tileindex
     * @param x
     * @param y
     */
    public void setTile(int tileindex, int x, int y) {
        mTileGrid[x][y] = tileindex;
    }

    /**
     * Resets all tiles to 0 (empty)
     * 清空圖形顯示。
     * 用以更新畫面。
     * 呼叫了繪圖的setTile()。
     */
    public void clearTiles() {
        for (int x = 0; x < mXTileCount; x++) {
            for (int y = 0; y < mYTileCount; y++) {
                setTile(0, x, y);
            }
        }
    }

/*
 * 將我們直接操作的畫布繪製到手機介面上!
 * @see android.view.View#onDraw(android.graphics.Canvas)
 */
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int x = 0; x < mXTileCount; x += 1) {
            for (int y = 0; y < mYTileCount; y += 1) {
                if (mTileGrid[x][y] > 0) {
                    canvas.drawBitmap(mTileArray[mTileGrid[x][y]], 
                    		mXOffset + x * mTileSize,
                    		mYOffset + y * mTileSize,
                    		mPaint);
                }
            }
        }
    }

}


SnakeView.java
package com.example.android.snake;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

/**
 * SnakeView: implementation of a simple game of Snake
 */
public class SnakeView extends TileView {

    private static final String TAG = "SnakeView";

    /**
     * Current mode of application: READY to run, RUNNING, or you have already
     * lost. static final ints are used instead of an enum for performance
     * reasons.
     * 遊戲的四種狀態。初始時為 預備開始的狀態。
     */
    private int mMode = READY;    
    public static final int PAUSE = 0;	//暫停
    public static final int READY = 1;	//準備好了,預備開始
    public static final int RUNNING = 2;//正在執行
    public static final int LOSE = 3;	//結束,輸了遊戲

    /**
     * Current direction the snake is headed.
     * 蛇體運動的方向標識。
     */
    private int mDirection = NORTH;
    private int mNextDirection = NORTH;
    private static final int NORTH = 1;
    private static final int SOUTH = 2;
    private static final int EAST = 3;
    private static final int WEST = 4;

    /**
     * Labels for the drawables that will be loaded into the TileView class
     * 遊戲中僅有的三種磚塊對應的數值。
     */	
    private static final int RED_STAR = 1;
    private static final int YELLOW_STAR = 2;
    private static final int GREEN_STAR = 3;

    /**
     * mScore: used to track the number of apples captured mMoveDelay: number of
     * milliseconds between snake movements. This will decrease as apples are
     * captured.
     */
    private long mScore = 0;   //記錄獲得的分數。
    private long mMoveDelay = 600;  //每移動一步的延時。初始時設定為600ms,以後每吃一個果子,打個9折
               					//造成的結果是速度越來越快。
    
    /**
     * mLastMove: tracks the absolute time when the snake last moved, and is used
     * to determine if a move should be made based on mMoveDelay.
     * 記錄上次移動的確切時間。
     * 同mMoveDelay一起處理與使用者的非同步操作的協同問題。
     */
    private long mLastMove;
    
    
    /**
     * mStatusText: text shows to the user in some run states
     * 用來顯示遊戲狀態的TextView
     */
    private TextView mStatusText;

    /**
     * mSnakeTrail: a list of Coordinates that make up the snake's body
     * mAppleList: the secret location of the juicy apples the snake craves.
     * 兩個連結串列,分別用來儲存 蛇體 和 果子的座標。
     * 每次蛇體的運動,蛇體的增長,產生新的蘋果,被吃掉蘋果,都會在這裡記錄。
     */
    private ArrayList<Coordinate> mSnakeTrail = new ArrayList<Coordinate>();
    private ArrayList<Coordinate> mAppleList = new ArrayList<Coordinate>();

    /**
     * Everyone needs a little randomness in their life
     * 隨機數生成器。用來產生隨機的蘋果。在addRandomApple()中使用。
     */
    private static final Random RNG = new Random();

    /**
     * Create a simple handler that we can use to cause animation to happen.  We
     * set ourselves as a target and we can use the sleep()
     * function to cause an update/invalidate to occur at a later date.
     * 用Handler機制實現定時重新整理。
     * 為什麼使用Handler呢?大家可以參考 android 的執行緒模型(注意UI執行緒不是執行緒安全的~)
     * 具體使用方法網上的資源很多,在此不贅述~
     */
    private RefreshHandler mRedrawHandler = new RefreshHandler();

    class RefreshHandler extends Handler {

    	//獲取訊息並處理
        @Override
        public void handleMessage(Message msg) {
            SnakeView.this.update();
            SnakeView.this.invalidate(); //重新整理view為基類的介面
        }

        //定時傳送訊息給UI執行緒,以此達到更新的效果。
        public void sleep(long delayMillis) {
        	this.removeMessages(0); //清空訊息佇列,Handler進入對新訊息的等待
            sendMessageDelayed(obtainMessage(0), delayMillis); //定時傳送新訊息,啟用handler
        }
    };


    
    public SnakeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSnakeView();  //建構函式中,別忘了,初始化遊戲~
   }

    public SnakeView(Context context, AttributeSet attrs, int defStyle) {
    	super(context, attrs, defStyle);
    	initSnakeView();
    }

    //初始化SnakeView類,注意,這根初始化遊戲是不一樣的。
    private void initSnakeView() {
        setFocusable(true); //設定焦點,由於存在 文字介面 和 遊戲介面的跳轉。這個focus是不可或缺的。

        //取得資源中的圖片,載入到 磚塊字典 中。
        Resources r = this.getContext().getResources();
        resetTiles(4);
        loadTile(RED_STAR, r.getDrawable(R.drawable.redstar));
        loadTile(YELLOW_STAR, r.getDrawable(R.drawable.yellowstar));
        loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar));
    	
    }
    
    //如果不是從暫停中回覆,就緒要 初始化遊戲了。
    private void initNewGame() {
        //清空儲存蛇體和果子的資料結構。
    	mSnakeTrail.clear();
        mAppleList.clear();

        // For now we're just going to load up a short default eastbound snake
        // that's just turned north
        // 設定初始狀態的蛇體的位置。 
        
        mSnakeTrail.add(new Coordinate(7, 7));
        mSnakeTrail.add(new Coordinate(6, 7));
        mSnakeTrail.add(new Coordinate(5, 7));
        mSnakeTrail.add(new Coordinate(4, 7));
        mSnakeTrail.add(new Coordinate(3, 7));
        mSnakeTrail.add(new Coordinate(2, 7));
        mNextDirection = NORTH;

        // Two apples to start with
        addRandomApple();
        addRandomApple();

        mMoveDelay = 600;
        mScore = 0;
    }


    /**
     * Given a ArrayList of coordinates, we need to flatten them into an array of
     * ints before we can stuff them into a map for flattening and storage.
     * 
     * @param cvec : a ArrayList of Coordinate objects
     * @return : a simple array containing the x/y values of the coordinates
     * as [x1,y1,x2,y2,x3,y3...】
     * 在遊戲暫停時,需要通過Bundle方式儲存資料。見saveState()。
     * Bundle支援簡單的陣列。
     * 所以需要將我們的部分資料結構,如蛇體和蘋果位置的陣列,轉換成簡單的序列化的int陣列。
     */
    private int[] coordArrayListToArray(ArrayList<Coordinate> cvec) {
        int count = cvec.size();
        int[] rawArray = new int[count * 2];
        for (int index = 0; index < count; index++) {
            Coordinate c = cvec.get(index);
            rawArray[2 * index] = c.x;
            rawArray[2 * index + 1] = c.y;
        }
        return rawArray;
    }

    /**
     * Save game state so that the user does not lose anything
     * if the game process is killed while we are in the 
     * background.
     * 在意外情況下,暫時性儲存遊戲資料,在下次開啟遊戲時,可以繼續遊戲。如來電話了。
     * @return a Bundle with this view's state
     */
    public Bundle saveState() {
        Bundle map = new Bundle();

        map.putIntArray("mAppleList", coordArrayListToArray(mAppleList));
        map.putInt("mDirection", Integer.valueOf(mDirection));
        map.putInt("mNextDirection", Integer.valueOf(mNextDirection));
        map.putLong("mMoveDelay", Long.valueOf(mMoveDelay));
        map.putLong("mScore", Long.valueOf(mScore));
        map.putIntArray("mSnakeTrail", coordArrayListToArray(mSnakeTrail));

        return map;
    }

    /**
     * Given a flattened array of ordinate pairs, we reconstitute them into a
     * ArrayList of Coordinate objects
     * 是coordArrayListToArray()的逆過程,用來讀取儲存在Bundle中的資料。
     * @param rawArray : [x1,y1,x2,y2,...]
     * @return a ArrayList of Coordinates
     */
    private ArrayList<Coordinate> coordArrayToArrayList(int[] rawArray) {
        ArrayList<Coordinate> coordArrayList = new ArrayList<Coordinate>();

        int coordCount = rawArray.length;
        for (int index = 0; index < coordCount; index += 2) {
            Coordinate c = new Coordinate(rawArray[index], rawArray[index + 1]);
            coordArrayList.add(c);
        }
        return coordArrayList;
    }

    /**
     * Restore game state if our process is being relaunched
     * 回覆遊戲資料。是saveState()的逆過程
     * @param icicle a Bundle containing the game state
     */
    public void restoreState(Bundle icicle) {
        setMode(PAUSE);

        mAppleList = coordArrayToArrayList(icicle.getIntArray("mAppleList"));
        mDirection = icicle.getInt("mDirection");
        mNextDirection = icicle.getInt("mNextDirection");
        mMoveDelay = icicle.getLong("mMoveDelay");
        mScore = icicle.getLong("mScore");
        mSnakeTrail = coordArrayToArrayList(icicle.getIntArray("mSnakeTrail"));
    }

    /*
     * handles key events in the game. Update the direction our snake is traveling
     * based on the DPAD. Ignore events that would cause the snake to immediately
     * turn back on itself.
     * 按鍵的監聽。
     * 現在大多數的android手機都沒有按鍵了。
     * 筆者就是在自己的模擬機上才能正常的使用這款小遊戲的 - -#
     * @see android.view.View#onKeyDown(int, android.os.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {
    	//這裡是遊戲的基本邏輯。如果你還沒嘗試一下它,先玩玩再說吧。那有助於你對程式碼的理解~
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            if (mMode == READY | mMode == LOSE) {
                /*
                 * At the beginning of the game, or the end of a previous one,
                 * we should start a new game.
                 */
                initNewGame();
                setMode(RUNNING);
                update(); //update()實現了對遊戲資料的更新,是整個遊戲的推動力。
                return (true);
            }

            if (mMode == PAUSE) {
                /*
                 * If the game is merely paused, we should just continue where
                 * we left off.
                 */
                setMode(RUNNING);
                update();
                return (true);
            }

            if (mDirection != SOUTH) {  //如果按鍵的方向 跟蛇本身的運動方向完全相反,則無法執行
                mNextDirection = NORTH;  
            }
            return (true);
        }

        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            if (mDirection != NORTH) {
                mNextDirection = SOUTH;
            }
            return (true);
        }

        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            if (mDirection != EAST) {
                mNextDirection = WEST;
            }
            return (true);
        }

        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            if (mDirection != WEST) {
                mNextDirection = EAST;
            }
            return (true);
        }

        return super.onKeyDown(keyCode, msg);
    }

    /**
     * Sets the TextView that will be used to give information (such as "Game
     * Over" to the user.
     * 起初不明白這個方法有什麼作用。刪除了以後才發現錯誤。Snake類會呼叫到它,來繫結到相應的textview.
     */
    public void setTextView(TextView newView) {  
        mStatusText = newView;
    }

    /**
     * Updates the current mode of the application (RUNNING or PAUSED or the like)
     * as well as sets the visibility of textview for notification
     * 
     * @param newMode
     */
    public void setMode(int newMode) {
        int oldMode = mMode;
        mMode = newMode;

        if (newMode == RUNNING & oldMode != RUNNING) {
            mStatusText.setVisibility(View.INVISIBLE); //遊戲開始後,將TextView的文字顯示設定為不可見。
            update(); //注意到,在initGame中也有update(),不過放心~ 多次重複 update不會影響效果的,
             			//蛇的移動有mLastMove 和 mMoveDelay 來校驗。這會在Update()中體現。
            			//當然,經過實驗,註釋掉這個update()似乎不會影響結果噢。
            return;
        }

        Resources res = getContext().getResources();
        CharSequence str = "";
        if (newMode == PAUSE) {
            str = res.getText(R.string.mode_pause);
        }
        if (newMode == READY) {
            str = res.getText(R.string.mode_ready);
        }
        if (newMode == LOSE) {
            str = res.getString(R.string.mode_lose_prefix) + mScore
                  + res.getString(R.string.mode_lose_suffix);
        }

        mStatusText.setText(str);
        mStatusText.setVisibility(View.VISIBLE);
    }

    /**
     * Selects a random location within the garden that is not currently covered
     * by the snake. Currently _could_ go into an infinite loop if the snake
     * currently fills the garden, but we'll leave discovery of this prize to a
     * truly excellent snake-player.
     * 在地圖上隨機的增加果子。注意蘋果的位置不可以是蛇體所在噢~這裡有個小bug,沒有檢測
     * 產生的果子位置 可能與 另一個果子位置重合。
     * 新產生的果子的座標會增加到mApplist的陣列上。
     */
    private void addRandomApple() {
        Coordinate newCoord = null;
        boolean found = false;
        while (!found) {
            // Choose a new location for our apple
        	//注意別產生在邊框上的果子
            int newX = 1 + RNG.nextInt(mXTileCount - 2);
            int newY = 1 + RNG.nextInt(mYTileCount - 2);
            newCoord = new Coordinate(newX, newY);

            // Make sure it's not already under the snake
            boolean collision = false;
            int snakelength = mSnakeTrail.size();
            for (int index = 0; index < snakelength; index++) {
                if (mSnakeTrail.get(index).equals(newCoord)) {
                    collision = true;
                }
            }
            // if we're here and there's been no collision, then we have
            // a good location for an apple. Otherwise, we'll circle back
            // and try again
            found = !collision;
        }
        if (newCoord == null) {
            Log.e(TAG, "Somehow ended up with a null newCoord!");
        }
        mAppleList.add(newCoord);
    }


    /**
     * Handles the basic update loop, checking to see if we are in the running
     * state, determining if a move should be made, updating the snake's location.
     * 重新整理遊戲狀態。每次遊戲畫面的更新、遊戲資料的更新,都是依靠這個update()來完成的。
     */
    public void update() {
        if (mMode == RUNNING) {
            long now = System.currentTimeMillis();

            if (now - mLastMove > mMoveDelay) {  //這裡是對蛇體遊戲剛開始時連續的兩個移動速率的控制
            						//主要作用應該是mMode變化時,對update()正確效果的保障。
                clearTiles(); 		//清空 介面畫布。		
                updateWalls();      //重新繪製牆壁
                updateSnake(); 	   //對蛇的 遊戲邏輯 的處理 以及繪製
                updateApples();   //對果子的 遊戲邏輯 的處理 以及繪製
                mLastMove = now;
            }
            mRedrawHandler.sleep(mMoveDelay);   //利用Handler進行 定時重新整理的控制
        }

    }

    /**
     * Draws some walls.
     * 用setTile繪製牆壁
     */
    private void updateWalls() {
        for (int x = 0; x < mXTileCount; x++) {
            setTile(GREEN_STAR, x, 0);
            setTile(GREEN_STAR, x, mYTileCount - 1);
        }
        for (int y = 1; y < mYTileCount - 1; y++) {
            setTile(GREEN_STAR, 0, y);
            setTile(GREEN_STAR, mXTileCount - 1, y);
        }
    }

    /**
     * Draws some apples.
     * 繪製果子
     */
    private void updateApples() {
        for (Coordinate c : mAppleList) {
            setTile(YELLOW_STAR, c.x, c.y);
        }
    }

    /**
     * Figure out which way the snake is going, see if he's run into anything (the
     * walls, himself, or an apple). If he's not going to die, we then add to the
     * front and subtract from the rear in order to simulate motion. If we want to
     * grow him, we don't subtract from the rear.
     * 
     */
    private void updateSnake() {
        boolean growSnake = false;  //吃過果子的蛇會長長。這個變數即為它的標記。

        // grab the snake by the head
        Coordinate head = mSnakeTrail.get(0);  //頭部很重要,只有頭部可能碰到果子。
        Coordinate newHead = new Coordinate(1, 1); //蛇下一步一定會前移,也就試newHead。長長只會從尾部增加。
        				//那麼為啥不用Coordinate newHead 呢?反正肯定會給他賦值的。
        				//注意到之後咱們的程式是在switch語句中給newHead賦值的,這個是編譯無法通過的~
        mDirection = mNextDirection;

        switch (mDirection) {
        case EAST: {
            newHead = new Coordinate(head.x + 1, head.y);
            break;
        }
        case WEST: {
            newHead = new Coordinate(head.x - 1, head.y);
            break;
        }
        case NORTH: {
            newHead = new Coordinate(head.x, head.y - 1);
            break;
        }
        case SOUTH: {
            newHead = new Coordinate(head.x, head.y + 1);
            break;
        }
        }

        // Collision detection
        // For now we have a 1-square wall around the entire arena
        //撞牆檢測
        if ((newHead.x < 1) || (newHead.y < 1) || (newHead.x > mXTileCount - 2)
                || (newHead.y > mYTileCount - 2)) {
            setMode(LOSE);
            return;

        }

        // Look for collisions with itself
        //撞自己檢測
        int snakelength = mSnakeTrail.size();
        for (int snakeindex = 0; snakeindex < snakelength; snakeindex++) {
            Coordinate c = mSnakeTrail.get(snakeindex);
            if (c.equals(newHead)) {
                setMode(LOSE);
                return;
            }
        }

        // Look for apples
        //吃果子檢測
        int applecount = mAppleList.size();
        for (int appleindex = 0; appleindex < applecount; appleindex++) {
            Coordinate c = mAppleList.get(appleindex);
            if (c.equals(newHead)) {
                mAppleList.remove(c);
                addRandomApple();
                
                mScore++;
                mMoveDelay *= 0.9;

                growSnake = true;
            }
        }

        // push a new head onto the ArrayList and pull off the tail
        //前進
        mSnakeTrail.add(0, newHead);
        // except if we want the snake to grow
        if (!growSnake) {
            mSnakeTrail.remove(mSnakeTrail.size() - 1);
        }

        //繪製新的蛇體
        int index = 0;
        for (Coordinate c : mSnakeTrail) {
            if (index == 0) {
                setTile(YELLOW_STAR, c.x, c.y);
            } else {
                setTile(RED_STAR, c.x, c.y);
            }
            index++;
        }

    }

    /**
     * Simple class containing two integer values and a comparison function.
     * There's probably something I should use instead, but this was quick and
     * easy to build.
     * 這是座標點的類。很簡單的儲存XY座標。
     */
    private class Coordinate {
        public int x;
        public int y;

        public Coordinate(int newX, int newY) {
            x = newX;
            y = newY;
        }

        public boolean equals(Coordinate other) {
            if (x == other.x && y == other.y) {
                return true;
            }
            return false;
        }

        @Override
        public String toString() {
            return "Coordinate: [" + x + "," + y + "]";
        }
    }
    
}


Snake.java

package com.example.android.snake;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

/**
 * Snake: a simple game that everyone can enjoy.
 * This is an implementation of the classic Game "Snake", in which you control a
 * serpent roaming around the garden looking for apples. Be careful, though,
 * because when you catch one, not only will you become longer, but you'll move
 * faster. Running into yourself or the walls will end the game.
 */

public class Snake extends Activity {


    private SnakeView mSnakeView;
    
    private static String ICICLE_KEY = "snake-view";

    /**
     * Called when Activity is first created. Turns off the title bar, sets up
     * the content views, and fires up the SnakeView.
     * 
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.snake_layout);

        mSnakeView = (SnakeView) findViewById(R.id.snake);
        mSnakeView.setTextView((TextView) findViewById(R.id.text));

        if (savedInstanceState == null) {
            // We were just launched -- set up a new game
            mSnakeView.setMode(SnakeView.READY);
        } else {
            // We are being restored
            Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
            if (map != null) {
                mSnakeView.restoreState(map);
            } else {
                mSnakeView.setMode(SnakeView.PAUSE);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Pause the game along with the activity
        mSnakeView.setMode(SnakeView.PAUSE);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //Store the game state
        outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
    }

}