请选择 进入手机版 | 继续访问电脑版

[Android] Android 2d游戏开辟 之贪吃蛇基于surfaceview

[复制链接]
查看93 | 回复15 | 2021-9-14 07:39:47 | 显示全部楼层 |阅读模式

前两个游戏是基于View游戏框架的,View游戏框架只得当 做静止的,异步触发的游戏,假如 做不停 在动的游戏,View的服从 就不高了,我们必要 一种同步触发的游戏框架,也就是surfaceview游戏框架,你大概 会问,什么七零八落 的,啥叫同步?啥叫异步?。。。我就不告诉你。。。我们先看一下这个同步框架,看看骚年你能不能本身 意会 。

GameView.java(继承自SurfaceView)

  1. package com.next.eatsnake;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.view.MotionEvent;
  7. import android.view.SurfaceHolder;
  8. import android.view.SurfaceView;
  9. import android.view.View;
  10. import android.view.View.OnTouchListener;
  11. import java.util.Random;
  12. /**
  13. * Created by Next on 2016/3/24 0024.
  14. */
  15. public class GameView extends SurfaceView implements SurfaceHolder.Callback,OnTouchListener,Runnable {
  16. enum GameState{
  17. Menu,
  18. Playing,
  19. Over;
  20. }
  21. GameState gameState;//游戏状态
  22. Thread thread;//游戏线程
  23. Boolean flag;//游戏循环控制标志
  24. Canvas canvas;//画布
  25. Paint paint;//画笔
  26. SurfaceHolder holder;//SurfaceView控制句柄
  27. Random random;//随机数
  28. NextEvent nextEvent;//游戏输入事件
  29. int scrW,scrH;//屏幕的宽和高
  30. public GameView(Context context) {
  31. super(context);
  32. gameState = GameState.Menu;
  33. flag = true;
  34. paint = new Paint();
  35. paint.setAntiAlias(true);//笔迹平滑
  36. thread = new Thread(this);
  37. random = new Random();
  38. nextEvent = new NextEvent();
  39. holder = this.getHolder();
  40. holder.addCallback(this);
  41. this.setOnTouchListener(this);
  42. setKeepScreenOn(true);
  43. scrW = ((MainActivity)context).getWindowManager().getDefaultDisplay().getWidth();
  44. scrH = ((MainActivity)context).getWindowManager().getDefaultDisplay().getHeight();
  45. }
  46. @Override
  47. public boolean onTouch(View v, MotionEvent event) {
  48. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  49. nextEvent.setDownX((int) event.getX());
  50. nextEvent.setDownY((int) event.getY());
  51. }else if (event.getAction() == MotionEvent.ACTION_UP) {
  52. nextEvent.setUpX((int) event.getX());
  53. nextEvent.setUpY((int) event.getY());
  54. }
  55. return true;
  56. }
  57. private void mLogic(){
  58. switch (gameState){
  59. case Menu:
  60. menuLogic();
  61. break;
  62. case Playing:
  63. playingLogic();
  64. break;
  65. case Over:
  66. overLogic();
  67. break;
  68. }
  69. nextEvent.init();//每次逻辑循环后,清空事件
  70. }
  71. private void menuLogic(){
  72. if(nextEvent.getUpX() > 0){
  73. gameState = GameState.Playing;
  74. }
  75. }
  76. private void playingLogic(){
  77. if(nextEvent.getDir() == NextEvent.DOWN){
  78. gameState = GameState.Over;
  79. }
  80. }
  81. private void overLogic(){
  82. if(nextEvent.getDir() == NextEvent.RIGHT){
  83. gameState = GameState.Menu;
  84. }
  85. }
  86. private void mDraw(){
  87. try {
  88. canvas = holder.lockCanvas();
  89. canvas.drawColor(Color.WHITE);
  90. switch (gameState){
  91. case Menu:
  92. menuDraw(canvas);
  93. break;
  94. case Playing:
  95. playingDraw(canvas);
  96. break;
  97. case Over:
  98. overDraw(canvas);
  99. break;
  100. }
  101. }catch (Exception e){
  102. e.printStackTrace();
  103. }finally {
  104. holder.unlockCanvasAndPost(canvas);
  105. }
  106. }
  107. private void menuDraw(Canvas canvas){
  108. paint.setColor(Color.RED);
  109. paint.setTextSize(50);
  110. canvas.drawText("I am in menu.Touch me to next scence",100,100,paint);
  111. }
  112. private void playingDraw(Canvas canvas){
  113. paint.setColor(Color.RED);
  114. paint.setTextSize(50);
  115. canvas.drawText("I am in playing,Slide down to next scence ",100,100,paint);
  116. }
  117. private void overDraw(Canvas canvas){
  118. paint.setColor(Color.RED);
  119. paint.setTextSize(50);
  120. canvas.drawText("I am in over,Slide right to next scence",100,100,paint);
  121. }
  122. //这里就是同步触发机制了
  123. //每一个时钟周期,执行一次逻辑方法和绘图方法
  124. @Override
  125. public void run() {
  126. while(flag){
  127. mLogic();
  128. mDraw();
  129. try {
  130. Thread.sleep(500);
  131. }catch (Exception e){
  132. e.printStackTrace();
  133. }
  134. }
  135. }
  136. //SurfaceView创建时调用
  137. @Override
  138. public void surfaceCreated(SurfaceHolder holder) {
  139. thread.start();
  140. }
  141. //SurfaceView发生改变时调用
  142. @Override
  143. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  144. }
  145. //SurfaceView销毁时调用
  146. @Override
  147. public void surfaceDestroyed(SurfaceHolder holder) {
  148. flag = false;
  149. }
  150. }
复制代码

这只是一个游戏框架,还没有往里写任何内容,但根据我的履历 (固然 我也没有很多履历 ),这里已经包括了全部 游戏(这里特指小游戏-_-)的主体框架代码,有了这个框架,我们就只必要 写游戏的逻辑和绘图方法了,不用纠结怎么搭建游戏框架了。
这里我还加了一个NextEvent的方法,在内里 我封装了上个游戏用到的滑动手势,在这个挨踢圈里,人们最常说的一句话就是:不要重复造轮子。当我们看到写了很多重复代码时,就是我们必要 精简的时间 了。
上代码:

NextEvent.java

  1. package com.next.eatsnake;
  2. /**
  3. * Created by Next on 2016/3/24 0024.
  4. */
  5. public class NextEvent {
  6. public static final int LEFT = 1;
  7. public static final int RIGHT = 2;
  8. public static final int UP = 3;
  9. public static final int DOWN = 4;
  10. public static final int QUIET = -1;//没有滑动
  11. private int dir;
  12. private int downX,downY,upX,upY;
  13. public NextEvent()
  14. {
  15. init();
  16. }
  17. public void init(){
  18. this.dir = QUIET;
  19. this.downX = -1;
  20. this.downY = -1;
  21. this.upX = -1;
  22. this.upY = -1;
  23. }
  24. public int getDir(){
  25. float offsetX,offsetY;
  26. offsetX = upX - downX;
  27. offsetY = upY - downY;
  28. if (Math.abs(offsetX) > Math.abs(offsetY)) {
  29. if (offsetX > 5 ) {
  30. dir = RIGHT;
  31. }else if (offsetX < -5) {
  32. dir = LEFT;
  33. }
  34. }else {
  35. if (offsetY > 5) {
  36. dir = DOWN;
  37. }else if (offsetY < -5) {
  38. dir = UP;
  39. }
  40. }
  41. return dir;
  42. }
  43. public int getDownX() {
  44. return downX;
  45. }
  46. public void setDownX(int downX) {
  47. this.downX = downX;
  48. }
  49. public int getDownY() {
  50. return downY;
  51. }
  52. public void setDownY(int downY) {
  53. this.downY = downY;
  54. }
  55. public int getUpX() {
  56. return upX;
  57. }
  58. public void setUpX(int upX) {
  59. this.upX = upX;
  60. }
  61. public int getUpY() {
  62. return upY;
  63. }
  64. public void setUpY(int upY) {
  65. this.upY = upY;
  66. }
  67. }
复制代码

这个NextEvent是用来存储用户输入变乱 的,我们只是存储,而没有直打仗 发游戏逻辑。那么什么时间 用到读取这个NextEvent呢?假如 你细致 看了第一段代码,应该已经知道了——在每一个时钟周期的逻辑方法里判断 NextEvent,并由此改变游戏逻辑。这种就是同步机制,而用户输入变乱 游戏逻辑就随之改变的就是异步机制。

下面我们用这个框架做一个贪吃蛇游戏,效果 图如下:

这里写图片形貌

这里写图片形貌

这里写图片形貌

MainActivity.java

  1. package com.next.eatsnake;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Window;
  5. import android.view.WindowManager;
  6. public class MainActivity extends Activity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. requestWindowFeature(Window.FEATURE_NO_TITLE);
  11. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  12. setContentView(new GameView(this));
  13. }
  14. }
复制代码

GameView.java

  1. package com.next.eatsnake;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.view.MotionEvent;
  7. import android.view.SurfaceHolder;
  8. import android.view.SurfaceView;
  9. import android.view.View;
  10. import android.view.View.OnTouchListener;
  11. import java.util.ArrayList;
  12. import java.util.Random;
  13. /**
  14. * Created by Administrator on 2016/3/24 0024.
  15. */
  16. public class GameView extends SurfaceView implements SurfaceHolder.Callback,OnTouchListener,Runnable {
  17. enum GameState{
  18. Menu,
  19. Playing,
  20. Over;
  21. }
  22. GameState gameState;//游戏状态
  23. Thread thread;//游戏线程
  24. Boolean flag;//游戏循环控制标志
  25. Canvas canvas;//画布
  26. Paint paint;//画笔
  27. SurfaceHolder holder;//SurfaceView控制句柄
  28. public static Random random;//随机数
  29. NextEvent nextEvent;//游戏输入事件
  30. int scrW,scrH;//屏幕的宽和高
  31. final int MAX_X = 15;
  32. int MAX_Y;//竖向tile数量根据MAX_X动态计算,保证tile是正方形
  33. public static Tile[][] tiles;
  34. Snake snake;
  35. public static boolean isEatFood;
  36. public GameView(Context context) {
  37. super(context);
  38. gameState = GameState.Menu;
  39. flag = true;
  40. paint = new Paint();
  41. paint.setAntiAlias(true);//笔迹平滑
  42. paint.setTextAlign(Paint.Align.CENTER);//文字中间对齐
  43. thread = new Thread(this);
  44. random = new Random();
  45. nextEvent = new NextEvent();
  46. holder = this.getHolder();
  47. holder.addCallback(this);
  48. this.setOnTouchListener(this);
  49. setKeepScreenOn(true);
  50. scrW = ((MainActivity)context).getWindowManager().getDefaultDisplay().getWidth();
  51. scrH = ((MainActivity)context).getWindowManager().getDefaultDisplay().getHeight();
  52. Tile.width = scrW/MAX_X;
  53. MAX_Y = scrH/Tile.width;
  54. tiles = new Tile[MAX_X][MAX_Y];
  55. isEatFood = false;
  56. }
  57. private void newGame(){
  58. for (int x = 0;x < MAX_X;x++){
  59. for (int y = 0;y < MAX_Y;y++){
  60. if (x == 0||y == 0||x == MAX_X-1||y == MAX_Y-1){
  61. tiles[x][y] = new Tile(x,y,Tile.TYPE_WALL);
  62. }else {
  63. tiles[x][y] = new Tile(x,y,Tile.TYPE_NULL);
  64. }
  65. }
  66. }
  67. snake = new Snake(tiles[4][4],tiles[5][4],tiles[6][4], NextEvent.DOWN);
  68. addFood();
  69. addFood();
  70. addFood();
  71. }
  72. public void addFood(){
  73. ArrayList<Tile> nullList = new ArrayList<Tile>();
  74. for (int x = 0;x < MAX_X;x++){
  75. for (int y = 0;y < MAX_Y;y++){
  76. if (tiles[x][y].getType() == Tile.TYPE_NULL){
  77. nullList.add(tiles[x][y]);
  78. }
  79. }
  80. }
  81. if (nullList.size()!=0){
  82. nullList.get(random.nextInt(nullList.size())).setType(Tile.TYPE_FOOD);
  83. }
  84. }
  85. @Override
  86. public boolean onTouch(View v, MotionEvent event) {
  87. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  88. nextEvent.setDownX((int) event.getX());
  89. nextEvent.setDownY((int) event.getY());
  90. }else if (event.getAction() == MotionEvent.ACTION_UP) {
  91. nextEvent.setUpX((int) event.getX());
  92. nextEvent.setUpY((int) event.getY());
  93. }
  94. return true;
  95. }
  96. private void mLogic(){
  97. switch (gameState){
  98. case Menu:
  99. menuLogic();
  100. break;
  101. case Playing:
  102. playingLogic();
  103. break;
  104. case Over:
  105. overLogic();
  106. break;
  107. }
  108. nextEvent.init();//每次逻辑循环后,清空事件
  109. }
  110. private void menuLogic(){
  111. if(nextEvent.getUpX() > 0){
  112. gameState = GameState.Playing;
  113. newGame();
  114. }
  115. }
  116. private void playingLogic(){
  117. if (nextEvent.getDir()!=NextEvent.QUIET){
  118. snake.setDir(nextEvent.getDir());
  119. }
  120. snake.move();
  121. if (isEatFood){
  122. addFood();
  123. isEatFood = false;
  124. }
  125. if(!snake.isAlive()){
  126. gameState = GameState.Over;
  127. }
  128. }
  129. private void overLogic(){
  130. if(nextEvent.getUpX() > 0){
  131. gameState = GameState.Playing;
  132. newGame();
  133. }
  134. }
  135. private void mDraw(){
  136. try {
  137. canvas = holder.lockCanvas();
  138. canvas.drawColor(Color.WHITE);
  139. switch (gameState){
  140. case Menu:
  141. menuDraw(canvas);
  142. break;
  143. case Playing:
  144. playingDraw(canvas);
  145. break;
  146. case Over:
  147. overDraw(canvas);
  148. break;
  149. }
  150. }catch (Exception e){
  151. e.printStackTrace();
  152. }finally {
  153. holder.unlockCanvasAndPost(canvas);
  154. }
  155. }
  156. private void menuDraw(Canvas canvas){
  157. paint.setColor(Color.BLACK);
  158. paint.setTextSize(50);
  159. canvas.drawText("Eat Snake,Touch me and start",scrW/2,scrH/2,paint);
  160. }
  161. private void playingDraw(Canvas canvas){
  162. for (int x = 0;x < MAX_X;x++){
  163. for (int y = 0;y < MAX_Y;y++){
  164. tiles[x][y].draw(canvas,paint);
  165. }
  166. }
  167. }
  168. private void overDraw(Canvas canvas){
  169. paint.setColor(Color.BLACK);
  170. paint.setTextSize(50);
  171. canvas.drawText("Your score is:"+snake.getLength(),scrW/2,scrH/4,paint);
  172. canvas.drawText("Touch me and try again",scrW/2,scrH/2,paint);
  173. }
  174. @Override
  175. public void run() {
  176. while(flag){
  177. mLogic();
  178. mDraw();
  179. try {
  180. Thread.sleep(500);
  181. }catch (Exception e){
  182. e.printStackTrace();
  183. }
  184. }
  185. }
  186. @Override
  187. public void surfaceCreated(SurfaceHolder holder) {
  188. thread.start();
  189. }
  190. @Override
  191. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  192. }
  193. @Override
  194. public void surfaceDestroyed(SurfaceHolder holder) {
  195. flag = false;
  196. }
  197. }
复制代码

Tile.java

  1. package com.next.eatsnake;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.graphics.Paint;
  5. /**
  6. * Created by Next on 2016/3/26 0026.
  7. */
  8. public class Tile {
  9. public static final int TYPE_NULL = 0;//空
  10. public static final int TYPE_WALL = 1;//墙
  11. public static final int TYPE_HEAD = 2;//蛇头
  12. public static final int TYPE_BODY = 3;//蛇身
  13. public static final int TYPE_TAIL = 4;//蛇尾
  14. public static final int TYPE_FOOD = 5;//食物
  15. private int x,y;
  16. private int type;
  17. public static int width;
  18. public Tile(int x, int y, int type) {
  19. this.x = x;
  20. this.y = y;
  21. this.type = type;
  22. }
  23. public void draw(Canvas canvas,Paint paint){
  24. switch (type){
  25. case TYPE_NULL:
  26. break;
  27. case TYPE_WALL:
  28. paint.setColor(Color.BLACK);
  29. canvas.drawCircle(x*width+width/2,y*width+width/2,width/2,paint);
  30. break;
  31. case TYPE_HEAD:
  32. paint.setColor(Color.MAGENTA);
  33. canvas.drawCircle(x*width+width/2,y*width+width/2,width/2,paint);
  34. paint.setColor(Color.WHITE);
  35. canvas.drawCircle(x*width+width/2,y*width+width/2,width/8,paint);
  36. break;
  37. case TYPE_BODY:
  38. paint.setColor(Color.MAGENTA);
  39. canvas.drawCircle(x*width+width/2,y*width+width/2,width/2,paint);
  40. break;
  41. case TYPE_TAIL:
  42. paint.setColor(Color.MAGENTA);
  43. paint.setStrokeWidth(10);
  44. canvas.drawLine(x * width, y * width + width / 2, x * width + width / 2, y * width, paint);
  45. canvas.drawLine(x*width+ width / 2,y*width,x*width+width,y*width+width/2,paint);
  46. canvas.drawLine(x*width+width,y*width+width/2,x*width+width/2,y*width+width,paint);
  47. canvas.drawLine(x*width+width/2,y*width+width,x*width,y*width+ width / 2,paint);
  48. break;
  49. case TYPE_FOOD:
  50. switch (GameView.random.nextInt(3)){
  51. case 0:
  52. paint.setColor(Color.YELLOW);
  53. break;
  54. case 1:
  55. paint.setColor(Color.GREEN);
  56. break;
  57. case 2:
  58. paint.setColor(Color.CYAN);
  59. break;
  60. }
  61. canvas.drawCircle(x*width+width/2,y*width+width/2,width/2,paint);
  62. break;
  63. }
  64. }
  65. public int getX() {
  66. return x;
  67. }
  68. public void setX(int x) {
  69. this.x = x;
  70. }
  71. public int getY() {
  72. return y;
  73. }
  74. public void setY(int y) {
  75. this.y = y;
  76. }
  77. public int getType() {
  78. return type;
  79. }
  80. public void setType(int type) {
  81. this.type = type;
  82. }
  83. }
复制代码

Snake.java

  1. package com.next.eatsnake;
  2. import java.util.ArrayList;
  3. /**
  4. * Created by Administrator on 2016/3/26 0026.
  5. */
  6. public class Snake {
  7. private ArrayList<Tile> snake;
  8. private int dir;
  9. private boolean isAlive;
  10. public Snake(Tile head,Tile body,Tile tail,int dir){
  11. snake = new ArrayList<Tile>();
  12. head.setType(Tile.TYPE_HEAD);
  13. body.setType(Tile.TYPE_BODY);
  14. tail.setType(Tile.TYPE_TAIL);
  15. snake.add(head);
  16. snake.add(body);
  17. snake.add(tail);
  18. isAlive = true;
  19. this.dir = dir;
  20. }
  21. public void move(){
  22. if (!isAlive)
  23. return;
  24. switch (dir){
  25. case NextEvent.LEFT:
  26. switch (GameView.tiles[snake.get(0).getX()-1][snake.get(0).getY()].getType()){
  27. case Tile.TYPE_WALL:
  28. case Tile.TYPE_BODY:
  29. case Tile.TYPE_TAIL:
  30. isAlive = false;
  31. break;
  32. case Tile.TYPE_FOOD:
  33. GameView.tiles[snake.get(0).getX()-1][snake.get(0).getY()].setType(Tile.TYPE_HEAD);
  34. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  35. snake.add(0, GameView.tiles[snake.get(0).getX() - 1][snake.get(0).getY()]);
  36. GameView.isEatFood = true;
  37. break;
  38. case Tile.TYPE_NULL:
  39. GameView.tiles[snake.get(0).getX()-1][snake.get(0).getY()].setType(Tile.TYPE_HEAD);
  40. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  41. snake.add(0, GameView.tiles[snake.get(0).getX() - 1][snake.get(0).getY()]);
  42. snake.get(snake.size()-1).setType(Tile.TYPE_NULL);
  43. snake.remove(snake.size()-1);
  44. snake.get(snake.size()-1).setType(Tile.TYPE_TAIL);
  45. break;
  46. }
  47. break;
  48. case NextEvent.RIGHT:
  49. switch (GameView.tiles[snake.get(0).getX()+1][snake.get(0).getY()].getType()){
  50. case Tile.TYPE_WALL:
  51. case Tile.TYPE_BODY:
  52. case Tile.TYPE_TAIL:
  53. isAlive = false;
  54. break;
  55. case Tile.TYPE_FOOD:
  56. GameView.tiles[snake.get(0).getX()+1][snake.get(0).getY()].setType(Tile.TYPE_HEAD);
  57. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  58. snake.add(0, GameView.tiles[snake.get(0).getX() + 1][snake.get(0).getY()]);
  59. GameView.isEatFood = true;
  60. break;
  61. case Tile.TYPE_NULL:
  62. GameView.tiles[snake.get(0).getX()+1][snake.get(0).getY()].setType(Tile.TYPE_HEAD);
  63. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  64. snake.add(0, GameView.tiles[snake.get(0).getX() + 1][snake.get(0).getY()]);
  65. snake.get(snake.size()-1).setType(Tile.TYPE_NULL);
  66. snake.remove(snake.size() - 1);
  67. snake.get(snake.size()-1).setType(Tile.TYPE_TAIL);
  68. break;
  69. }
  70. break;
  71. case NextEvent.UP:
  72. switch (GameView.tiles[snake.get(0).getX()][snake.get(0).getY()-1].getType()){
  73. case Tile.TYPE_WALL:
  74. case Tile.TYPE_BODY:
  75. case Tile.TYPE_TAIL:
  76. isAlive = false;
  77. break;
  78. case Tile.TYPE_FOOD:
  79. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()-1].setType(Tile.TYPE_HEAD);
  80. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  81. snake.add(0, GameView.tiles[snake.get(0).getX()][snake.get(0).getY()-1]);
  82. GameView.isEatFood = true;
  83. break;
  84. case Tile.TYPE_NULL:
  85. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()-1].setType(Tile.TYPE_HEAD);
  86. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  87. snake.add(0, GameView.tiles[snake.get(0).getX()][snake.get(0).getY()-1]);
  88. snake.get(snake.size()-1).setType(Tile.TYPE_NULL);
  89. snake.remove(snake.size()-1);
  90. snake.get(snake.size()-1).setType(Tile.TYPE_TAIL);
  91. break;
  92. }
  93. break;
  94. case NextEvent.DOWN:
  95. switch (GameView.tiles[snake.get(0).getX()][snake.get(0).getY()+1].getType()){
  96. case Tile.TYPE_WALL:
  97. case Tile.TYPE_BODY:
  98. case Tile.TYPE_TAIL:
  99. isAlive = false;
  100. break;
  101. case Tile.TYPE_FOOD:
  102. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()+1].setType(Tile.TYPE_HEAD);
  103. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  104. snake.add(0, GameView.tiles[snake.get(0).getX()][snake.get(0).getY() + 1]);
  105. GameView.isEatFood = true;
  106. break;
  107. case Tile.TYPE_NULL:
  108. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()+1].setType(Tile.TYPE_HEAD);
  109. GameView.tiles[snake.get(0).getX()][snake.get(0).getY()].setType(Tile.TYPE_BODY);
  110. snake.add(0, GameView.tiles[snake.get(0).getX()][snake.get(0).getY()+1]);
  111. snake.get(snake.size()-1).setType(Tile.TYPE_NULL);
  112. snake.remove(snake.size()-1);
  113. snake.get(snake.size()-1).setType(Tile.TYPE_TAIL);
  114. break;
  115. }
  116. break;
  117. }
  118. }
  119. public void setDir(int dir){
  120. if (this.dir == dir||this.dir == -dir||dir == NextEvent.QUIET)
  121. return;
  122. else
  123. this.dir = dir;
  124. }
  125. public boolean isAlive(){
  126. return isAlive;
  127. }
  128. public int getLength(){
  129. return snake.size();
  130. }
  131. }
复制代码

NextEvent.java

就是刚开始先容 的谁人 类,这里就不重复贴出了

到此这篇关于Android 2d游戏开辟 之贪吃蛇基于surfaceview的文章就先容 到这了,更多干系 Android 贪吃蛇内容请搜索 脚本之家从前 的文章或继续欣赏 下面的干系 文章渴望 大家以后多多支持脚本之家!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

avatar 123457814 | 2021-9-14 13:05:10 | 显示全部楼层
看在admin楼主的面子上,认真回帖!
回复

使用道具 举报

avatar 我就是但丁徊 | 2021-9-19 05:00:03 | 显示全部楼层
admin楼主是在找骂么?
回复

使用道具 举报

avatar 不忘初心xrl | 2021-9-25 08:43:07 | 显示全部楼层
大神就是大神,这么经典!
回复

使用道具 举报

avatar FREEDOM326 | 2021-9-25 08:43:10 | 显示全部楼层
admin楼主是一个典型的文艺青年啊!
回复

使用道具 举报

avatar 永远丶并不远 | 2021-9-26 23:31:46 | 显示全部楼层
好东西,赞一个!
回复

使用道具 举报

avatar 保佑缎 | 2021-9-27 11:45:42 | 显示全部楼层
楼上的心情不错啊!
回复

使用道具 举报

avatar 静美人2017 | 2021-10-4 10:06:38 | 显示全部楼层
admin楼主,我告诉你一个你不知道的的秘密,有一个牛逼的网站,影视频道的网站所有电影和连续剧都可以免费看的。访问地址:http://tv.mxswl.com
回复

使用道具 举报

avatar 楼琴观雪让 | 2021-10-12 05:06:05 | 显示全部楼层
一口气看完了,我要下去回味回味了!
回复

使用道具 举报

avatar 忆清静丫 | 2021-10-12 05:06:08 | 显示全部楼层
admin楼主,我告诉你一个你不知道的的秘密,有一个牛逼的源码论坛他的站点都是商业源码,还是免费下载的那种!特别好用。访问地址:http://www.mxswl.com 猫先森网络
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则