Snake series ten - game map

        In the previous project, we already have the object of the stone, but the problem is that we only draw one stone, which is not the end result we want, so, in this version, we will introduce a new class, GameMap, again, create this class and put it in our com.gulang.snake.entity package. The following is the code of our game map, please see:

package com.gulang.snake.entity;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

/**
 * 游戏的地图
 * @author [email protected]
 *
 */
public class GameMap {
	
	/** 地图数据 */
	private int[][] mapData = new int[][]{
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
	};
	/** 装游戏中石头的集合 */
	private List<Stone> stones = new ArrayList<Stone>();
	
	/**
	 * 游戏地图的构造方法
	 */
	public GameMap(){
		for(int row = 0; row < mapData.length; row++){
			for(int col = 0; col < mapData[row].length; col++){
				if(mapData[row][col] == 1){
					stones.add(new Stone(col * Stone.SIZE, row * Stone.SIZE));
				}
			}
		}
	}
	
	/**
	 * 绘制游戏的地图
	 * @param g
	 */
	public void drawMe(Graphics g){
		for(Stone stone : stones){
			stone.drawMe(g);
		}
	}

	public List<Stone> getStones() {
		return stones;
	}
	
}
        Here, let me simply explain what our game map is all about. In our game development, the map of the game is a very important part. Usually in our game development, our game development engine will provide us with a map editor. I think everyone is very clear about the reason. Our code above. In our game, different materials will have different properties. For example, in our game, stones and food are two very similar objects with different properties. The properties of these objects are meaningful to us, but to the computer, they are the same. So in order to let the computer know that these are two different objects, we must first make some conventions. For example, in our map object, we agree that 0 means nothing, and 1 means stone. Therefore, the map editor will provide us with all the legal materials in our game, and we will use these materials to assemble a map of the game. The final result is a multi-dimensional array, which is the mapData array in our GameMap class. Of course, the environment here is very simple, so we can write it directly in the form of a two-dimensional array. In a real game, the terrain can be designed to be very complex. At that time, we can directly use 1, 2, 3 and 4 are more difficult to construct, so our game engine must have a visual map editor for us to use. Interested students can write a map editor for our snake-eating map editor, here we are I won't discuss this further.
        Well, this class is also relatively simple, mainly responsible for the creation and drawing of maps. The only thing you need to pay attention to is that when we construct the map, we should pay attention to the position of row and col when setting the coordinates of the stone. The coordinate conversion of this place should be careful. Of course, it doesn't matter if you do it wrong. After running it, you can find that it is not the effect we want and you can go back and change it. I think everyone who sees the structure of this array also knows that we have placed stones around the game window. The result obtained after running is as follows:

        As for why we put two rows of stones on the top, it is because our game window The upper part is covered by the title bar of the window. In order to let everyone see the effect, I set up two rows.
        Then after we have our game map, we don't need to draw stones directly. Therefore, we remove the reference to Stone in GameView and change it to a reference to GameMap. At the same time, we need to modify the isEatStone() method in the Snake class. The modified method is as follows:
        /**
	 * 判断蛇是否吃到了石头
	 * @return		如果蛇吃到了石头则返回true,否则返回false
	 */
	public boolean isEatStone(){
		Body head = snakeBody.getFirst();
		List<Stone> stones = gameView.getGameMap().getStones();
		for(Stone stone : stones){
			if(head.x == stone.getX() && head.y == stone.getY()){
				return true;
			}
		}
		return false;
	}
        I will not explain this code, I think everyone understands it. So here, even if our version of Snake is complete, run it and see, um, it is quite similar to the Snake game in our memory. But after running for a while, we will find that our food sometimes runs to the stone, which is obviously unreasonable. Therefore, we add the following method to the Food class to ensure that the food will not be generated when it is generated. Run to the stone:
        /**
	 * 判断食物是否出现在石块上
	 * @return	如果食物出现在石块上则返回true,否则返回false
	 */
	public boolean isOnStones(){
		List<Stone> stones = gameView.getGameMap().getStones();
		for(Stone stone : stones){
			if(x == stone.getX() && y == stone.getY()){
				return true;
			}
		}
		return false;
	}
        I don't think I need to explain more about this code, and then we can add a call to this method in the constructor of the Food class.
do{
	createXY(r, cellsInRow, cellsInCol);
} while (isInSnakeBody(snake) || isOnStones());
        Well, at this point, our snake-eating project has basically been completed. After running it, there are basically no major problems. Finally, there are some other problems that we have not yet dealt with. For example, our snakes can now pass through their own bodies, which is obviously not possible. We will solve this problem in the next project. At the same time, we also want to make a summary of this small project in a project. After all, in addition to doing, we need to reflect and summarize more, so that we can progress faster. So, see you on the next project.
        Also, source files for this and previous projects: http://kuai.xunlei.com/d/Ae2cA1x2zAzfUAQA7a7

Guess you like

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