Python to realize the big battle of Snake

Source: Internet
Author: User
This article to share is a use of Cocos2d-python game Engine Library made out of the snake game code, based on the Python 2.7 and cocos2d library, the need for small partners can refer to the next

Feel the game audit after the implementation of the New Deal, the domestic hand tour market slightly deserted, is not the latest games are in line and other audit. The media, in addition to the previous race to chase "Pokemon Go" lively, it seems to hear no sound. Until the last few days, suddenly heard a few people have mentioned the same game, there are people on the Internet to say that the circle of friends have been brush screen. (But now it's been blatantly blocked from sharing)

This game is now the first in the free list of ios "Snake Big Battle". A simple to no game, do not know how the fire. Anyway, a game fire, the media, experts always say all kinds of routines, so I will not express their views. But this is a very good realization of the game, so a moment kungfu, take Python to do a realization.

"Motion Diagram"

The demo 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

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

Pip Install cocos2d

Several difficulties in implementing the Code:

Control of the direction of motion. On the keyboard only up and down four keys, to convert to 360 degrees of the direction of movement, it is necessary to use a lot of trigonometric operations.

The handling of the snake body. Here I use a list of paths to record where the snake's head is, and the body updates the position based on the corresponding data in the path.

The motion strategy of the computer. Here I use the method is, for the other body in the head range, the calculation of the snake body and the snake Head connection angle, and its own direction of movement comparison, if the angle difference is very small, it means that will hit, so adjust the current direction of movement.

Operational efficiency. The biggest efficiency bottleneck is the picture on the screen. The beginning of the finished version, the total number of snakes on the field reached about 300 of the lag serious. Later with the Cocos Batchnode replace the direct add, greatly reduce the number of pictures drawn, can ensure smooth operation of the game. But if the play to the minute is very high, there will still be efficiency problems, temporarily not resolved.

In all fairness, Python is not suitable for business games, and it is a good choice to learn or develop prototypes. Now the popular mobile game development engine Cocos2d-x, originally from the Python version, but also the cocos2d library I used this time. Although there is a certain gap in functionality, the engine structure is very close, including the concept of scenes and layers, actions, events, refreshes and so on.

Just a whim, write at hand, the code is not how to optimize, the comments are basically not. Let's have a look. If you like, forward more people, then you will consider continuing to optimize, and dedicated to a series of tutorials.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.