【正文】
= new Coordinate(newX, newY)。 // Make sure it39。s not already under the snake boolean collision = false。 int snakelength = ()。 for (int index = 0。 index snakelength。 index++) { if ((index).equals(newCoord)) { collision = true。 } } // if we39。re here and there39。s been no collision, then we have // a good location for an apple. Otherwise, we39。ll circle back // and try again found = !collision。 } if (newCoord == null) { (TAG, Somehow ended up with a null newCoord!)。 } (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 snake39。s location. */ public void update() { if (mMode == RUNNING) { long now = ()。 if (now mLastMove mMoveDelay) { clearTiles()。 updateWalls()。 updateSnake()。 updateApples()。 mLastMove = now。 } (mMoveDelay)。 } } /** * Draws some walls. * */ 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, , )。 } } /** * Figure out which way the snake is going, see if he39。s run into anything (the * walls, himself, or an apple). If he39。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 don39。t subtract from the rear. * */ private void updateSnake() { boolean growSnake = false。 // grab the snake by the head Coordinate head = (0)。 Coordinate newHead = new Coordinate(1, 1)。 mDirection = mNextDirection。 switch (mDirection) { case EAST: { newHead = new Coordinate( + 1, )。 break。 } case WEST: { newHead = new Coordinate( 1, )。 break。 } case NORTH: { newHead = new Coordinate(, 1)。 break。 } case SOUTH: { newHead = new Coordinate(, + 1)。 break。 } } // Collision detection // For now we have a 1square wall around the entire arena if (( 1) || ( 1) || ( mXTileCount 2) || ( mYTileCount 2)) { setMode(LOSE)。 return。 } // Look for collisions with itself int snakelength = ()。 for (int snakeindex = 0。 snakeindex snakelength。 snakeindex++) { Coordinate c = (snakeindex)。 if ((newHead)) { setMode(LOSE)。 return。 } } // Look for apples int applecount = ()。 for (int appleindex = 0。 appleindex applecount。 appleindex++) { Coordinate c = (appleindex)。 if ((newHead)) { (c)。 addRandomApple()。 mScore++。 mMoveDelay *= 。 growSnake = true。 } } // push a new head onto the ArrayList and pull off the tail (0, newHead)。 // except if we want the snake to grow if (!growSnake) { (() 1)。 } int index = 0。 for (Coordinate c : mSnakeTrail) { if (index == 0) { setTile(YELLOW_STAR, , )。 } else { setTile(RED_STAR, , )。 } index++。 } } /** * Simple class containing two integer values and a parison function. * There39。s probably something I should use instead, but this was quick and * easy to build. * */ 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 == amp。amp。 y == ) { return true。 } return false。 } @Override public String toString() { return Coordinate: [ + x + , + y + ]。 } } }?xml version= encoding=utf8?! Copyright (C) 2007 The Android Open Source Project Licensed under the Apache License, Version (the License)。 you may not use this file except in pliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.FrameLayout xmlns:android= android:layout_width=match_parent android:layout_height=match_parent android:id=@+id/snake android:layout_width=match_parent android:layout_height=match_parent tileSize=24 / RelativeLayout android:layout_width=match_parent an