// ** The class related to the snake is painted, and no polymorphism is used during implementation **//
Class abstractdraw {
Public:
Virtual void draw (int x, int y) = 0;
};
Class drawhead: 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 cleantail: 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; Sy = getmaxy ()/2-14 * fudu;
Brush. circle3 (SX + (x + 0.5) * fudu, SY + (Y + 0.5) * fudu, fudu/2, yellow, white );
}
// */* Draws the starting and static scenes of graphic functions */*//
Class screenlayout {
Public:
Void graphicstart ();
Void graphicend ();
Void errordetect ();
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 occurred */
{
Gamestatur = exit;
Printf ("graphics error: % s/n", grapherrormsg (errorcode ));
Printf ("press any key to halt :");
Getch ();
}
}
Void screenlayout: Test (){
;
}
Class snake {// core snake class
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 MX ;}
Int Gety () {return my ;}
Int getdir () {return mdir ;}
Void setdir (Direction P) {mdir = P;
Dirchan ();}
Void bianlidir (); // refined-> function used to repeat the linked list and change the value of Related Variables
Bool eatself ();
// Void settail () {This-> next = NULL ;}
Bool hitwall (); // wall hitting Detection
Void makenewbean (obstacle ob [], int num, bean & tmpd); // generate new beans
Bool hitbrack (obstacle ob [], int num); // Obstacle Detection
Bool gotbean (bean Dou); // check the 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 forgive 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 --; break;
Case right: This-> MX ++; break;
Default: break;
}
}
Void snake: insert (snake * Now ){
Now-> next = This-> next; // note that now can be accessed internally, which makes TC without friends very convenient.
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)
{If (this-> getx () = pTMP-> getx () & (this-> Gety () = pTMP-> Gety ()))
{
Flag = true;
Break;
}
}
}
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;
}
}
Randomize ();
For (I = 0; I <num; I ++ ){
If (OB [I]. Flag ){
Tmparray [ob [I]. x] [ob [I]. Y] = 1;
}
}
If (next ){
For (pTMP = next; pTMP! = NULL; pTMP = pTMP-> next)
{
Tmparray [pTMP-> getx ()] [pTMP-> Gety ()] = 1;
}
}
Flag = false;
While (! Flag)
{Dx = random (35) + 1;
DY = random (25) + 1;
// Cout <"dx" <DX;
If (tmparray [dx] [dy] = 0 ){
Tmpd. x = DX;
Tmpd. Y = Dy;
Flag = true;
}
}
Gotoxy (2, 2 );
Printf ("score: % d", score );
Score ++;
}
Bool snake: hitbrack (obstacle ob [], int num)
{Bool flag = false;
Int I = 0;
For (I = 0; I <num; I ++ ){
If (OB [I]. Flag ){
If (OB [I]. X = This-> getx () & (OB [I]. y = This-> Gety () Flag = true;
}
}
Return flag;
}
Bool snake: gotbean (bean Dou ){
// Consider a multiple beans mode? Too complicated, maybe.
Bool flag = false;
If (DOU. x = This-> getx () & (DOU. Y = This-> Gety () Flag = true;
Return flag;
}