


Head and tail display.īefore getting the next position, first judge whether the next position is on the boundary, whether it is your own snake body, or whether it is food. In each execution of the loop, it just refreshes. So considering that the snake’s walking process should be partially refreshed. It’s just that the direction has changed, but the snake’s body shouldn’t swing around at this time. In this way, the next position obtained according to the direction is the position of the new direction. In the loop condition, each time before the next position is obtained, the keyboard keys are asynchronously obtained to see if any key is pressed, and if there is, the direction of the snake is changed. After getting the next position, perform the operation of inserting and deleting the head and tail of the linked list, so that there is a feeling of snake movement. Each step of the process will sleep for a certain period of time, and then it can get to the next position according to the current direction of the snake. Write an endless loop in the main control logic. How does the snake keep moving in the current direction? The food is also a single coordinate, and the wall is a set length and width. There are only four directions, so use enumeration. The snake also needs to save the current moving direction. Each node of the linked list is the x and y coordinates. The snake is represented by a linked list. The entities have games, snakes, coordinates, food, and walls, so our model is abstracted first. C language game-Snake and Tetris Greedy snakeįirst think about the process of designing this game
