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
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】
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
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!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.