C++ Snake Game (Simple!) - Instructables
C++ Snake Game (Simple!) - Instructables
)
By Circuitalist in CircuitsSoftware
the snake game is a very popular one, here is a very simple one written in C++ using Visual Studio
the code is only 150 line and can be modified in several ways
Enjoy!
////////////////////////////////////////////////////
You can browse and buy materials from my Amazon Store with the same price. This way I get a small
commission:
////////////////////////////////////////////////////
Step 1: Watch on Youtube...
Step 2: Coding...
bool gameover;
int nTail;
eDirecton dir;
void Setup() {
gameover = false;
dir = STOP;
x = width / 2;
y = height / 2;
void Draw() {
system("cls");
if (j == 0)
if (i == y && j == x)
else {
if (j == width -1)
}
void Input ()
{
if (_kbhit ()) {
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN ;
break;
case 'x':
gameover = true;
break;
void algorithm()
{
tailX[0] = x;
tailY[0] = y;
for(int i = 1;i < nTail ; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y ;
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
score +=10;
nTail ++;
int main()
{
Setup();
while (!gameover) {
Draw ();
Input ();
algorithm ();
return 0;