androidSnake(阅读源码)

/MySnake/src/edu/hhxy/android/snake/Snake.java


package edu.hhxy.android.snake;

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

public class Snake extends Activity {
private SnakeView mSnakeView;
private static String ICICLE_KEY = "snake-view";
@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
mSnakeView.setMode(SnakeView.PAUSE);
}

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

/MySnake/src/edu/hhxy/android/snake/SnakeView.java


package edu.hhxy.android.snake;

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

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import edu.hhxy.android.utils.Coordinate;
import edu.hhxy.android.utils.SnakeDataUtils;

@SuppressLint("HandlerLeak")
public class SnakeView extends TileView {
private SnakeDataUtils snakeDataUtils = new SnakeDataUtils();
private static final String TAG = "SnakeView";
// mMode一个变量表示,用于确定游戏是PAUSE、READY、RUNNING、LOSE
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;

// mDirection 当前蛇头的方向,蛇头方向有NORTH、SOUTH、EAST、WEST
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;

// 什么颜色的苹果RED_STAR红、YELLOW_STAR黄、GREEN_STAR绿
private static final int RED_STAR = 1;
private static final int YELLOW_STAR = 2;
private static final int GREEN_STAR = 3;

// mScore:当前得分
private long mScore = 0;
// 刷新屏幕的时间
private long mMoveDelay = 600;
// 最后一次移动的时间,用now-mLastMove可知已经刷新了多长时间
private long mLastMove;

/**
* mStatusText: text shows to the user in some run states
*/
private TextView mStatusText;

/**
* mSnakeTrail: 蛇链表 mAppleList: 苹果链表
*/
private ArrayList<Coordinate> mSnakeTrail = new ArrayList<Coordinate>();
private ArrayList<Coordinate> mAppleList = new ArrayList<Coordinate>();
// 随机数产生类
private static final Random RNG = new Random();

// 线程,定时刷新屏幕
private RefreshHandler mRedrawHandler = new RefreshHandler();

class RefreshHandler extends Handler {
public void handleMessage(Message msg) {
SnakeView.this.update();
SnakeView.this.invalidate();
}

public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};

// 重写父类构造方法
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();// 新游戏开始,清空list
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;

// 添加两个苹果,准备被蛇吃
addRandomApple();
addRandomApple();

mMoveDelay = 600;
mScore = 0;
}

// 保存下蛇和苹果的坐标,蛇的方向,分数,延迟时间等信息
public Bundle saveState() {
Bundle map = new Bundle();

map.putIntArray("mAppleList",
snakeDataUtils.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",
snakeDataUtils.coordArrayListToArray(mSnakeTrail));

return map;
}

// 存储数据
public void restoreState(Bundle icicle) {
setMode(PAUSE);

mAppleList = snakeDataUtils.coordArrayToArrayList(icicle
.getIntArray("mAppleList"));
mDirection = icicle.getInt("mDirection");
mNextDirection = icicle.getInt("mNextDirection");
mMoveDelay = icicle.getLong("mMoveDelay");
mScore = icicle.getLong("mScore");
mSnakeTrail = snakeDataUtils.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;
}

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

// 添加一个随机的苹果
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);

// 保证苹果不出现在蛇身上
boolean collision = false;
int snakelength = mSnakeTrail.size();
for (int index = 0; index < snakelength; index++) {
if (mSnakeTrail.get(index).equals(newCoord)) {
collision = true;
}
}
// 如果出现两个苹果,保证苹果重复
int applelength = mAppleList.size();
for (int index = 0; index < applelength; index++) {
if (mAppleList.get(index).equals(newCoord)) {
collision = true;
}
}
found = !collision;
}
if (newCoord == null) {
Log.e(TAG, "Somehow ended up with a null newCoord!");
}
mAppleList.add(newCoord);
}

// 更新信息
public void update() {
if (mMode == RUNNING) {
long now = System.currentTimeMillis();

if (now - mLastMove > mMoveDelay) {
clearTiles();
updateWalls();
updateSnake();
updateApples();
mLastMove = now;
}
mRedrawHandler.sleep(mMoveDelay);
}
}

/**
* 给mTileGrid赋值,标识墙
*/
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);
}
}

/**
* 给mTileGrid赋值,标识苹果
*/
private void updateApples() {
for (Coordinate c : mAppleList) {
setTile(YELLOW_STAR, c.x, c.y);
}
}

/*
* 更新蛇,因为蛇要吃苹果
*/
private void updateSnake() {
boolean growSnake = false;

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

// 检测蛇是否撞墙
if ((newHead.x < 1) || (newHead.y < 1) || (newHead.x > mXTileCount - 2)
|| (newHead.y > mYTileCount - 2)) {
setMode(LOSE);
return;

}

// 检测蛇是否撞自身
int snakelength = mSnakeTrail.size();
for (int snakeindex = 0; snakeindex < snakelength; snakeindex++) {
Coordinate c = mSnakeTrail.get(snakeindex);
if (c.equals(newHead)) {
setMode(LOSE);
return;
}
}

// 吃苹果,自身涨一截,屏幕刷新变快,也就是速度变快
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.99;

growSnake = true;// growSnake表示蛇是否迟到苹果,判定是否自身增长
}
}
/*
* 蛇移动的过程其实就是把蛇尾放到蛇头前,然后去掉蛇尾,这期间,如果蛇吃了苹果,则蛇自身增长,否则自身不增长
*/
mSnakeTrail.add(0, newHead);
// 如果蛇不需要增长,减去蛇尾即可,如果需要增长,则把增长放到蛇头,不减蛇尾
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++;
}
}
}

/MySnake/src/edu/hhxy/android/snake/TileView.java


package edu.hhxy.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:一个继承View的基类
*/
public class TileView extends View {
protected static int mTileSize;// 图片大小
protected static int mXTileCount;// 总的列数
protected static int mYTileCount;// 总的行数
private static int mXOffset;// x边距
private static int mYOffset;// y边距
private Bitmap[] mTileArray;// 图片数组

/**
* 二维数组,根据其值用来确定是否要画苹果,以及要画什么颜色的苹果
*/
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();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// w宽,h长
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();
}

/**
* 初始化
*/

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

/**
* 加载图片,使mTitleArray[]表示不同的图片
*/
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;
}

/**
* 初始化mTitleGrid[][]
*/
public void clearTiles() {
for (int x = 0; x < mXTileCount; x++) {
for (int y = 0; y < mYTileCount; y++) {
setTile(0, x, y);
}
}
}

/**
* 把某一位置的坐标置为tileindex,以确定mTitleGrid[][]处要画的图片
*/
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);
}
}
}
}
}

/MySnake/src/edu/hhxy/android/utils/Coordinate.java


package edu.hhxy.android.utils;

public class Coordinate {

/**
* 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.
*
*/
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 + "]";
}
}

/MySnake/src/edu/hhxy/android/utils/SnakeDataUtils.java


package edu.hhxy.android.utils;

import java.util.ArrayList;

public class SnakeDataUtils {
/**
* 数据转换成以int [x1,y1,x2,y2,x3,y3...]的形式便于保存下蛇和苹果的坐标
*/
public 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;
}

/**
* 数据从int [x1,y1,x2,y2,x3,y3...]恢复
*/
public 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;
}

}

androidSnake(阅读源码),布布扣,bubuko.com

时间: 06-07

androidSnake(阅读源码)的相关文章

Linux 平台下阅读源码的工具链

原文:http://blog.jobbole.com/101322/ 前言 看源代码是一个程序员必须经历的事情,也是可以提升能力的一个捷径.个人认为: 要完全掌握一个软件的方法只有阅读源码. 在Windows下有sourceinsight这个源码阅读软件(虽然我没用过,但是网上评价还不错),由于我是个Linuxer,并不喜欢用Windows,所以自然是选择在Linux下阅读源码的工具了. 下面我将逐一介绍在Linux下阅读源码的工具. vim + ctags + cscope 源码阅读三剑客.v

使用 vim + ctags + cscope + taglist 阅读源码

转自:http://my.oschina.net/u/554995/blog/59927 最近,准备跟学长一起往 linux kernel 的门里瞧瞧里面的世界,虽然我们知道门就在那,但我们还得找到合适的角度才会看得更舒服,对吧^_^ . 阅读源码的工具有很多,而且如今的集成开发环境(IDE)也很强大,但对于经常使用vim编辑器的程序员来说,对vim的强大绝对是“不抛弃,不放弃”的,况且我们只要安装一些插件配合vim的工作一样能达到IDE的效果,好了,废话少说.浏览了很多有关的网页资源后,发现有

Spring源码解析——如何阅读源码

最近没什么实质性的工作,正好有点时间,就想学学别人的代码.也看过一点源码,算是有了点阅读的经验,于是下定决心看下spring这种大型的项目的源码,学学它的设计思想. 手码不易,转载请注明:xingoo 这篇博文你可以了解到: 1 Spring jar包以及源码使用 2 简单的spring运行示例 3 利用断点调试程序,如何快速的阅读程序[快捷键等的使用] 这次阅读的源码比较老了,是3.0.5版本的,由于正好手头有这个版本的源码,而且平时基本上也是用的这个版本Spring,因此后文的分析也都是针对

如何阅读源码,如何提高阅读源码的效率

就我自己阅读安卓源码的经验,本人研究过 settings,launcher,Sysupdate ,framework /services ,recovery的部分源码,并且成功修改google留下来的bug. 如果就是熟悉代码,不带问题的去看,看的会比较累,但是仍然是有方法可以借鉴的,其实这个方法,也是生活经验得来的,大家都会的. 就是从整体到局部,由总而分. 比如:你想了解recovery的代码,就应该先了解这个Recovery的运作流程,网上有很多人总结了,总概括图,先有个大概的了解: 第二

阅读源码的重要性:如厨师选食材,耍厨具——在Eclipse中如何查看Java、Android源码

首先,很多人说,不会看jdk中的源码就不叫学过Java.显然这是肯定的.打个比方:真正的厨师需要从食材的选取.加工,到最后的烹饪.装盘成型,甚至到最后给用户介绍食用方法等一整套流程走下来.而实际上很多厨师只是做了其中的一小部分,所以最多也只能叫得上"炒菜的",而不是一个厨师.而很多顶级餐厅强调选材,就是从源头上找到适合做某道菜的原料,甚至是反过来,得到一个供应链供应某种独特食材而专门为该材料设计某种烹饪技术.这就是从头至尾的资源最大化利用,其体现创新及内功的地方贯穿整个流水线!特别的,

Linux 下阅读源码工具(Vim + ctags+Cscope)

0. 写在前面的废话 开发环境迁移到了Ubuntu下,所有windows下好用的工具都要找个替代品. windows下一直用 souce Insight 来阅读源码,需要在Ubuntu下找个替代品. 上网看了看,貌似Vim + Ctags + Cscope不错,安装来试试 o(∩∩)o 1. 安装 在我用的ubuntu中,这三个软件都不是自带的,需要手动安装,很简单用apt-get命令安装就好了 $ sudo apt-get install vim $ sudo apt-get install

大神如何阅读源码

1.腾讯IMWEB负责人说: 首先,搞清楚自己要读懂他们的原因和动机. 其次,可以先看下这些优秀框架或者库的设计文档和架构图,这样会让你宏观上对一些概念有些认识. 然后,从你最感兴趣的一个点,开始设置断点,跟进去看发生了哪些事情. 和架构设计哪一块是match的. 有人补充:最快,最易懂方法.断点单步调试. 如:jquery中 $.fn.show 源码是如何实现的. 自己写个 $('#test').show(),打上断点.单步调试.那么你可以看到jquery中每一步发生了什么事情.分析即可. 2

怎样阅读源码

第一章: 导论 ++++++++++++ 1.要养成一个习惯, 经常花时间阅读别人编写的高品质代码. 2.要有选择地阅读代码, 同一时候, 还要有自己的目标. 您是想学习新的模式|编码风格|还是满足某些需求的方法. 3.要注意并重视代码中特殊的非功能性需求, 这些需求或许会导致特殊的实现风格. 4.在现有的代码上工作时, 请与作者和维护人员进行必要的协调, 以避免反复劳动或产生厌恶情绪. 5.请将从开放源码软件中得到的益处看作是一项贷款, 尽可能地寻找各种方式来回报开放源码社团. 6.多数情况下

Spring源码解析——如何阅读源码(转)

最近没什么实质性的工作,正好有点时间,就想学学别人的代码.也看过一点源码,算是有了点阅读的经验,于是下定决心看下spring这种大型的项目的源码,学学它的设计思想. 手码不易,转载请注明:xingoo 这篇博文你可以了解到: 1 Spring jar包以及源码使用 2 简单的spring运行示例 3 利用断点调试程序,如何快速的阅读程序[快捷键等的使用] 这次阅读的源码比较老了,是3.0.5版本的,由于正好手头有这个版本的源码,而且平时基本上也是用的这个版本Spring,因此后文的分析也都是针对