10 minutes to write a Snake game in Python

Snake, you should have played. When did the first contact with the Snake, or to drop walnuts on Nokia, then play the whole day. Today, we use the Python programming a Snake game, let's first look at the results:

Here Insert Picture Description
Well, first introduced a line of thought

All of the content is the most important game in the cycle programs, this is the premise of ensuring normal operation of a game.

Here are the main ideas written Snake game.
Here Insert Picture Description
Ado, we write directly to explain how to use it in Python Snake

First, call the library as well as the initial setting

  1. Call third-party library

Python and other languages ​​very different thing is that he has a lot of third-party libraries can be invoked. In the preparation of the game Python, pygame is a very simple to use third-party libraries, can be installed directly through the pip. Installation methods already mentioned in the previous article, will not repeat them. For more pygame functions friends can refer to the official documents.

This is the library when writing Snake we need to call.
Here Insert Picture Description
2 Initial Settings

We initialize title and icon pygame, define the size of the window (the border), the window through which a few lines of code.
Here Insert Picture Description
3. Define variable color

Because we need to use some color, but Python is not built-in. So we need to define a few colors.
Here Insert Picture Description
Two, GameOver

Mentioned before, the most important part of any game is cycling. The GameOver function of this cycle is out of condition. Here are displayed when the snake to eat his body across the border or interface (judgment code after death will show)
Here Insert Picture Description
Third, the Snake and raspberries

The next section describes the theme of the game, that is, Snake and Duchesnea display and motion.

  1. Defining the initial position

Our entire interface as many 20 * 20 small squares, each square represents a unit, the length of the snake can use several units friends. Here the snake's body stored in the form of a list, after deletion of convenience.

Here Insert Picture Description
2. Analyzing keyboard snake motion

We need to up and down left and right keys or WASD keyboard controls the movement of snakes, while adding press Esc to exit the function of the game.
Here Insert Picture Description
Snake movement has a characteristic: You can not reverse direction movement. So we need to add a restriction.
Here Insert Picture Description
The next turning operation is to be snake heads according to the input keyboard, snakehead and added to the list of current location of snake.
Here Insert Picture Description
3 determines whether or not to eat raspberry

If the smugglers and raspberry squares coincide, it is judged eat raspberries, raspberry number will be cleared; and did not have to raspberries, then the snake will follow snakeheads campaign, snake last one will be kicked out of the list .
Here Insert Picture Description
4. Rebuild raspberries

When the number is 0 raspberry, raspberry regenerate, while increasing the score.
Here Insert Picture Description
The refresh display layer

Every movement snakes and raspberries, will refresh the display layer operation display. Somewhat similar to the animated "frame."
Here Insert Picture Description
6. determine whether the death

When the smugglers, or snakeheads beyond the boundaries coincide with their snake, snakes death, calling GameOver.
Here Insert Picture Description
7. Control game speed

To increase the difficulty, we set the faster the longer the snake until it reaches a limit.

Here Insert Picture Description

Here, Snake game finished. How easy is not?

Published 38 original articles · won praise 1 · views 2197

Guess you like

Origin blog.csdn.net/wulishinian/article/details/103064984