C # Snake Game-logic layer (3)

Source: Internet
Author: User

At the logic layer, we need to analyze the rules of the game: whether the snake can crawl, whether to eat food, whether to generate new food, whether the game ends, points rules, and so on.

There are currently two forms of snake death: hitting the wall and biting yourself :(

Prerequisite for producing new food: A snake eats food and generates new coordinates at random (new coordinates! = Snake body [N]).

Gamelogic. CS

Public class gamelogic <br/>{< br/> /// <summary> <br/> /// snail ke_init_lengh: Default length of a snake <br/> /// snail ke_init_direction: default crawling direction of the snake <br/> /// </Summary> <br/> Public const int snakke_init_lengh = 3; <br/> Public const snkedirection snke_init_direction = snkedirection. right; <br/> private snake; <br/> Private food; <br/> // game scenario <br/> private system. windows. forms. panel; <br/> private graphics g; <br/> private solidbrush backbrush; <br/> private bool gameover = false; <br/> Public int backwidth; <br/> Public int backheight; </P> <p> private point objfoodpoint <br/> {<br/> get {return food. point ;}< br/> set {food. point = value ;}< br/>}< br/> private point newsnkehead <br/>{< br/> get {return snake. point ;}< br/> set {snake. point = value ;}< br/>}< br/> Public snkedirection <br/>{< br/> get {return snake. snkedirection;} <br/> set {snake. snail kedirection = value ;}< br/>}< br/> Public bool gameover <br/>{< br/> get {return gameover ;} <br/>}</P> <p> // constructor, instantiated object <br/> Public gamelogic (system. windows. forms. panel P, int W, int h) <br/>{< br/> This. panel = P; <br/> backwidth = W; <br/> backheight = H; <br/> snake = new snake (snak_init_lengh, snak_init_direction, backwidth, backheight ); <br/> food = new food (); <br/>}</P> <p> // initial game screen <br/> Public void initgame () <br/> {<br/> G = panel. creategraphics (); <br/> backbrush = new solidbrush (panel. backcolor); <br/> snake. drawsnake (g); <br/> createfood (); <br/>}</P> <p> // clear the game screen <br/> Public void crearall () <br/> {<br/> foreach (point in snake. snake) <br/> snake. crear (G, backbrush, point); <br/> food. crear (G, backbrush, objfoodpoint); <br/>}</P> <p> // snake movement <br/> Public void snkemove () <br/>{< br/> point newpoint = new point (); <br/> If (snake. iscanmove (ref newpoint) <br/>{< br/> newsnkehead = newpoint; <br/> snake. drawhead (g); <br/> If (iseatfood (objfoodpoint) <br/>{< br/> createfood (); <br/>}< br/> else <br/> {<br/> snake. crear (G, backbrush, Snake. snail kepoints [Snake. snail kepoints. count-1]); <br/> snake. snail kepoints. remove (snake. snail kepoints [Snake. snail kepoints. count-1]); <br/>}< br/> else <br/>{< br/> gameover = true; <br/>}< br/> If (ishitoneself () <br/> gameover = true; <br/>}</P> <p> // snake food <br/> Public bool iseatfood (point) <br/> {<br/> If (point = snake. snail kepoints [0]) <br/>{< br/> return true; <br/>}< br/> return false; <br/>}</P> <p> // a food with random coordinates is generated when it is eaten. <br/> Public void createfood () <br/>{< br/> random objrandom = new random (); <br/> point objpoint; <br/> int tempx, Tempy; <br/> for (INT I = 0; I ++) <br/> {<br/> tempx = objrandom. next (backwidth) % (backwidth/11); <br/> Tempy = objrandom. next (backheight) % (backheight/11); <br/> If (tempx = 0 | Tempy = 0) <br/>{< br/> continue; <br/>}< br/> else <br/>{< br/> objpoint = new point (tempx, Tempy); </P> <p> If (snake. snail kepoints. contains (objpoint) <br/>{< br/> continue; <br/>}< br/> else <br/>{< br/> objfoodpoint = objpoint; <br/> break; <br/>}< br/> food. drawfood (g); <br/>}</P> <p> // The Snake head hits the snake body. <br/> Public bool ishitoneself () <br/> {<br/> for (INT I = 1; I <snake. snail kepoints. count; I ++) <br/>{< br/> If (snake. snail kepoints [0] = snake. snail kepoints [I]) <br/> return true; <br/>}< br/> return false; <br/>}</P> <p> // accumulate points <br/> Public String getfenshu () <br/>{< br/> return (snake. snail kepoints. count-Snail ke_init_lengh) * 15 ). tostring (); <br/>}< br/>}

AboveCodeThere are many comments, so it is not difficult to understand them. Let's take a look at some suggestions for improvement! :)

Related Article

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.