// ** Draw the related class of snake, did not use polymorphism ** // Class AbstractDraw {public: Virtual Void Draw (int x, int y) = 0;}; Class Drawhead: public abstractdraw {public AbstractDraw {public : Void Draw (int x, int y);}; class drawbody: public abstractdraw {public: void draw (int x, int y);}; class cleanTail: public abstractdraw {public: void draw (int x, int y) };
Class Drawbean: Public AbstractDraw {public: Void Draw (INT X, INT Y);
Void Drawhead :: Draw (int x, int y) {supercol brush; int SX, Sy; SX = getmaxx () / 2-19 * fudu; sy = getmaxy () / 2-14 * fudu; brush.circle3 (SX (x 0.5) * fudu, SY (Y 0.5) * fudu, fudu / 2, blue, red);} void DrawBody :: Draw (int x, int y) {supercol brush; int SX, Sy; SX = Getmaxx () / 2-19 * fudu; Sy = getmaxy () / 2-14 * fudu; brush.bar2 (SX X * fudu, SY Y * fudu, sx (x 1) * fudu, SY (Y 1) * fudu, blue);} void cleangTail :: Draw (int x, int y) {supercol brush; int SX, Sy; SX = getmaxx () / 2-19 * fudu; Sy = getMaxy () / 2 -14 * fudu; brush.bar2 (SX X * fudu, SY Y * fudu, SX (x 1) * fudu, SY (Y 1) * fudu, black);} void drawbean :: Draw (int x, int y) {supercol brush; int SX, Sy; SX = getmaxx () / 2-19 * fudu; sylpere, sy = getmaxy () / 2-14 * fudu; brush.circle3 (SX (x 0.5) * fudu , SY (Y 0.5) * fudu, fudu / 2, yellow, white);
// * / * Located on the start of graphics functions and static scenes Drawing * / * // Class ScreenLayout {public: void graphicStart (); void graphicend (); void errotetect (); void test ();};
void ScreenLayOut :: GraphicStart () {int gdriver, gmode; gdriver = DETECT; gmode = VGAHI; // Kinitgraph (& gdriver, & gmode, "D: // TC // BGI"); // Bgi's address shoud be compitabled with Source File initgraph (& gdriver, & gmode, address); cleardevice (); // Haha Got it !!!} void screenLayOut :: GraphicEnd () {closegraph ();} void screenLayOut :: ErrorDetect () {int errorcode = 0; errorcode = graphResult (); if (ErrorCode! = GROK) / * An error odurred * / {GameStatur = exit; printf ("Graphics Error:% S / N", GrapherrorMSG (ErrorCode)); Printf ("Press Any Key to Halt) : "); Getch ();}} void screenlayout :: test () {// core snake public: void insert (snake * now); void dirchan (); void checkeatself (); void Checkeatbean (); snake () {mx = 0; my = 0; mdir = right; next = null;} snake (int X, int y, direction dir, snake * s = null) {mx = x; my = y MDIR = DIR; Next = S;} void show (); // void bianli (); int GETX () {return;} int gety () {return my;} int getdir () {Return MDIR;} void SetDir (Directi) ON P) {mdir = p; Dirchan ();} void bianlidir (); //精 -> Functions of the related variable value BOOL EATSELF (); // void settail () {this-> next = null } Bool Hitwall (); // Touch Wall Detection Void Makenewbean (ObStacle Ob [], Int Num, Bean & Tmpd); // Generate Six Bean Bool Hitbrack (Obstacle Ob [], int Num); // Bool Gotbean (Bean Dou); // Eat the test of beans
Private: int mx; int my; Direction Mdir; Snake * Next; // Here You Didn't Use New / Delete, Which is Surely a hidden dangerous.//as a beginner, i forward you.}; void snake :: Show () {cout << "x: << MX <<" Y: "<< my <<" DIR "<< mdir <<" -> "; if (next) Next-> show (); else COUT << "End / N";} void snake :: Dirchan () {switch (this-> mdir) {case up: this-> my -; break; case down: this-> my ; break; case left : this-> mx -; breaf; case right: this-> mx ; break; default: break;}} void snake :: INSERT (snake * now) {now-> next = this-> next; // pay attention The inside of Now is accessible, which brings great convenience to the TC without friends. This-> next = now; now-> mx = this-> mx; now-> my = this-> my ; now-> mdir = this-> mdir; // cout << (now-> next == null); Dirchan (); // yes}
Void Snake :: bianlidir () {// from tail to head, not head to tail if (next) {next-> bianlidir (); next-> mdir = this-> mdir; next-> dirchan ();}} Bool Snake :: Eatself () {bool flag = false; snake * ptmp = null; if (next) {for (ptmp = next; ptmp! = null; ptmp = ptmp-> next) {= ((this-> getx () == PTMP-> getX ()) && (this-> getY () == ptmp-> getY ())) {flag = true; break;}}} return;}}}} Return flag;} Bool Snake :: Hitwall () {BOOL flag = false; // bad smell, for the bare number !. IF ((this-> mx <0) || (this-> mx> 37) || (this-> my <0) || ( THIS-> My> 27)) {flag = true;} Return flag;} void snake :: makenewbean (obstacle ob [], int num, bean & tmpd) {BOOL flag = true; int i = 0, j = 0, DX = 0, DY = 0; Snake * PTMP = NULL; INT TMPARRAY [38] [28]; for (i = 0; i <38; i ) {for (j = 0; j <28; j ) {TMPARRAY [i] [j] = 0;}}}}}}}}}}}}} = 0; I
Bool Snake :: Hitbrack (Obstacle Ob [], INT NUM) {BOOL FLAG = FALSE; INT i = 0; for (i = 0; I