Python implements Snake War
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
