0% found this document useful (0 votes)
66 views

C Snake Game Simple

This document provides the code for a simple C++ snake game written in 150 lines of code using Visual Studio. The code includes functions for setting up the game, drawing the game board and snake, getting input, and running the core game algorithm to move the snake and check for collisions. Key aspects include storing the snake coordinates in arrays, randomly generating the fruit location, increasing the snake length when eating fruit, and ending the game if the snake runs into itself or the edges of the board. The full code is broken into multiple pages that show the various functions and main game loop.

Uploaded by

ahmad riza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

C Snake Game Simple

This document provides the code for a simple C++ snake game written in 150 lines of code using Visual Studio. The code includes functions for setting up the game, drawing the game board and snake, getting input, and running the core game algorithm to move the snake and check for collisions. Key aspects include storing the snake coordinates in arrays, randomly generating the fruit location, increasing the snake length when eating fruit, and ending the game if the snake runs into itself or the edges of the board. The full code is broken into multiple pages that show the various functions and main game loop.

Uploaded by

ahmad riza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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