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:
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.
Ado, we write directly to explain how to use it in Python Snake
First, call the library as well as the initial setting
- 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.
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.
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.
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)
Third, the Snake and raspberries
The next section describes the theme of the game, that is, Snake and Duchesnea display and motion.
- 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.
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.
Snake movement has a characteristic: You can not reverse direction movement. So we need to add a restriction.
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.
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 .
4. Rebuild raspberries
When the number is 0 raspberry, raspberry regenerate, while increasing the score.
The refresh display layer
Every movement snakes and raspberries, will refresh the display layer operation display. Somewhat similar to the animated "frame."
6. determine whether the death
When the smugglers, or snakeheads beyond the boundaries coincide with their snake, snakes death, calling GameOver.
7. Control game speed
To increase the difficulty, we set the faster the longer the snake until it reaches a limit.
Here, Snake game finished. How easy is not?