Python realizes greedy snake big battle code sharing _python

Source: Internet
Author: User

Feeling game audit after the implementation of the New deal, the domestic market slightly deserted, is not the latest games are in line and other audit. The media, in addition to the previous scramble to "Pokemon go" lively, it seems that there is no sound. Until the last few days, suddenly heard several people have mentioned the same game, online there are people said that the circle of friends was it painted screen. (But now the micro-letter has blatantly blocked its share)

The game is now the first of the iOS free list of "greedy snake big fight." A simple to no game, also do not know how the fire. Anyway, a game of fire, the media, experts can always say a variety of routines, so I do not give an opinion. But this is a very good implementation of the game, so a moment itched, with Python made a realization.

"Moving Diagram"

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

Github-crossin/gluttonous:game of gluttonous python (imitation "snake Big Fight")

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

Pip Install cocos2d

Some of the difficulties in code implementation:

Control of the direction of motion. Keyboard only up and down about four keys, to convert to 360 degrees of movement direction, need to use a lot of trigonometric operations.

The handling of the snake body. Here I used a path list to record the position of the snake's head, while the body updates the location according to the corresponding data in the path.

The motion strategy of the computer. The way I use this is, for other snakes in the head range, to calculate the angle between the snake body and the snake head, and the direction of their own movement, if the angle difference is very small, it means that will hit, so adjust the current direction of motion.

Operating efficiency. The biggest efficiency bottleneck is the picture drawing on the screen. At the beginning of the completion of the version, the total number of snakes on the field reached about 300 cotton is serious. Later, the use of Cocos to replace the Batchnode directly added, greatly reducing the number of pictures to draw, to ensure the smooth running of the game. But if you play until the minute is very high, there will still be efficiency problems, not resolved for the time being.

In all fairness, Python is not the right place to do business games, and it's a good choice to learn or develop prototypes. Now the popular mobile game development engine Cocos2d-x, originally from the Python version, also on my use of the cocos2d library. Although there is a certain gap in the function, but the engine structure is very close, including the scene and layer concepts, actions, events, refreshes and so on.

Just a whim, write and write, the code is not how to optimize the annotation is basically not. Let's have a look. If you have more people to praise and forward, you will consider continuing optimization and a series of tutorials.

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.