---Resume content start---
I. Introduction to the game
Snake is a very simple game, suitable for hand training. Let's take a look at the screenshot of my game:
How to play:
Enter: Start Game
Space bar: pause / resume
↑ ↓ ←→ direction key or WSAD key: control the direction of movement.
There are three kinds of food dividend, green and blue, which correspond to 10 points, 20 points and 30 points respectively. Each time I eat a food, I will increase the corresponding score, and the speed of each 100 points increase by one level. There is no checkpoint set. I play 1100 points, which is too fast, and then GAME OVER.
II. Game analysis
The game of greedy snake is very simple. A random point appears on the screen, indicating "food". Control the movement of "snake" from up to down and left to right. After eating "food", the body of "snake" will lengthen. If "snake" touches the frame or its own body, the game will end.
Let's first analyze what we need to pay attention to in order to write the game.
1. How do snakes Express
We can divide the whole game area into small squares, which are composed of a group of connected small squares to form "snake". We can use different colors to represent it. As shown in the above figure, I use dark color to represent the background and light color to represent "snake".
We can use coordinates to represent each small square. The range of X-axis and Y-axis can be set. Use a list to store the coordinates of the "snake body", then a "snake" will come out. Finally, as long as it is displayed in different colors.
2. How does the snake move?
The first reaction is to move each small square forward one grid like an earthworm, but it is very troublesome to achieve this. It was stuck here in the first place.
Imagine the greedy snake we've played. Every time the "snake" moves, it feels like it moves forward one grid as a whole. Eliminate the "action" of the "snake" in the brain. Think about the changes in the position of the "snake" before and after the move. In fact, except for the head and tail, other parts have not changed at all. That's easy. Adding the coordinates of the next grid to the beginning of the list and removing the last element of the list is equivalent to moving the snake one grid forward.
3. How to determine the end of the game?
"Snake" moves beyond the range of the game area or even loses when it encounters itself. The range of axis coordinates is set in advance, and it is easy to judge if it goes beyond the range. So how do you judge yourself?
If you think about the picture of "snake" moving in your mind, it's really difficult, but put it into the code, our "snake" is a list, so just judge whether the coordinates of the next grid have been included in the list of "snake".
With these problems sorted out, we can start coding.
III. code display
Because of the frequent addition and deletion of "snake" in the program, we use "deque" instead of "list" for better performance.
First you need to initialize the snake, which has an initial length of 3 and is located in the upper left corner.
# Coordinate range of game area SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1) SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1) snake = deque() def _init_snake(): snake.clear() snake.append((2, scope_y[0])) snake.append((1, scope_y[0])) snake.append((0, scope_y[0]))
Create "food" and randomly select a point on the screen as "food", but make sure that "food" is not on the "snake".
def create_food(snake):
food_x = random.randint(SCOPE_X[0], SCOPE_X[1])
food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])
while (food_x, food_y) in snake:
# If the food appears on the snake, come back
food_x = random.randint(SCOPE_X[0], SCOPE_X[1])
food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])
return food_x, food_y
The movement of "snake" can have four directions. A tuple is used to represent the direction of movement. Press the direction key every time to assign the corresponding value.
# direction pos = (1, 0) for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: if event.key in (K_w, K_UP): # This judgment is to prevent the snake from moving up by pressing the down key, resulting in a direct GAME OVER if pos[1]: pos = (0, -1) elif event.key in (K_s, K_DOWN): if pos[1]: pos = (0, 1) elif event.key in (K_a, K_LEFT): if pos[0]: pos = (-1, 0) elif event.key in (K_d, K_RIGHT): if pos[0]: pos = (1, 0)
The movement of "snake" can be expressed as follows:
next_s = (snake[0][0] + pos[0], snake[0][1] + pos[1]) if next_s == food: # We got the food. snake.appendleft(next_s) food = create_food(snake) else: if SCOPE_X[0] <= next_s[0] <= SCOPE_X[1] and SCOPE_Y[0] <= next_s[1] <= SCOPE_Y[1] and next_s not in snake: snake.appendleft(next_s) snake.pop() else: game_over = True #Python Learning group 631441315, there are a lot of PDF Free use of books and tutorials! No matter which stage you learn, you can get the tutorial you need!
---End of recovery content---