C # Game Programming: "The console Games series" "Six, greedy snake example" __ programming

Source: Internet
Author: User
First, game analysis1976, the Gremlin platform launched a classic arcade game blockade, then for the snake's prototype, this simple little game is very popular, give this generation of people to bring indelible memory.   As future programmers of us, playing their own design of the greedy snake than to play the existing more interesting, we can add the various features we think of, even if the game is not strange, I am the game of the decision. Greedy snake design is not complicated, is a lot of game programming enthusiasts to get started with one of the preferred items, Rite also. The whole concept of the game, shielding other fancy features, let's focus on the game's two main objects: snakes and food. Someone who has played this little game. Know, in order to let their little snakes grow crazy, you have to find food to eat, the premise is not to encounter obstacles, or the snake hung.   The snake by the player through the keyboard key to control the direction of the crawl, and food in the snake field of sight randomly generated, when the snake ate food, snake long section, food again randomly generated, of course, food will not fall on the snake, otherwise the snake had to be closed. Through the above, we are not difficult to find the main operation of the player is to control the crawling of snakes, for the snake object, it is the characteristics of crawling can climb, so how to design the snake crawling and how to feed back to the picture is what we need to consider now.   Snakes are crawling in a variety of ways, but before that, we have to consider how snakes are represented and how to express a snake with data structures. You might think of using a linear table to store the nodes of a snake, when the snake moves, every node is updated to the new value after the crawl direction, so each time the snake moves one step, the whole body: all nodes have to visit once, you can see that the final efficiency must not be too high; maybe you can think of a linear table to store the snakes ' nodes. , when the snake moves, the new coordinate node is added to the linear table according to the crawling direction, then the relative "snake Tail" node is deleted, the last traverse each node, adjusts each node in the linear table position, prevents the linear table to appear the vacancy question. This method is still unavoidable to move the whole body, efficiency is not high.
May be thought of using a linked list to store the various nodes of the snake, when the snake moves only need to operate the chain tail pointer, the other nodes do not need one visit, efficiency relative to the previous two kinds of upgrade a lot.
In C #, however, we don't have to consider so many,list<> can solve this problem very well, using list<> to express a snake, when the snake crawling into a new node, and delete the tail end of the tail node, so as to achieve the crawling effect of snakes; When the snake eats the food, Just add a new node to it, no need to delete the old node operation, so the snake's body will grow a section. It is the same idea of how to render the snake to the screen (perhaps because our snakes are in the same style each node), we do not need to traverse each node and then to render them to the screen, but to take the "paint head wipe Tail" way, only the change of the node, the invariant node does not need to consider, So that the performance of the game greatly improved, even if the snake node thousands of tens of thousands of, the final need to consider only two nodes before and after, the snake crawling up the waist is not tired, eat why incense. The following diagram is used to express this idea:

Second, the realization of the gameAccording to the above analysis, we clearly know what the main object of our game is, everything is difficult to begin with, after analysis we will be easy to achieve what we need, first of all to see the realization of snakes. Snake class implementation [CSharp]  View plain copy print? using system;   using system.collections.generic;   using CEngine;    using cgraphics;      namespace snake   {        /// <summary>       ///  greedy Snake         /// </summary>       internal class Snake        {            #region   Field               /// <summary>           ///  Snake body             /// </summary>           private  list<cpoint> m_body;           /// <summary>           ///  Crawl Direction             /// </summary>            private CDirection m_dir;            /// <summary>           ///   Snake head            /// </summary>           private CPoint m_head;            /// <summary>            ///  Snake tail            /// </summary>            private CPoint m_tail;               #endregion               #region   Constructors               /// <summary>            ///  Constructor             /// </summary>           /  <param name= "Len" ></param>            /// <param name= "dir" ></param>  

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.