【正文】
} return (true)。 } return (true)。 } return (true)。 return (true)。 } if (mMode == PAUSE) { /* * If the game is merely paused, we should just continue where * we left off. */ setMode(RUNNING)。 update()。 } /* * 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. * * (nonJavadoc) * * see onKeyDown(int, ) */ Override public boolean onKeyDown(int keyCode, KeyEvent msg) { if (keyCode == ) { 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()。 mScore = (mScore)。 mNextDirection = (mNextDirection)。 mAppleList = coordArrayToArrayList((mAppleList))。 } return coordArrayList。 index += 2) { Coordinate c = new Coordinate(rawArray[index], rawArray[index + 1])。 for (int index = 0。 } /** * Given a flattened array of ordinate pairs, we reconstitute them into a * ArrayList of Coordinate objects * * param rawArray * : [x1,y1,x2,y2,...] * return a ArrayList of Coordinates */ private ArrayListCoordinate coordArrayToArrayList(int[] rawArray) { ArrayListCoordinate coordArrayList = new ArrayListCoordinate()。 (mSnakeTrail, coordArrayListToArray(mSnakeTrail))。 (mMoveDelay, (mMoveDelay))。 (mDirection, (mDirection))。s state */ public Bundle saveState() { Bundle map = new Bundle()。 } return rawArray。 rawArray[2 * index] = 。 index count。 int[] rawArray = new int[count * 2]。 mScore = 0。 addRandomApple()。 // ?????????? mNextDirection = NORTH。 (new Coordinate(3, 30))。 (new Coordinate(5, 30))。s just turned north // ???????????? (new Coordinate(7, 30))。 // For now we39。 } private void initNewGame() { ()。 loadTile(YELLOW_STAR, ())。 // ??????tile???????????? resetTiles(4)。 } private void initSnakeView() { setFocusable(true)。 } public SnakeView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle)。 /** * Constructs a SnakeView based on inflation from XML * * param context * param attrs */ public SnakeView(Context context, AttributeSet attrs) { super(context, attrs)。 sendMessageDelayed(obtainMessage(0), delayMillis)。 ()。 private Button mBottom。 private Button mRight。?handler?????????????Handler???Ь?????????????? // ??????Handler?????????????????????30?????????????????????????????????????????????? private Button mStart。// ????? /** * 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. */ private RefreshHandler mRedrawHandler = new RefreshHandler()。// ??????У???tile?????????? private ArrayListCoordinate mAppleList = new ArrayListCoordinate()。// ?????????????????? /** * mSnakeTrail: a list of Coordinates that make up the snake39。// ??????????????????? /** * mLastMove: tracks the absolute time when the snake last moved, and is * used to determine if a move should be made based on mMoveDelay. */ private long mLastMove。 /** * 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。// ??????????????????????tile??drawable??????RED_STAR????????????????tile?? private static final int YELLOW_STAR = 2。 private static final int WEST = 4。 private static final int SOUTH = 2。 // ???????????????????????? private int mNextDirection = NORTH。 public static final int LOSE = 3。 public static final int READY = 1。 /** * 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。判斷蛇是否吃到食物的方法刷新蛇的當(dāng)前位置的方法主要用于刷新蛇的當(dāng)前位置。在隨機位置出現(xiàn)食物的方法通過程序判斷動態(tài)地設(shè)置用戶提示信息,如游戲結(jié)束。2.在Android手機上,每個按鍵都會有一個唯一的鍵值與它對應(yīng),可以通過獲得鍵值來判斷哪個鍵被按下了并采取相應(yīng)的動作。1.SnakeView類的詳細設(shè)計 } } } }} y mYTileCount。 x mXTileCount。??tile?????????????????????????tile??draw??canvas??? Override public void onDraw(Canvas canvas) { (canvas)。???tile??????????????????????????????tileindex?????????mTileArray[]?е?index */ public void setTile(int tileindex, int x, int y) { mTileGrid[x][y] = tileindex。 y++) { setTile(0, x, y)。 x++) { for (int y = 0。 } /** * Resets all tiles to 0 (empty) * */ public void clearTiles() { for (int x = 0。 (canvas)。 Canvas canvas = new Canvas(bitmap)。 clearTiles()。 mYOffset = ((h (mTileSize * mYTileCount)) / 2)。 mYTileCount = (int) (h / mTileSize)。 } // ?????????????????????????????????view???????????????????????????view???С????0????????layout???255。 ()。 TypedArray a = (attrs, )。 ()。???googleū???????????????飬?????????????????????????? TypedArray a = (attrs, )。 // ?????245。// ????????????????????? // ??????tile?????顣????int[1][1]=0?????????????int[1][2