Snake series six - turn

      In the last version, our snake can already move in the game window, but this one will not come, so in this version, we will fix this bug first, we let our snake have the ability to penetrate , that is, running out of the screen from which side, and running back from the other side. Let's look at our key code first:


        /**
	 * 蛇移动的方法
	 */
	public void move(){
		//1.去尾。这个很简单,意思就是说去掉我们snakeList的最后一个元素
		snakeBody.removeLast();
		
		//2.加头。这个就相对复杂一点,需要我们根据蛇当前运动的方向来判断我们的蛇头应该加在什么位置
		Body head = snakeBody.getFirst();//获得当前蛇头的那个对象
		int newX = head.x;//我们要添加的蛇头的横坐标
		int newY = head.y;//我们要添加的蛇头的纵坐标
		switch(direction){
		case DIR_UP:
			//如果蛇跑出了上边界我们就让蛇从下边跑出来
			if(head.y <= 0){
				newY = GameView.WINDOW_HEIGHT - BODY_SIZE;
			} else {
				newY -= BODY_SIZE;
			}
			break;
		case DIR_DOWN:
			//如果蛇跑出了下边界我们就让蛇从上边跑出来
			if(head.y >= GameView.WINDOW_HEIGHT - BODY_SIZE){
				newY = 0;
			} else {
				newY += BODY_SIZE;
			}
			break;
		case DIR_LEFT:
			//如果蛇跑出了左边界我们就让蛇从右边跑出来
			if(head.x <= 0){
				newX = GameView.WINDOW_WIDTH - BODY_SIZE;
			} else {
				newX -= BODY_SIZE;
			}
			break;
		case DIR_RIGHT:
			//如果蛇跑出了右边界我们就让蛇从左边跑出来
			if(head.x >= GameView.WINDOW_WIDTH - BODY_SIZE){
				newX = 0;
			} else {
				newX += BODY_SIZE;
			}
			break;
		}
		snakeBody.addFirst(new Body(newX, newY));
	}
We need to modify the move method of the snake, we need to judge the current position of the snake head, and selectively calculate the position of the next snake head according to the critical position of our snake head, which should not be difficult to understand.
      Then, we have to make our snake obey our command, we tell it to turn left and it will turn left, not right. Then we will add a method to the snake, changeDirection(). In this method, we replace the original direction of the snake's movement with the new direction to complete the snake's direction change. Here is the code:
        /**
	 * 改变蛇的运动方向
	 * @param direction	蛇的新的运动方向
	 */
	public void changeDirection(int direction){
		this.direction = direction;
	}
      With the method of changing direction, the next thing we have to do is to monitor the event of the user pressing the keyboard in the GameView. Here, we use the four arrow keys on the keyboard to control the movement of the snake. Of course, you Other keys can also be selected. Then, to realize the monitoring of the PC keyboard, in our java, we need to implement an interface called KeyListener. There is a very important concept here - interface. Lone Wolf briefly talks about the concept of interface here. Everyone needs to understand what an interface is. You can imagine our plug-in board. Are there many two-hole or three-hole sockets on the plug-in board? Those are equivalent to our interfaces. What is the function of the interface? Take the power strip as an example. You need to use an electrical appliance to plug it into a specific jack in the power strip. You can't say that a plug with four corners is inserted into the power strip. into the three-hole jack. Therefore, the interface is a series of specifications (specifically, methods) defined by our developers. What programs will use these methods in the future We don't know when we build the patch panel. But as long as the plug of your electrical appliance meets the style (that is, the specification) that I gave you in the future, then your electrical appliance (program) can run normally. Students who don't understand can only go to check the information, because the concept of interface is very important and is one of the cores of our object-oriented.
      Let's continue to talk about the interface we want to implement, KeyListener. In java, the way to implement an interface is to use the keyword implements after our class name and add the name of the interface we want to implement. Then when we implement this interface, Eclipse will report an error because we haven't implemented the methods in KeyListener yet. Using Eclipse's automatic repair function, we implement those methods:
        /**
	 * 键盘被按下的事件
	 */
	@Override
	public void keyPressed(KeyEvent e) {
		
	}

	/**
	 * 键盘弹起的事件
	 */
	@Override
	public void keyReleased(KeyEvent e) {
		switch(e.getKeyCode()){
		case KeyEvent.VK_LEFT://向左
			snake.changeDirection(Snake.DIR_LEFT);
			break;
		case KeyEvent.VK_RIGHT://向右
			snake.changeDirection(Snake.DIR_RIGHT);
			break;
		case KeyEvent.VK_UP://向上
			snake.changeDirection(Snake.DIR_UP);
			break;
		case KeyEvent.VK_DOWN://向下
			snake.changeDirection(Snake.DIR_DOWN);
			break;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
These three methods are defined in the interface, and our virtual machine will call the corresponding methods to handle keyboard events after listening to the user pressing the keyboard. We choose one of the events that pops up when the keyboard is released to implement our operation of changing the direction of the snake.
      At this point, our version is basically complete. We can control our snake through the keyboard. Does it feel like we are getting closer to the goal? Ha ha. However, there is still a small bug. Now our snake can go back. Oh, what does it mean? It turns out that my snake is running to the right. I can press the left button to make the snake run to the left. This is in the game. It is not allowed inside, we will fix this small bug in the next version. I have uploaded all the code on Xunlei Express. Let’s go and tell everyone. Here is a link. A blog post corresponds to a project file. You can directly import the project and run it:  http://kuai.xunlei.com/d /nNdkCTf2DCjTUAQA450
     


Guess you like

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