C # Snake Game-Object layer (2)

Source: Internet
Author: User

This section analyzes the definition and implementation of a specific entity class. The following is the entity class structure of the game:

 

The movement of Game objects is the process of constantly creating and destroying pictures. The principle is the same as that of playing video tapes. Therefore, I have defined a base class, which defines the common members of sub-classes. Because the methods are implemented in the same way, the draw and Crear methods in the base class are not defined as abstract methods, instead, they are actually implemented! After subclass inheritance, you do not need to implement it.

Commonmethod. CS

/// <Summary> <br/> // the function and basic attribute data used in the snake and food classes are defined here, inherit from subclass <br/> /// </Summary> <br/> public class commonmethod <br/>{< br/> Public point {Get; set ;} </P> <p> Public void draw (Graphics g, pen P, solidbrush S, point) <br/>{< br/> G. drawrectangle (p, (11 * point. x-9), (11 * point. y-9), 10, 10); <br/> G. fillrectangle (S, (11 * point. x-8), (11 * point. y-8), 10-1, 10-1); <br/>}</P> <p> Public void Crear (Graphics g, solidbrush S, point) <br/> {<br/> G. fillrectangle (S, (11 * point. x-9), (11 * point. y-9), 10 + 1, 10 + 1); <br/>}< br/>}

(The "10" above is the size of the small square of the game map. The game map is composed of N * m small square, which is defined by myself based on hobbies)

In addition to inheriting the base class, the snake class also has its own members. For example, snakes can crawl while food is still.

A snake consists of a list of coordinate sets <point>. Therefore, the crawling of a snake is to change the coordinate points of a snake. I have considered two methods to implement crawling:

Method 1: traverse all the list <point> and make the last coordinate point equal to the previous coordinate point. The snake head is a new coordinate in the direction of motion, so that the snake can crawl;

For
(INT I = snake body. Count-1; I> = 1; I --)
{
Snake body [I] = snake body [I-1];
}

.......


Snake body [0] = new point (new coordinate );


Method 2: The crawling of a snake is the process of painting the head and removing the tail. You only need to change the coordinates of the Snake Head and delete the tail coordinates. If you eat food, you do not need to go to the end, in this way, snakes naturally become longer.


Void move ()


{


Snake body. insert (0, new coordinates );


If (eat food)

......


Else


Snake body. Remove (tail coordinate );

}

Compared with the two methods, method 2 is more efficient than method 1. method 1 re-draws the entire snake body every time a snake crawls, which sometimes causes the screen to flash (no dual buffering ); method 2: You only need to draw the snake head and tail.

Snake. CS

Public Enum snkedirection <br/>{< br/> up, down, left, right <br/>}< br/> Public Enum snkestate <br/> {<br/> normal = 50, fast = 30, low = 200 <br/>}< br/> public class snake: commonmethod <br/>{< br/> public list <point> snkepoints {Get; set ;} <br/> Public snkedirection {Get; Set ;}< br/> Public snkestate {Get; set ;} <br/> // venue <br/> Public int backwidth; <br/> Public int backheight; </P> <p> // constructor <br/> Public snake (INT snkelengh, snkedirection direction, int W, int H) <br/>{< br/> snail kepoints = new list <point> (); <br/> // default snake length <br/> for (INT I = snkelengh; i> 0; I --) <br/>{< br/> snail kepoints. add (new point (I, 5); <br/>}< br/> This. snkedirection = direction; <br/> This. backwidth = W; <br/> This. backheight = H; <br/>}</P> <p> // whether crawling is allowed. If it is true, add the new coordinates to the snake body and act as the snake header. <br/> Public bool iscanmove (ref point) <br/>{< br/> point. X = snail kepoints [0]. x; <br/> point. y = snail kepoints [0]. y; <br/> switch (snail kedirection) <br/>{< br/> case snail kedirection. up: <br/> If (point. y = 1) <br/> return false; <br/> else <br/> point. y-= 1; <br/> break; <br/> case snkedirection. down: <br/> If (point. y = backheight/10-3) <br/> return false; <br/> else <br/> point. Y + = 1; <br/> break; <br/> case snail kedirection. left: <br/> If (point. X = 1) <br/> return false; <br/> else <br/> point. x-= 1; <br/> break; <br/> case snkedirection. right: <br/> If (point. X = backwidth/10-2) <br/> return false; <br/> else <br/> point. X + = 1; <br/> break; <br/>}< br/> snail kepoints. insert (0, point); <br/> return true; <br/>}</P> <p> // draw the entire snake <br/> Public void drawsnake (Graphics g) <br/>{< br/> foreach (point in snail kepoints) <br/>{< br/> base. draw (G, new pen (color. black), new solidbrush (color. yellowgreen), point); <br/>}</P> <p> // draw a snake header <br/> Public void drawhead (Graphics g) <br/>{< br/> base. draw (G, new pen (color. black), new solidbrush (color. yellowgreen), point); <br/>}< br/>}

Food is simple.

Food. CS

/// <Summary> <br/> // define the food type and use it for future extension, currently, this version does not use <br/> /// </Summary> <br/> Public Enum foodtype <br/>{< br/> normal, fast, slow, long <br/>}</P> <p> public class food: commonmethod <br/>{< br/> Public foodtype {Get; set ;} </P> <p> Public void drawfood (Graphics g) <br/>{< br/> base. draw (G, new pen (color. black), new solidbrush (color. orange), point); <br/>}< br/>}

 

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.