Python implements Snake War

高洛峰
Release: 2017-02-27 09:59:20
Original
1784 people have browsed it

This article shares with you a game code for Snake Battle produced using the cocos2d-python game engine library. It is based on Python 2.7 and cocos2d library. Friends in need can refer to it

Python implements Snake War

I feel that after the implementation of the new game review policy, the domestic mobile game market has become slightly deserted. Are all new games from various companies queuing up for review? In addition to the previous excitement about "Pokemon Go", the media seems to have heard nothing. Until the past few days, I suddenly heard several people mentioning the same game, and some people online said that their circle of friends was blocked by it. (However, WeChat has now blatantly blocked its sharing)

This game is "Snake Fight" which is currently ranked No. 1 on the iOS free list. A ridiculously simple game, but I don’t know how it became popular. Anyway, when a game becomes popular, various media and experts will always come up with all kinds of tricks, so I won’t express my opinion. However, this is really a very easy game to implement, so I was inspired to implement it in Python.

【Animation】

Python implements Snake War

demo It took about a whole day to achieve the basic effect (no acceleration). The code has been uploaded to GitHub:

GitHub - crossin/gluttonous: game of gluttonous python

uses cocos2d-python as the game engine. If you want to run the code, you need to install Python 2.7 and the cocos2d library.

pip install cocos2d
Copy after login

Several difficulties in code implementation:

Control of movement direction. There are only four keys on the keyboard: up, down, left, and right. To convert to a 360-degree movement direction, a lot of trigonometric functions are required.

Handling of snake body. Here I use a path list to record the location where the snake's head passes, and the body updates its location based on the corresponding data in the path.

Computer sports strategy. The method I use here is to calculate the angle between the snake body and the snake head for other snake bodies within the head range, and compare it with its own movement direction. If the angle difference is very small, it means that it will collide, so Adjust the current movement direction.

operating efficiency. The biggest efficiency bottleneck is drawing pictures on the screen. In the first completed version, when the total number of snakes on the field reached about 300, it became severely lagging. Later, Cocos' BatchNode was used instead of adding it directly, which greatly reduced the number of picture drawings and ensured the smooth running of the game. But if you play to very high minutes, there will still be efficiency problems, which have not been solved yet.

To be fair, Python is not suitable for commercial games, but it is a good choice for learning or developing prototypes. The now very popular mobile game development engine cocos2d-x was originally derived from the Python version, which is the cocos2d library I used this time. Although there is a certain gap in functionality, the engine structure is very close, including the concepts of scenes and layers, actions, events, refreshes, etc.

I just wrote it on a whim, the code is not optimized, and there are basically no comments. Let's take a look. If there are many likes and reposts, we will consider continuing to optimize and make a series of tutorials.

For more articles related to Python’s implementation of Snake War, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!