<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>中文 &#187; gaomatrix</title>
	<atom:link href="http://software.intel.com/zh-cn/blogs/author/gaomatrix/feed/" rel="self" type="application/rss+xml" />
	<link>http://software.intel.com/zh-cn/blogs</link>
	<description></description>
	<lastBuildDate>Mon, 28 May 2012 13:40:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Amazed 游戏学习二 坐标的分析</title>
		<link>http://software.intel.com/zh-cn/blogs/2012/01/13/amazed/</link>
		<comments>http://software.intel.com/zh-cn/blogs/2012/01/13/amazed/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 08:33:05 +0000</pubDate>
		<dc:creator>gaomatrix</dc:creator>
				<category><![CDATA[博客征文专栏]]></category>
		<category><![CDATA[游戏]]></category>

		<guid isPermaLink="false">http://software.intel.com/zh-cn/blogs/2012/01/13/amazed/</guid>
		<description><![CDATA[对于这种和贪吃蛇类似的游戏，坐标的计算在程序中占了很大的比重，只有坐标算对了，才能在正确的时间、正确的地点画出来正确的东西。 下面是Maze迷宫的坐标计算 01.// maze level data 02.private static int[] mMazeData; 这个一维数组存放所有的tile的类型 01.// maze tile size and dimension 02.private final static int TILE_SIZE = 16; 03.private final static int MAZE_COLS = 20; 04.private final static int MAZE_ROWS = 26; 01.// tile types 02.public final static int PATH_TILE = 0; 03.public final static int VOID_TILE = 1;//death [...]]]></description>
			<content:encoded><![CDATA[<p>对于这种和贪吃蛇类似的游戏，坐标的计算在程序中占了很大的比重，只有坐标算对了，才能在正确的时间、正确的地点画出来正确的东西。</p>
<p>下面是Maze迷宫的坐标计算<br />
01.// maze level data<br />
02.private static int[] mMazeData;</p>
<p>这个一维数组存放所有的tile的类型<br />
01.// maze tile size and dimension<br />
02.private final static int TILE_SIZE = 16;<br />
03.private final static int MAZE_COLS = 20;<br />
04.private final static int MAZE_ROWS = 26;</p>
<p>01.// tile types<br />
02.public final static int PATH_TILE = 0;<br />
03.public final static int VOID_TILE = 1;//death area<br />
04.public final static int EXIT_TILE = 2;//the target<br />
而这个坐标的读取是通过从文件读取的，当然也可以通过写几个数组的形式，但是以文件的形式的话就不需要修改代码就可以实现软件的修改是一种比在代码中写几个大的数组要好的形式。</p>
<p>01./**<br />
02. * Load specified maze level.<br />
03. *<br />
04. * @param activity<br />
05. * Activity controlled the maze, we use this load the level data<br />
06. * @param newLevel<br />
07. * Maze level to be loaded.<br />
08. */<br />
09. void load(Activity activity, int newLevel) {<br />
10. // maze data is stored in the assets folder as level1.txt, level2.txt<br />
11. // etc....<br />
12. String mLevel = "level" + newLevel + ".txt";<br />
13.<br />
14. InputStream is = null;<br />
15.<br />
16. try {<br />
17. // construct our maze data array.<br />
18. mMazeData = new int[MAZE_ROWS * MAZE_COLS];<br />
19. // attempt to load maze data.<br />
20. is = activity.getAssets().open(mLevel);<br />
21.<br />
22. // we need to loop through the input stream and load each tile for<br />
23. // the current maze.<br />
24. for (int i = 0; i &lt; mMazeData.length; i++) {<br />
25. // data is stored in unicode so we need to convert it.<br />
26. mMazeData[i] = Character.getNumericValue(is.read());<br />
27.<br />
28. // skip the "," and white space in our human readable file.<br />
29. is.read();<br />
30. is.read();<br />
31. }<br />
32. } catch (Exception e) {<br />
33. Log.i("Maze", "load exception: " + e);<br />
34. } finally {<br />
35. closeStream(is);<br />
36. }<br />
37. }<br />
mMazeData数组存放了每个tile是什么类型的，MAZE_ROWS一共有多少行，MAZE_COLS一共多少列，TILE_SIZE每个tile的宽度，这样就可以在界面上画出来整个界面的布局</p>
<p>01./**<br />
02. * Draw the maze.<br />
03. *<br />
04. * @param canvas<br />
05. * Canvas object to draw too.<br />
06. * @param paint<br />
07. * Paint object used to draw with.<br />
08. */<br />
09. public void draw(Canvas canvas, Paint paint) {<br />
10. // loop through our maze and draw each tile individually.<br />
11. for (int i = 0; i 0)<br />
22. mLocation = mCellRow * MAZE_COLS;<br />
23.<br />
24. // add the column location.<br />
25. mLocation += mCellCol;<br />
26.<br />
27. return mMazeData[mLocation];<br />
28. }<br />
/**<br />
* Determine which cell the marble currently occupies.<br />
*<br />
* @param x<br />
* Current x co-ordinate of marble on the screen.<br />
* @param y<br />
* Current y co-ordinate of marble on the screen.<br />
* @return The actual cell occupied by the marble.<br />
*/<br />
public int getCellType(int x, int y) {<br />
// convert the x,y co-ordinate into row and col values.<br />
int mCellCol = x / TILE_SIZE;<br />
int mCellRow = y / TILE_SIZE;</p>
<p>// location is the row,col coordinate converted so we know where in the<br />
// maze array to look.<br />
int mLocation = 0;</p>
<p>// if we are beyond the 1st row need to multiple by the number of<br />
// columns.<br />
if (mCellRow &gt; 0)<br />
mLocation = mCellRow * MAZE_COLS;</p>
<p>// add the column location.<br />
mLocation += mCellCol;</p>
<p>return mMazeData[mLocation];<br />
}下面是Marble小球的坐标计算</p>
<p>01.// marble attributes<br />
02.// x,y are private because we need boundary checking on any new values to<br />
03.// make sure they are valid.<br />
04.private int mX = 0;<br />
05.private int mY = 0;<br />
06.private int mRadius = 8;<br />
07.private int mColor = Color.WHITE;<br />
08.private int mLives = 5;</p>
<p>在AmazedView中的onDraw()调用gameTick()的时候会更新小球Marble的坐标：</p>
<p>01./**<br />
02. * Called from gameTick(), update marble x,y based on latest values obtained<br />
03. * from the Accelerometer sensor. AccelX and accelY are values received from<br />
04. * the accelerometer, higher values represent the device tilted at a more<br />
05. * acute angle.<br />
06. */<br />
07. public void updateMarble() {<br />
08. // we CAN give ourselves a buffer to stop the marble from rolling even<br />
09. // though we think the device is "flat".<br />
10. if (mAccelX &gt; mSensorBuffer || mAccelX mSensorBuffer || mAccelY 0) {<br />
19. // user still has some lives remaining, restart the level.<br />
20. mMarble.death();<br />
21. mMarble.init();<br />
22. mWarning = true;<br />
23. } else {<br />
24. // user has no more lives left, end of game.<br />
25. mEndTime = System.currentTimeMillis();<br />
26. mTotalTime += mEndTime - mStartTime;<br />
27. switchGameState(GAME_OVER);<br />
28. }<br />
29.<br />
30. } else if (mMaze.getCellType(mMarble.getX(), mMarble.getY()) == mMaze.EXIT_TILE) {<br />
31. // user has reached the exit tiles, prepare the next level.<br />
32. mEndTime = System.currentTimeMillis();<br />
33. mTotalTime += mEndTime - mStartTime;<br />
34. initLevel();<br />
35. }<br />
36. }</p>
<p>在Marble中更新小球坐标：</p>
<p>01./**<br />
02. * Attempt to update the marble with a new x value, boundary checking<br />
03. * enabled to make sure the new co-ordinate is valid.<br />
04. *<br />
05. * @param newX<br />
06. * Incremental value to add onto current x co-ordinate.<br />
07. */<br />
08. public void updateX(float newX) {<br />
09. mX += newX;<br />
10.<br />
11. // boundary checking, don't want the marble rolling off-screen.<br />
12. if (mX + mRadius &gt;= mView.getWidth())<br />
13. mX = mView.getWidth() - mRadius;<br />
14. else if (mX - mRadius &lt; 0)<br />
15. mX = mRadius;<br />
16. }</p>
<p>if-else中是对左右边界的处理，同样的是对上下边界的处理。</p>
]]></content:encoded>
			<wfw:commentRss>http://software.intel.com/zh-cn/blogs/2012/01/13/amazed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

