Snake eating , Everyone should have played . When Xiaobian first came into contact with snake
, Or can it hit a walnut , Did you have a good time at that time . today , We use Python Programming a snake game , Let's see the effect first :

okay , Let's start with an idea

The main content of all the Games is the internal circulation of the program , This is the premise to ensure the normal operation of a game .

The following is the main idea of writing Snake games .

No more nonsense , Let's talk about how to use it Python Let's write the greedy snake

one , Call library and initial settings

1. Call the third party Library

Python Unlike other languages, it has many third-party libraries that can be called . stay Python When writing a game ,pygame Is a very simple start of the third-party library , Can pass pip Direct installation . The installation method has been described in the previous article , I will not repeat it . Want to know more pygame Function friends can also refer to the official document .

This is the library we need to call when writing snake .

2 . Initial setup

We use these lines of code to initialize pygame, define window ( boundary ) The size of , The title and icon of the window .

3. Define color variables

Because we need to use some colors , and Python It doesn't come with you . So we need to define a few colors .

two ,GameOver

Mentioned earlier , The most important part of all games is the loop . and GameOver Function is the condition to jump out of this loop . Here is the interface that the snake displays when it eats its own body or touches the boundary ( The death code will be displayed later )

three , Snake and raspberry

Next, introduce the theme of the game , That is, snake and raspberry display and movement .

1. Define initial position

We think of the whole interface as many 20*20 Small square of , Each square represents a unit , The length of a snake can be expressed in several units . Here the body of the snake is stored in a list , Deletion after convenience .

2 . Keyboard input to judge the movement of snake

We need to input the up and down key or left and right key through the keyboard WASD To control the movement of snakes , Join at the same time press Esc Just exit the function of the game .

The snake eating movement has one characteristic : You can't move in the opposite direction . So we need to add restrictions .

The next step is to turn the snake head according to the keyboard input , And add the current position of snake head to the list of snake body .

3 . Judge if you eat raspberries

If the snakehead overlaps the raspberry's Square , You'll get raspberries , Clear the raspberry count ; And without raspberries , The snake's body will follow the snake's head , The last quarter of snake body will be kicked out of the list .

4 . Regenerate Raspberry

When the number of raspberries is 0 Time , Regenerate Raspberry , At the same time, the score increases .

5. Refresh display layer

Every movement of snake and raspberry , Will refresh the display layer to display . It's kind of like animation " frame ".

6. Judge whether death or not

When the snake's head goes beyond the boundary or the snake's head coincides with its body , Snake death , call GameOver.

7. Control game speed

In order to increase the difficulty , We set the longer the body, the faster the speed , Until it reaches an upper limit .

Come here , Snake game is finished . how , Simple No ?

Technology