The Snake game is a widespread game. It has been done a million times, and is playable on any device. Variants of snake run on things like cellphones to mainframes and Snake has been written in most of the major programming languages, from BASIC, C/C++, Pascal, Java, C# to whatever. Today we are going to take a look at a Snake game that is written in C/C++ and is designed to run in a Win32 console using text characters.
So, what is the Snake and how does it all work? Our Snake is simply a text character that traverses within specified boundaries on the screen gobbling up apples that are randomly placed within that specified boundary. Our snake compares its x y location to that of the apples to see if there has been a collision, i.e., if our snake has eaten up the apple. If so, that apple is removed, and a new apple is randomly placed within the specified boundary, and our snake gets
a body transplant, it is enlongated. All we need is to do is simply process the keyboard commands from the user and draw the snake. We will use the <LEFT>, <RIGHT>, <UP> and <DOWN> keys to steer the snake.
So let's get going.
We begin as usual by creating a project and a class, then we continue further on by embodying the class with members. We choose not to define and embody our member functions in the header file, we will just put the prototyes in a separate file.
The project files are:
||The main file
||The class definition and method body file
||The class declaration file
We will put the declaration of the variables, prototyes and function headers in CSnake.h.
int color_background, color_border , color_apple ,
color_normal , color_snake ; int snake, prvSnake, snake_Enlongment ;
int randomX, randomY, apple ;
int level ;
int score ;
int i, j;
int centerX, centerY;
int x, y, oldX, oldY;
int lines, columns;
int direction, difficulty;
CSnake( int i);
void gotoxy(int x, int y);
void setcolor(WORD color);
void clrbox(unsigned char x1,unsigned char y1,
unsigned char x2,unsigned char y2,unsigned char bkcol);
void box(unsigned x,unsigned y,unsigned sx,unsigned sy,
unsigned char col,unsigned char col2,char text_);
void putbox(unsigned x1,unsigned y1,unsigned x2,unsigned y2,
unsigned char texcol,unsigned char frcol,
unsigned char bkgcol,char bheader);
And bring Snake to life in CSnake.cpp.
Now let's have a look at some vital methods that are contained in CSnake.cpp.
The Create_Apples() Method
Create_Apples() method creates a new apple and randomly places it within the specified boundaries.
randomX= ( rand()% columns )+ centerX ;
randomY= ( rand()% lines)+ centerY ;
The Check_Apples() Method
Check_Apples() method compares the Snake's location with the apples, if they have collided, the snake to gets a body transplant, it is enlongated, the apple is removed and a new one is placed randomly within the boundaries.
The Display_snake() Method
Display_snake()) method draws the snake and the enlongment of the snake in a
Display_snake()) also prints out the score and how many apples are left too.
printf(" apple(s) :%2.d/%2.d ",apple,(((lines*columns)/30)+6));
The keyPressed() Method
keyPressed() method simply gets the commands issued by the user from the keyboard and sets the direction of the snake. Use the <LEFT>, <RIGHT>, <UP> and <DOWN> keys to steer the snake.
case RIGHT :
}else x-- ;
}else x++ ;
case DOWN :
}else y-- ;
case UP :
}else y++ ;
case SPACE : getch() ; break;
The Main File
Finally the CSnake_main.cpp file is where we create an instance for our class and run our Snake game.
CSnake *snake = new CSnake(1);
Thanks for reading.