You are on page 1of 6

instructables

C++ Snake Game (Simple!)

by Circuitalist

the snake game is a very popular one, here is a very You can browse and buy materials from my Amazon
simple one written in C++ using Visual Studio Store with the same price. This way I get a small
commission:
the code is only 150 line and can be modified in
several ways C++ How to Program (10th Edition)

Enjoy! Beginning C++ Through Game Programming

//////////////////////////////////////////////////// Starting out with Visual C# (4th Edition)

You want to support my videos? ////////////////////////////////////////////////////

Step 1: Watch on Youtube...

https://www.youtube.com/embed/Kj0e10helxk

C++ Snake Game (Simple!): Page 1


Step 2: Coding...

C++ Snake Game (Simple!): Page 2


#include < iostream > void Draw() {
system("cls");
#include < conio.h >
for(int i = 0; i < width+2; i++)
using namespace std;
cout << "#";
bool gameover;
cout << endl ;
const int width = 20;
for (int i = 0; i < height ; i++) {
const int height = 17;
for (int j = 0; j < width; j++) {
int x, y, fruitX, fruitY, score;
if (j == 0)
int tailX[100], tailY[100]; //snake coordinates
cout << "#"; //walls
int nTail;
if (i == y && j == x)
enum eDirecton {STOP = 0, LEFT,RIGHT, UP,
DOWN}; // Controls cout << "*"; // snake tale

eDirecton dir; else if (i == fruitY && j == fruitX )

void Setup() { cout << "%"; // change it to change the fruit


gameover = false;
else {
dir = STOP;
bool print = false;
x = width / 2;
for (int k = 0; k< nTail ; k++) {
y = height / 2;
if (tailX [k] == j && tailY [k] == i) {
fruitX = rand() % width; //display fruit in a random
place cout << "*"; print = true;

fruitY = rand() % height; score = 0; }

} }

C++ Snake Game (Simple!): Page 3


if (!print) cout << " "; case 's':

} dir = DOWN ;

if (j == width -1) break;

cout << "#"; case 'x':

} gameover = true;

cout << endl; break;

} }

for (int i = 0; i< width+2; i++) }

cout << "#"; }

cout << endl; void algorithm()


{
cout << "Score:" << score << endl ;
int prevX = tailX [0];
}
int prevY = tailY [0];
void Input ()
{ int prev2X, prev2Y;

if (_kbhit ()) { tailX[0] = x;

switch (_getch ()) { tailY[0] = y;

case 'a': for(int i = 1;i < nTail ; i++) {

dir = LEFT; prev2X = tailX[i];

break; prev2Y = tailY[i];

case 'd': tailX[i] = prevX;

dir = RIGHT; tailY[i] = prevY;

break; prevX = prev2X;

case 'w': prevY = prev2Y ;

dir = UP; }

break; switch (dir) {

C++ Snake Game (Simple!): Page 4


case LEFT: }

x--; }

break; int main()


{
case RIGHT:
Setup();
x++;
while (!gameover) {
break;
Draw ();
case UP:
Input ();
y--;
algorithm ();
break;
}
case DOWN:
return 0;
y++;
}
break;

default:

break;

if (x >= width) x =0;else if (x <0) x = width -1;

if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i =0; i< nTail ;i++)

if (tailX[i] == x && tailY[i] == y)


gameover = true;

if (x == fruitX && y == fruitY) {

score +=10;

fruitX = rand() % width;

fruitY = rand() % height;

nTail ++;

C++ Snake Game (Simple!): Page 5


C++ Snake Game (Simple!): Page 6

You might also like