Game Programming Notes-Start (a) a simple game-snake

Source: Internet
Author: User
Two game programming starts
1. A simple game-snake
1. Snake Game Analysis

1) The goal of the game. Under the premise of not being killed, eat the prize to increase their length, to complete the upgrade.

2) The object in the game. The serpent, the wall, the prize son.

3) action. Snakes move, snakes eat prizes, snakes increase length. 2. Data structure and algorithm analysis

1) data structure. For simplicity, all objects are spliced with squares.

The snake can use a one-dimensional array description, each unit of the array describes the state of the snake block, such as the direction, a two-dimensional array can be used to describe the ground situation, where the wall is not allowed to be set to 1, the place can be set to 0; The prize is a special block.

2) algorithm.

Observe the characteristics of snakes. When you dismember a snake into a block, you will find that each block's movement depends on the previous move state of the block (the first block is controlled by the player).


As shown, 1 is the head of the snake, 4 is the tail, the blue arrow is the original direction of movement, the Red Arrow is the direction of the player control (press the next key). Move direction from 1~4. Right and right under (a), (b), under (c), right-right.

According to this rule, we can summarize a basic algorithm: use an array to store the snake block, each snake block node contains the information has the current direction of movement, then update the state of the snake, from the tail to the head to deal with.


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 look closely, we will find that the law, the snake every time it moves, it is only the head and tail have changed, then, every time we update the snake, we only need to move the tail of the snake block to the corresponding position, not easier. The answer is yes. This is the charm of the algorithm. As long as we are diligent in our brains, we always find some better solutions.

POS (n-1) = pos (0) +dir
Insert (n-1) before POS (0)

The complexity of the algorithm immediately changes from O (n) to O (1). And we'll also find that we just need to record One direction and then the complexity of the space is reduced.


3) map. Describes the ground information.

Our Snake game map information is simple, with a total of 3 types of objects standing on the ground: walls, snakes, and prizes. In each update, we populate the map with information about 3 types of objects by category. For example, the location of the wall fill 1, the prize sub-position 2, the position of the Snake fill 3 (Each snake block is filled), there is no place to fill 0. The two-dimensional array that fills the 0,1,2,3 is then handed to the rendering system.

In this way, our behind-the-scenes operation is basically done, and the rest is the details, waiting for the code to be dealt with in detail.



3. Rendering
Map Data description

Rendering snake game is also very simple, the map of the 1 parts painted blue, 2 of the part painted red, 3 of the part painted green, which will be a kind of effect. Look at the following picture:


What's the feeling of a snake game? And look at the following picture:


It's perfect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.