Snake series eight - I am a foodie and I am proud

        In the previous project, we have drawn food on our game interface, so in this project, we will eat this food. Well, let's look at the code first:


        /**
	 * 判断蛇有没有吃到食物的方法
	 * @param food	食物对象
	 * @return		如果蛇吃到食物则返回true,否则返回false
	 */
	public boolean isEatFood(){
		Body head = snakeBody.getFirst();
		Food food = gameView.getFood();
		if(head.x == food.getX() && head.y == food.getY()){
			return true;
		}
		return false;
	}
	
	/**
	 * 蛇吃掉食物的方法
	 */
	public void eatFood(){
		//让原来的食物消失,并且产生一个新的食物
		gameView.setFood(new Food(this));
	}
In order to complete the logic of the snake eating food, we added two methods to the Snake class, which are the above two methods. IsEatFood is the method we use to judge whether the snake has eaten food. I think everyone can understand the logic at a glance. The eatFood method is to handle the logic that the snake actually eats the food after it touches the food. Here, everyone found out, I reference a new object, gameView. In fact, this object is not difficult to understand, it is the window in which we run the game, or more generally, it is called the running environment of the game. Because here, the food object we need to deal with is actually in our game environment, so we need to introduce this object. For this reason, we assign a value to this object when constructing the snake object. Therefore, we slightly modify the Snake construction method, the code is as follows:

       /** 游戏的运行环境对象 */
       private GameView gameView;

       /**
	 * 初始化一条蛇
	 */
	public Snake(GameView gameView){
		this.gameView = gameView;
		//初始化蛇最开始前进的方向为向右
		direction = DIR_RIGHT;
		//我们将蛇的第一节身体初始化在游戏窗口的中央
		int cellsInRow = GameView.WINDOW_WIDTH / BODY_SIZE;
		int cellsInCol = GameView.WINDOW_HEIGHT / BODY_SIZE;
		int startX = cellsInRow / 2 * BODY_SIZE;
		int startY = cellsInCol / 2 * BODY_SIZE;
		//初始化蛇的身体,即向body链表中添加数据,我们初始化蛇的初始节点为3个
		for(int i = 0; i < 3; i++){
			//逐个的计算出蛇的每一个节点的位置
			Body body = new Body(startX - i * BODY_SIZE, startY);
			snakeBody.add(body);
		}
	}
        If our code only writes this, you will find that the food disappears after the snake touches the food, but our snake does not grow, so we still need to deal with this thing. Where to deal with it, the answer is Snake's move method, so we modify Snake's move method as follows:
       /**
	 * 蛇移动的方法
	 */
	public void move(){
		//1.去尾。这个很简单,意思就是说去掉我们snakeList的最后一个元素
		if(isEatFood()){
			eatFood();
		} else {
			snakeBody.removeLast();
		}
              //......下面的代码没有变
        }
The key is that we added a judgment to the snake's move method. To make the snake grow longer, we just don't remove the tail when the snake eats food.
        So far, our version is done, let's try it out, our snake can already eat food. In the next version, we started coding the stones in the game. Okay, here we go, see you on the next project.
        The following is the source code from the first project to this project: http://kuai.xunlei.com/d/Ae2cA3pyNczeUAQA93a

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325445536&siteId=291194637