package com.example.android.snake;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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 final static String TAG = "**** ***Snake***** *****";

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);

// No Title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);

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());
}

}

/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/



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;

/** x轴方向上格子的个数 */
protected static int mXTileCount;
/** y轴方向上格子的个数 */
protected static int mYTileCount;

private static int mXOffset;
private static int mYOffset;


/**
*
* A hash that maps integer handles specified by the subclasser to the
* drawable that will be used for that reference
*/
private Bitmap[] mTileArray;

/**
* 声明用来存放绘画图像的x,y轴的位置的数组
* A two-dimensional array of integers in which the number represents the
* index of the tile that should be drawn at that locations
*/
private int[][] mTileGrid;

private final Paint mPaint = new Paint();

public TileView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

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
*
* @param tilecount
*/

public void resetTiles(int tilecount) {
mTileArray = new Bitmap[tilecount];
}


@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 = ((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.
*
* @param key
* @param tile
*/
public void loadTile(int key, Drawable tile) {
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;
}

/**
* Resets all tiles to 0 (empty)
* 清空
*/
public void clearTiles() {
for (int x = 0; x < mXTileCount; x++) {
for (int y = 0; y < mYTileCount; y++) {
setTile(0, x, y);
}
}
}

/**
* 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.
*
* @param tileindex 图片的索引
* @param x
* @param y
*/
public void setTile(int tileindex, int x, int y) {
mTileGrid[x][y] = tileindex;
}

/**
* 画出主界面图
*/
@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);
}
}
}

}

}


/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

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;
/**
* 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;

/**
* 显示游戏运行的状态
* mStatusText: text shows to the user in some run states
*/
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
*/
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.
*/
private RefreshHandler mRedrawHandler = new RefreshHandler();

class RefreshHandler extends Handler {

/**
* 响应消息
*/
@Override
public void handleMessage(Message msg) {
SnakeView.this.update();
SnakeView.this.invalidate();
}

/**
* 向外提供人工的调用消息的接口
* @param delayMillis
*/
public void sleep(long delayMillis) {
//注销消息
this.removeMessages(0);
//添加消息
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};


/**
* Constructs a SnakeView based on inflation from XML
*
* @param context
* @param attrs
*/
public SnakeView(Context context, AttributeSet attrs) {
super(context, attrs);
initSnakeView();
}

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

/**
* 初始化界面
*/
private void initSnakeView() {
setFocusable(true);

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...]
* 将坐标的列表信息转换成一维数组,格式如上
*
* *****************注意这边格式的写法*******************
*/
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
*
* @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
*
* @param icicle a Bundle containing the game state
*/
public void restoreState(Bundle icicle) {
setMode(PAUSE);

//从资源中读取arrayList
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.
*
* (non-Javadoc)
*
* @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();
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.
*
* @param newView
*/
public void setTextView(TextView newView) {
mStatusText = newView;
}

/**
* 更新的应用程序当前的状态(运行或已暂停或类似),以及目前的模式,设置textview能见度的通知
*
* 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();

Log.i(TAG, "now - mLastMove ===== "+(now - mLastMove));
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;
}
}

/**
* 如果撞到四周的墙,则Game over
*/
// 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;

}

/**
* 如果撞到自己身体部分,则Game over
*/
// 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) {
Log.i(TAG, "-----cut trail--------");
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;
}

/**
* 比较两个坐标是否相等
* @param other
* @return
*/
public boolean equals(Coordinate other) {
if (x == other.x && y == other.y) {
return true;
}
return false;
}

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

}

总结一下,慢慢的分析就行了,算法也挺简单的

更多相关文章

  1. Android 判断是都联网状态
  2. android 设置状态栏全透明,背景延伸到状态栏
  3. Android全透明状态栏
  4. android 如何判断GPS,NETWORK开启状态
  5. android沉浸式状态栏和虚拟按键
  6. [rk3399][Android 6.0]隐藏状态栏
  7. android 判断当前应用是否处于活动状态

随机推荐

  1. MediaStore与Media.EXTERNAL_CONTENT_URI
  2. 修改eclipse中Android工程的sdk
  3. android用户界面之WebView教程实例汇总
  4. 测试你的Android应用程序
  5. Android的screenOrientation设置
  6. android 每天定时提醒功能实现
  7. 一些非常实用的 Android 开发资源
  8. Android 将APK文件安装到AVD中并分析其界
  9. android客户端向服务器提交请求的中文乱
  10. 招募Android手机游戏开发团队成员(学员)