These two things took time to write the snake program. Now we will describe the general design process. Please download the source program at www.csdn.net. My username is andamajing.
A simple analysis shows that the snake shows and controls two parts in general. The first part shows the overall layout of the form, the display of game activity, and the display of score, game help and instructions on this game; the second aspect is the control part, which includes the control key and direction key controls such as the color settings, start, pause, and stop of snakes and food in the game.
First of all, we need to define a class for snakes. This class includes the composition unit and size of snakes, the color of snakes, the length of snakes, the direction of movements of snakes, and other basic fields. The movement of snakes, food and other basic methods, as well as whether to die or not. The specific definition is as follows:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. drawing; using system. windows. forms; using system. collections; namespace snkegame {class snake {// Private Label bodyceil of the snake body unit; // the size of the snake body unit static private size = new size (10, 10); // The Snake body color public color snkecolor; // snake linked list arraylist snake = new arraylist (); // snake length private int snkelength = 5; Public int snkelengt H {get {return snake. count ;}// the direction of the snake movement. The initial direction is private direction snkedirection = direction. right; Public direction snkedirection {get {return snkedirection;} set {snkedirection = value ;}// indicates whether the food is eaten. Private bool flag = false; public bool flag {get {return flag;} set {flag = value ;}}// draw the initial snake private void drawbeginsnake () {for (INT I = 0; I <5; I ++) {bodyceil = new label (); bodyceil. B Ackcolor = snkecolor; bodyceil. size = size; bodyceil. location = new point (40-I * 10, 0); bodyceil. borderstyle = system. windows. forms. borderstyle. fixedsingle; snake. add (bodyceil) ;}}// the constructor with parameters. You can set the snake body color public snake (color newcolor) {snkecolor = newcolor; drawbeginsnake ();} // return the public arraylist getsnake () {return snake;} // whether there is a food flag private bool hasfood = false; // defines whether the snake has a death mark. True indicates death, false: not dead Private bool isdead = false; Public bool isdead {get {return isdead; }}// snake mobile public void move (system. windows. forms. control, label food) {If (! Hasfood) {control. controls. remove (control. getchildatpoint (Label) Snake [Snake. count-1]). location); snake. removeat (snake. count-1);} label snkehead = new label (); copysnkebodyceil (snkehead, (Label) Snake [0]); Switch (snkedirection) {Case direction. up: Snake Head. top-= 10; If (snail kehead. top <0) isdead = true; break; Case direction. down: snail head. top + = 10; If (snail kehead. top> 270) isdead = true; break; Case direction. left: snail head. left-= 10; If (snail kehead. left <0) isdead = true; break; Case direction. right: Snake Head. left + = 10; If (snail kehead. left> 340) isdead = true; break;} snake. insert (0, snail kehead); If (hasfood) {hasfood = false; control. controls. remove (control. getchildatpoint (food. location); // control. controls. clear (); this. flag = true ;}}// copy the private void copysnakbodyceil (Label to, label from) {. backcolor = from. backcolor;. size = from. size;. location = from. location;. borderstyle = from. borderstyle;} // determines whether a food flag is public void eatfood (Label _ food) {Switch (snkedirection) {Case direction. up: If (Label) Snake [0]). left = _ food. left & (Label) Snake [0]). top = _ food. top + 10) hasfood = true; else hasfood = false; break; Case direction. down: If (Label) Snake [0]). left = _ food. left & (Label) Snake [0]). top = _ food. top-10) hasfood = true; else hasfood = false; break; Case direction. left: If (Label) Snake [0]). left = _ food. left + 10 & (Label) Snake [0]). top = _ food. top) hasfood = true; else hasfood = false; break; Case direction. right: If (Label) Snake [0]). left = _ food. left-10 & (Label) Snake [0]). top = _ food. top) hasfood = true; else hasfood = false; break ;}}}}
The next step is the definition of food. The first step is as follows:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. windows. forms; using system. drawing; namespace snail keame {class food {// define a food private label foodceil = new label (); Public label foodceil {get {return foodceil;} set {foodceil = value ;}} // define the food color private color foodcolor; Public color foodcolor {get {return foodcolor ;}// define the food location private point foodlocation; public point foodlocation {get {return new point (foodceil. top, foodceil. left) ;}}// define the food size Private Static size foodsize = new size (10, 10); // constructor, public food (color, point) {foodceil. size = foodsize; foodceil. borderstyle = system. windows. forms. borderstyle. fixedsingle; foodceil. backcolor = color; foodceil. left = point. x; foodceil. top = point. y; foodcolor = color;} // copy the public void copyfoodceil (Label to, label from) {. backcolor = from. backcolor;. size = from. size;. location = from. location;. borderstyle = from. borderstyle ;}}}
The above two Classes define two objects in the game. Next we need to control the snake movement in the display interface of the topic. The interface of the topic is as follows:
The running interface is as follows:
The following figure shows the end of the game:
The format with the color settings is displayed as follows:
Next, let's talk about how to control programs in the main program and control menu items and buttons in the program.
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using snail keame. properties; namespace snkegame {public partial class formsnakegame: FORM {// define a snake private snake mysnake; // define a food Private food myfood; // define the snake color private color mysnkecolor; // define the food color private color myfoodcolor; // Define a color settings window color settings gamecolorsetting = new color settings (); Public formsnakegame () {initializecomponent (); this. buttonstop. enabled = false; cleardirection (); this. low level toolstripmenuitem. checked = true; this. panelgame. backgroundimage = resources. myblog;} private void Game Description toolstripmenuitem1_click (Object sender, eventargs e) {game operation gameoperationinformation = new game operation (); gameoperationinformation. show ();} private Void about toolstripmenuitem_click (Object sender, eventargs e) {about gameabout = new about (); gameabout. show ();} private void exit toolstripmenuitem_click (Object sender, eventargs e) {This. close ();} private void color settings toolstripmenuitem_click (Object sender, eventargs e) {// gamecolorsetting = new color settings (); gamecolorsetting. show () ;}// click the stop or pause button and press Private Static int I = 0; // It is used to determine whether to display stop or continue private void Bu Ttonstop_click (Object sender, eventargs e) {if (I ++ % 2 = 0) {This. buttonstop. TEXT = "continue"; this. timersnkespeed. enabled = false;} else {This. buttonstop. TEXT = "stop"; this. timersnkespeed. enabled = true ;}// start pressing private void buttonstart_click (Object sender, eventargs e) {This. panelgame. backgroundimage = NULL; this. buttonstart. enabled = false; enabledirection (); mysnkecolor = game Colorsetting. setsnkecolor; myfoodcolor = gamecolorsetting. setfoodcolor; this. buttonstop. enabled = true; mysnake = new snake (mysnkecolor); foreach (Label LBL in mysnake. getsnake () {This. panelgame. controls. add (LBL);} myfood = new food (myfoodcolor, new point (100,100); this. panelgame. controls. add (myfood. foodceil); this. myfood. copyfoodceil (currentfood, this. myfood. foodceil); this. timersnkespeed. E Nabled = true; // start timer} // draw the snake function private void drawsnake (snake _ newsnake) {foreach (Label LBL in _ newsnake. getsnake () {This. panelgame. controls. add (LBL) ;}}// define a label to store the current food label currentfood = new label (); // issue the food private void putfood () {random randfood = new random (); int newfoodx = randfood. next (35); int newfoody = randfood. next (28); label newfood = new label (); this. myfood. copyfoodceil (newfoo D, currentfood); newfood. left = newfoodx * 10; newfood. top = newfoody * 10; this. panelgame. controls. remove (this. myfood. foodceil); this. panelgame. controls. add (newfood); this. myfood. copyfoodceil (this. myfood. foodceil, newfood); this. myfood. copyfoodceil (currentfood, newfood);} // call the private void drawfood (Label newfood) {This. panelgame. controls. add (newfood);} // remove food private void removefood (Label oldfo OD) {This. panelgame. controls. remove (oldfood);} // timer function private void timersnkespeed_tick (Object sender, eventargs e) {mysnake. eatfood (this. myfood. foodceil); mysnake. move (this. panelgame, this. myfood. foodceil); drawsnake (mysnake); If (this. mysnake. isdead) {This. buttonstart. enabled = true; this. buttonstop. enabled = false; this. timersnkespeed. enabled = false; clearcolor (); cleardirection (); this. t Extboxscore. TEXT = ""; this. panelgame. controls. clear (); this. panelgame. backgroundimage = resources. gameover1;} If (mysnake. flag) {putfood (); mysnake. flag = false;} This. textboxscore. TEXT = (mysnake. snkelength-5) * 10 ). tostring ();} // specifies the private void clearflag () {This. low level toolstripmenuitem. checked = false; this. normal Level toolstripmenuitem. checked = false; this. high-level toolstripmenuitem. checked = False;} // select normal horizontal private void normal horizontal toolstripmenuitem_click (Object sender, eventargs e) {clearflag (); this. normal Level toolstripmenuitem. checked = true; this. timersnkespeed. interval = 300;} private void high-level toolstripmenuitem_click (Object sender, eventargs e) {clearflag (); this. high-level toolstripmenuitem. checked = true; this. timersnkespeed. interval = 200;} private void low level toolstripmenuitem_click (Object send Er, eventargs e) {clearflag (); this. low level toolstripmenuitem. checked = true; this. timersnkespeed. interval = 400;} // key handler private void formsnakegame_keypress (Object sender, keypresseventargs e) {If (E. keychar = 'A' | E. keychar = 'A') {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. right? Direction. right: direction. left); clearcolor (); this. buttonkeya. backcolor = color. blue;} else if (E. keychar = 'D' | E. keychar = 'D') {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. left? Direction. left: direction. right); clearcolor (); this. buttonkeyd. backcolor = color. blue;} else if (E. keychar = 'W' | E. keychar = 'W') {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. down? Direction. down: direction. UP); clearcolor (); this. buttonkeyw. backcolor = color. blue;} else if (E. keychar = 's' | E. keychar ='s ') {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. up? Direction. up: direction. down); clearcolor (); this. buttonkeys. backcolor = color. blue;} else {e. handled = true;} // press private void buttonkeyw_click (Object sender, eventargs e) {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. down? Direction. down: direction. UP); clearcolor (); this. buttonkeyw. backcolor = color. blue;} // press private void buttonkeya_click (Object sender, eventargs e) {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. right? Direction. right: direction. left); clearcolor (); this. buttonkeya. backcolor = color. blue;} // press private void buttonkeys_click (Object sender, eventargs e) {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. up? Direction. up: direction. down); clearcolor (); this. buttonkeys. backcolor = color. blue;} // press private void buttonkeyd_click (Object sender, eventargs e) {This. mysnake. snail kedirection = (this. mysnake. snkedirection = direction. left? Direction. left: direction. right); clearcolor (); this. buttonkeyd. backcolor = color. blue;} // clear the direction key background color private void clearcolor () {This. buttonkeyw. backcolor = system. windows. forms. button. defaultbackcolor; this. buttonkeya. backcolor = system. windows. forms. button. defaultbackcolor; this. buttonkeys. backcolor = system. windows. forms. button. defaultbackcolor; this. buttonkeyd. backcolor = system. windows. forms. button. defaultbackcolor;} // The direction key cannot be private void cleardirection () {This. buttonkeya. enabled = false; this. buttonkeyw. enabled = false; this. buttonkeyd. enabled = false; this. buttonkeys. enabled = false;} // The direction key can be private void enabledirection () {This. buttonkeya. enabled = true; this. buttonkeyw. enabled = true; this. buttonkeyd. enabled = true; this. buttonkeys. enabled = true ;}}}
The above is a simple introduction to snake programming. You can communicate with each other ....