Python implements code sharing for battle against snakes and snakes
I feel that after the implementation of the new game review policy, the domestic mobile game market is a little deserted. Are all new games waiting for review. In addition to competing for Pokemon Go, the media seems to have heard nothing. It wasn't until recently that several people suddenly talked about the same game, and some people on the internet showed that their moments had been flushed. (However, the sharing has been blocked)
This game is now the top game in the free iOS rankings. A game that is as simple as no one knows how to get angry. The opposite is a game, and various media and experts can always come up with various routines, so I will not comment on it. However, this is really a very good implementation of the game, so it was a bit tricky to use Python for implementation.
[Animation]
The demo took about a whole day to achieve the basic effect (no acceleration ). The Code has been uploaded to GitHub:
GitHub-crossin/glutonous: game of glutonous python
The cocos2d-python is used as the game engine. To run the code, install the Python 2.7 and cocos2d libraries.
pip install cocos2d
Several difficulties in code implementation:
Control the movement direction. On the keyboard, there are only four keys at the top, right, and right. to convert them to a motion direction of 360 degrees, many trigonometric operations are required.
Processing of snakes. Here, I used a path list to record the position of the Snake head. The body updates the position based on the corresponding data in the path.
The computer's motion strategy. Here, I use the method to calculate the angle of the line between the snake body and the Snake Head in the header range and compare it with the direction of its own motion. If the angle difference is small, it means it will hit, so the current movement direction is adjusted.
Operation Efficiency. The biggest efficiency bottleneck lies in the rendering of images on the screen. In the first version, the total number of snakes on the market reached about 300, and it was very slow. Later, cocos BatchNode was used as an alternative to direct addition, which greatly reduced the number of image plotting times and ensured smooth operation of the game. However, if you have been playing for a long time, there will still be efficiency issues, which have not been solved for the time being.
In all fairness, Python is not suitable for commercial games, but it is also a good choice for learning or developing prototypes. Now very popular mobile game development engine cocos2d-x, initially originated from the Python version, I am using the cocos2d library. Although there are some functional gaps, the engine structure is very close, including the concept, action, event, and refresh of the scenario and layer.
It's just a whim that allows you to write and write at will. The code is not optimized, and there are basically no comments. Let's take a look. If there are many people to like and forward, we will consider further optimization and create a series of tutorials.