📄 snakeview.java
字号:
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. * * @param newView */ 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); 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. * */ 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. */ public void update() { if (mMode == RUNNING) { long now = System.currentTimeMillis(); if (now - mLastMove > mMoveDelay) { clearTiles(); updateWalls(); updateSnake(); updateApples(); mLastMove = now; } mRedrawHandler.sleep(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, 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); 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. * */ 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 + "]"; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -