Second, game programming started
1. A simple game-snake
1. Analysis of snake games
1) goals of the game. If the prize winner is not killed, increase the length of the sub-account to complete the upgrade.
2) objects in the game. Snake, wall, prize child.
3) action. The snake moves, the snake eats prize, and the length of the snake increases.
2. Data Structure and algorithm analysis
1) Data Structure. For simplicity, all objects are spliced with blocks.
Then, the snake can use a one-dimensional array to describe the status of the snake block, such as the direction. A two-dimensional array can be used to describe the ground, the wall is set to 1 if it cannot pass, and the place where it passes is set to 0. The prize is a special square.
2) algorithm.
Observe the characteristics of a snake. When a snake is removed as a square, it is found that the movement of each square is dependent on the previous movement of the square above it (the first square is controlled by the player ).
, 1 is the Snake Head, 4 is the tail, the Blue Arrow is the original direction of movement, and the Red Arrow is the direction controlled by the player (pressed down the key ). Move from 1 ~ 4. (A)-> (B) Bottom Right right, (B)-> (c) Bottom Right right.
According to this rule, we can sum up a basic algorithm: Using arrays to store snake blocks. The information contained in each node of a snake block has the current direction of movement. When the snake status is updated, from the tail end to the header.
for i=n-1 to 1{dir(i) = dir(i-1)pos(i) += dir(i)}if dirKeyDown{dir(0) = k}pos(0) += dir(0)
In this way, the most complex part is solved. Of course, this is not the best algorithm. If we observe this Rule carefully, we will find that each time a snake moves, it only changes its head and tail, every time we update a snake, we only need to move the snake block at the tail end to the corresponding position in the header. Isn't it simpler? The answer is yes. This is the charm of algorithms! As long as we work hard, we will always find some better solutions.
pos(n-1) = pos(0)+dirinsert(n-1) before pos(0)
The complexity of the algorithm immediately changes from O (n) to O (1 )! We also find that we only need to record one direction, and the space complexity is also reduced.
3) map. Describes the ground information.
The map information of our snake games is very simple. There are three types of objects standing on the ground: walls, snakes, and prize winners. During each update, the information of the three types of objects is filled into the map by category. For example, set "1" for the wall position, "2" for the prize winner, "3" for the snake position (for each snake block), and "0" for the places with no content. Then, the two-dimensional array filled with 0, 1, and 2 is handed over to the rendering system.
At this point, our behind-the-scenes operations are basically completed, and the rest is some details, which will be processed in detail when coding.
3. RenderingMap Data Description
The rendering of the Snake game is actually very simple. The part of the map 1 is painted blue, the part of 2 is painted red, and the part of 3 is painted green. What kind of effect will this be? See:
How do you feel like a snake game? Let's take a look:
Perfect!