Imagine a snake game where there is a lot of food on the map. The program needs to check whether the snake head is in conflict with the food (based on the coordinates on the map ). If you use an array to save the food information, you need to traverse this array. But if there is a lot of food (the array is very large), it is unnecessary to traverse it completely, because there is only one... imagine a snake game where there is a lot of food on the map. The program needs to check whether the snake head is in conflict with the food (based on the coordinates on the map ).
If you use an array to save the food information, you need to traverse this array. However, if there are a lot of foods (the array is very large), it is unnecessary to traverse them completely, because only a certain range of foods may crash.
The problem is how to locate the food that may crash without traversing the entire array based on the coordinates.
What I can think of is to use array_filter, but basically it is traversal.
You can also use other data structures (without arrays.
Friends who have played games may have good solutions.
Reply content:
Imagine a snake game where there is a lot of food on the map. The program needs to check whether the snake head is in conflict with the food (based on the coordinates on the map ).
If you use an array to save the food information, you need to traverse this array. However, if there are a lot of foods (the array is very large), it is unnecessary to traverse them completely, because only a certain range of foods may crash.
The problem is how to locate the food that may crash without traversing the entire array based on the coordinates.
What I can think of is to use array_filter, but basically it is traversal.
You can also use other data structures (without arrays.
Friends who have played games may have good solutions.
Redis sort set type can meet your needs http://redis.readthedocs.io/e...
I think it looks like this:
-
First, the food in the greedy snake is a ball, and the array of food should be saved <坐标, 半径> , Map can be used in C ++;
-
Then, you can open an array called Map. The size of the array is w of the Map.H. suppose there is such a food <(x, y), r>, then Map needs to be assigned the value: Map [wX + y] = r; record a maxR item, representing the maximum radius of food, maxR = max (r ).
-
Finally, determine the collision and set the position of the snake header to (a, B). What you need to do is traverse to (a, B) take all coordinates (x, y) of the circle with the center radius of maxR, and then retrieve the r = Map [w * x + y], with now_r = dis (a, B ), (x, y) in comparison, if r> = now_r, it means that the snake will eat this food. if it is smaller than it, it means it cannot be eaten. continue to judge the next coordinate.
-
In fact, I guess the actual process is to traverse all the food. after all, there are not so many snake foods.