Oggi vediamo come programmare il gioco snake in linguaggio c++. Come sicuramente saprai, per giocare allo snake occorre utilizzare i cursori freccia aiutando il serpente a mangiare il cibo facendo quindi crescere evitando di far sbattere il capo con il suo stesso corpo.

Di seguito il codice sorgente del gioco Snake in Linguaggio C++


/*------------------------------------------------------------------------------
Snake Game

Programmer : Simone Tocco

Software Details
====================
Developed in : C++
OS : Window 98,2000,XP,
Vista,Windows7

-------------------------------------------------------------------------------------*/

//************************************************
// Header Files
//************************************************

#include
#include
#include
#include
#include #include

//************************************************
//
//************************************************

#define TRUE 1
#define FALSE 0
#define HORIZONTAL 1
#define VERTICAL 2

//************************************************
// Direction
//************************************************

#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3

//************************************************
//
//************************************************

#define SNK_BODY_COLOR GREEN
#define SNK_FACE_COLOR RED
#define BGCOLOR BLACK

#define MAXX 50
#define MAXY 40
#define SX 10
#define SY 10
#define WIDTH 10

//************************************************
// Keys
//************************************************

#define UP_KEY 72 // Up arrow key
#define DOWN_KEY 80 // Down arrow key
#define RIGHT_KEY 77 // Right arrow key
#define LEFT_KEY 75 // Left arrow key
#define PAUSE_KEY 32 // Space Bar Key
#define EXIT_KEY 27 // Escape Key

//************************************************
// Global variables
//************************************************

int snk_x[100], snk_y[100], length=8, l_k = RIGHT, xf, yf, eat=0, ch;
int chapter=1,level=1;
int isGamePause=FALSE;
long int score=0;
int timedelay=150;
int sXBlock[30]; // starting horizontal block position
int sYBlock[30];// starting vertical block position
int blockDir[30];//direction of block(horizontal or vertical)
int nBlock[30]; // Number of blocks vertically or horizontally
int nblock=0; // total number of blocks in one level

int starsX[200], starsY[200];

// Function Prototype
void mainMenu();

//************************************************
// Userdefine Functions
//************************************************

void initStars()
{
int sX, sY;
for(int g=0;g<200;g++)
{
sX=random(getmaxx());
sY=random(getmaxy());
starsX[g]=sX;
starsY[g]=sY;
}

}

void drawStars()
{
for(int g=0;g<200;g++) putpixel(starsX[g],starsY[g],random(getmaxcolor())); } void initSnkPosition(int x, int y) { for(int i=length-1;i>=0;i--)
{
snk_x[i]=x-i;
snk_y[i]=y;
}

}

int isSnkHitItself()
{

for(int i=length-1;i>1;i--)
if(snk_x[0]==snk_x[i]&&snk_y[0]==snk_y[i])
return TRUE;
return FALSE;
}

void drawSnake()
{
for(int i=length-1;i>0;i--)
{
//Body
setfillstyle(SOLID_FILL,SNK_BODY_COLOR);
fillellipse((SX+snk_x[i]*WIDTH)+WIDTH/2,(SY+snk_y[i]*WIDTH)+WIDTH/2,WIDTH/2,WIDTH/2);
//Outline
setfillstyle(SOLID_FILL,14);
fillellipse((SX+snk_x[i]*WIDTH)+WIDTH/2,(SY+snk_y[i]*WIDTH)+WIDTH/2,2,2);

}

//Face
setfillstyle(SOLID_FILL,SNK_FACE_COLOR);
fillellipse((SX+snk_x[0]*WIDTH)+WIDTH/2,(SY+snk_y[0]*WIDTH)+WIDTH/2,WIDTH/2,WIDTH/2);
// Eye
setcolor(WHITE);
setfillstyle(SOLID_FILL,BLACK);
fillellipse((SX+snk_x[0]*WIDTH)+WIDTH/2,(SY+snk_y[0]*WIDTH)+WIDTH/2,1,1);

}

void moveSnake()
{
for(int i=length-1;i>0;i--)
{
snk_x[i]=snk_x[i-1];
snk_y[i]=snk_y[i-1];
}
}

void hideLastPart()
{

// Hide the last body part

setcolor(BGCOLOR);
setfillstyle(SOLID_FILL,BGCOLOR);
fillellipse((SX+snk_x[length-1]*WIDTH)+WIDTH/2,(SY+snk_y[length-1]*WIDTH)+WIDTH/2,WIDTH/2,WIDTH/2);

}

int isCollide()
{
if((xf==snk_x[0]) && (yf==snk_y[0]))
return TRUE;
return FALSE;
}

int isSnkHitBlock()
{
for(int i=1;i<=nblock;i++)
{
for(int j=0;j<nBlock[i];j++)
{
if(blockDir[i]==HORIZONTAL)
{
if(snk_x[0]==sXBlock[i]+j && snk_y[0]==sYBlock[i])
return TRUE;
}
else if(blockDir[i]==VERTICAL)
{
if(snk_x[0]==sXBlock[i] && snk_y[0]==sYBlock[i]+j)
return TRUE;
}
}
}

return FALSE;
}

void checkFoodOnBlock()
{
for(int i=1;i<=nblock;i++)
{
for(int j=0;j<nBlock[i];j++)
{
if(blockDir[i]==HORIZONTAL)
{
if(xf==sXBlock[i]+j && yf==sYBlock[i])
{
xf=20;
yf=20;
}
}
else if(blockDir[i]==VERTICAL)
{
if(xf==sXBlock[i] && yf==sYBlock[i]+j)
{
xf=20;
yf=20;
}
}
}
}
}

void getFood(void)
{
static int color;
if(eat==0)
{

xf=random(MAXX-4)+2;
yf=random(MAXY-4)+2;
eat=1;
color= random(13)+2;
checkFoodOnBlock();
}

setcolor(DARKGRAY);
setfillstyle(SOLID_FILL,color);
circle((SX+xf*WIDTH)+WIDTH/2,(SY+yf*WIDTH)+WIDTH/2,WIDTH/2);
floodfill((SX+xf*WIDTH)+WIDTH/2,(SY+yf*WIDTH)+WIDTH/2,DARKGRAY);
setcolor(BGCOLOR);

if(isCollide())
{
char sData[20];
sound(2000);
delay(30);
nosound();
settextstyle(0,0,0);
setcolor(0);
outtextxy(550,270,itoa(score,sData,10));
score=score+9+chapter+level;
length++;
eat=0;
setcolor(3);
outtextxy(550,270,itoa(score,sData,10));

}
setcolor(BGCOLOR);
}

void text3D(int x, int y, int depth, int bgcolor, int fgcolor, char *caption)
{
int i;
setcolor(bgcolor);
for(i=0;i<depth;i++)
outtextxy(x+i,y+i,caption);
setcolor(fgcolor);
outtextxy(x+i,y+i,caption);

}

void gameOver()
{

char buffer[20];

cleardevice();

settextstyle(0,0,7);
text3D(100,150,4,3,11,"Game Over");
settextstyle(0,0,4);
text3D(180,300,2,3,11,"Score : ");
text3D(180+textwidth("Score : "),300,2,3,11,itoa(score,buffer,10));
settextstyle(0,0,2);
text3D(100,400,2,3,11,"Press any key to continue...");
settextstyle(0,0,0);
for(int j=0;j<2000;j++) putpixel(random(640),random(480),random(16)); getch(); mainMenu(); } void snakeTouchesWall() { // If snake touches the wall if(snk_x[0]==0) { snk_x[0]=MAXX-3; } if(snk_x[0]==MAXX-2) { snk_x[0]=1; } if(snk_y[0]==0) { snk_y[0]=MAXY-3; } if(snk_y[0]==MAXY-2) { snk_y[0]=1; } } void keyEvents( int ch) { // Key events if(ch==PAUSE_KEY) { setcolor(0); outtextxy(518,370,"PLAYING..."); setcolor(3); outtextxy(520,370,"PAUSED..."); while(1) { if(kbhit()) { int key=getch(); if(key==PAUSE_KEY) { setcolor(0); outtextxy(520,370,"PAUSED..."); setcolor(3); outtextxy(518,370,"PLAYING..."); break; } else if(key==EXIT_KEY) exit(0); } } } if(ch==EXIT_KEY) gameOver(); else if(ch==UP_KEY && l_k!=DOWN) l_k=UP; else if(ch==DOWN_KEY && l_k!=UP) l_k=DOWN; else if(ch==RIGHT_KEY && l_k!=LEFT) l_k=RIGHT; else if(ch==LEFT_KEY && l_k!=RIGHT) l_k=LEFT; } void moveTheSnake() { // move the snake if(l_k==RIGHT) { snk_x[0]++; } if(l_k==LEFT) { snk_x[0]--; } if(l_k==UP) { snk_y[0]--; } if(l_k==DOWN) { snk_y[0]++; } } void mainScreen() { char sData[20]; cleardevice(); setcolor(12); rectangle(10,10,500,400); setcolor(3); rectangle(11,11,499,399); setcolor(12); rectangle(12,12,498,398); setcolor(2); rectangle(510,10,600,100); setcolor(4); rectangle(510,110,600,200); setcolor(12); rectangle(510,210,600,300); setcolor(13); rectangle(510,310,600,400); settextstyle(0,0,0); setcolor(15); outtextxy(535,30,"LEVEL"); outtextxy(535,130,"CHAPTER"); outtextxy(535,230,"SCORE"); outtextxy(535,330,"STATUS"); settextstyle(0,0,5); text3D(100,420,3,3,11,"Snake Game"); settextstyle(0,0,0); setcolor(3); if(level==1) { outtextxy(530,70,"BEGINNER"); timedelay=150; } else if(level==2) { outtextxy(532,70,"ADVANCE"); timedelay=100; } else if(level==3) { outtextxy(535,70,"EXPERT"); timedelay=50; } outtextxy(550,170,itoa(chapter,sData,10)); outtextxy(550,270,itoa(score,sData,10)); outtextxy(518,370,"PLAYING..."); initStars(); } void snakeCaught() { nosound(); for(int x=500;x>0;x--)
{ sound(x);
delay(1);
setcolor(random(13)+1);
drawSnake();
}
nosound();
}

void drawBlock(int xb, int yb, int n, int dir)
{
nblock++;
sXBlock[nblock]=xb;
sYBlock[nblock]=yb;
nBlock[nblock]=n;
blockDir[nblock]=dir;
for(int i=1;i<=n;i++)
{
setfillstyle(SOLID_FILL,12);
bar((SX+xb*WIDTH)+1,(SY+yb*WIDTH)+1,(SX+xb*WIDTH)+WIDTH-1,(SY+yb*WIDTH)+WIDTH-1);
if(dir==HORIZONTAL)
xb++;
else if(dir==VERTICAL)
yb++;
}

}

void reset()
{
nblock=0;
length=8;
score=0;
l_k = RIGHT;
for(int i=0;i<30;i++)
{
sXBlock[i]=0;
sYBlock[i]=0;
blockDir[i]=0;
nBlock[i]=0;
}
}

/*-----------------------------------------------------------------*/

void JClogo()
{

/*********** Logo ************/

setcolor(3);
setfillstyle(SOLID_FILL,3);
circle(280,180,30);
floodfill(280,180,3);

setcolor(15);
setfillstyle(SOLID_FILL,15);
circle(280,180,27);
circle(280,180,24);
floodfill(254,180,15);

settextstyle(1,0,4);
outtextxy(265,160,"JC");
/******************************/

setcolor(15);
settextstyle(0,0,2);
text3D(200,240,2,3,11,"Just Coding");
text3D(220,290,2,3,11,"Presents");
text3D(70,390,2,3,11,"www.justdocodings.blogspot.in");

settextstyle(0,0,0);

}

//***************************************************************
// Starting Screen
//****************************************************************

void Start()
{

settextstyle(0,0,2);
text3D(230,200,2,3,11,"Loading...");
settextstyle(0,0,0);
for(int i=0;i<1000;i++)
putpixel(random(640),random(480),random(16));
delay(4000);
cleardevice();

JClogo();
for(int l=0;l<1000;l++)
putpixel(random(640),random(480),random(16));

delay(5000);
cleardevice();

settextstyle(0,0,2);
text3D(230,200,2,3,11,"a game by");
text3D(250,240,2,3,11,"Manish Kumar");
settextstyle(0,0,0);
for(int k=0;k<1000;k++)
putpixel(random(640),random(480),random(16));

delay(4000);
cleardevice();

settextstyle(0,0,7);
text3D(50,150,4,3,11,"SNAKE GAME");
settextstyle(0,0,2);
text3D(100,300,2,3,11,"(c) Copyright 2018 Manish Kumar");
settextstyle(0,0,0);
for(int j=0;j<2000;j++)
putpixel(random(640),random(480),random(16));
delay(6000);
cleardevice();

}

//***************************************************************
// Introduction Function
//****************************************************************

void Intro()
{

cleardevice();
setcolor(15);
settextstyle(0,0,4);
for(int i=0;i<4;i++)
outtextxy(260+i,30+i,"HELP");
settextstyle(0,0,2);
setcolor(4);
for(int k=0;k<=3;k++)
{
outtextxy(100+k,100+30-k,"Left Key : Move Left");
outtextxy(100+k,130+30-k,"Right Key : Move Right");
outtextxy(100+k,160+30-k,"Up Key : Move Up");
outtextxy(100+k,190+30-k,"Down Key : Move Down");
outtextxy(100+k,220+30-k,"Space : Pause");
outtextxy(100+k,250+30-k,"Esc : Return");

outtextxy(100+k,380+30-k,"Press any key to continue...");
if(k==2)
setcolor(12);
}
for(int _i=0;_i<1000;_i++)
putpixel(random(640),random(480),random(16));
getch();
}

/*-------------------------------------------------------------------------*/

void chapterOne()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE)
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterTwo()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(8,10,30,HORIZONTAL);
drawBlock(8,30,30,HORIZONTAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterThree()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(3,3,3,HORIZONTAL);
drawBlock(3,4,2,VERTICAL);

drawBlock(3,35,3,HORIZONTAL);
drawBlock(3,33,2,VERTICAL);

drawBlock(MAXX-7,3,3,HORIZONTAL);
drawBlock(MAXX-5,4,2,VERTICAL);

drawBlock(MAXX-7,35,3,HORIZONTAL);
drawBlock(MAXX-5,33,2,VERTICAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterFour()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(0,0,MAXX-1,HORIZONTAL);
drawBlock(0,MAXY-2,MAXX-1,HORIZONTAL);
drawBlock(0,1,MAXY-3,VERTICAL);
drawBlock(MAXX-2,1,MAXY-3,VERTICAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterFive()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(8,10,30,HORIZONTAL);
drawBlock(8,30,30,HORIZONTAL);

drawBlock(3,3,3,HORIZONTAL);
drawBlock(3,4,2,VERTICAL);

drawBlock(3,35,3,HORIZONTAL);
drawBlock(3,33,2,VERTICAL);

drawBlock(MAXX-7,3,3,HORIZONTAL);
drawBlock(MAXX-5,4,2,VERTICAL);

drawBlock(MAXX-7,35,3,HORIZONTAL);
drawBlock(MAXX-5,33,2,VERTICAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterSix()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(0,0,MAXX-1,HORIZONTAL);
drawBlock(0,MAXY-2,MAXX-1,HORIZONTAL);

drawBlock(0,1,16,VERTICAL);
drawBlock(MAXX-2,1,16,VERTICAL);

drawBlock(0,21,17,VERTICAL);
drawBlock(MAXX-2,21,17,VERTICAL);

drawBlock(8,10,30,HORIZONTAL);
drawBlock(8,30,30,HORIZONTAL);

drawBlock(3,3,3,HORIZONTAL);
drawBlock(3,4,2,VERTICAL);

drawBlock(3,35,3,HORIZONTAL);
drawBlock(3,33,2,VERTICAL);

drawBlock(MAXX-7,3,3,HORIZONTAL);
drawBlock(MAXX-5,4,2,VERTICAL);

drawBlock(MAXX-7,35,3,HORIZONTAL);
drawBlock(MAXX-5,33,2,VERTICAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterSeven()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(0,0,MAXX-1,HORIZONTAL);
drawBlock(0,MAXY-2,MAXX-1,HORIZONTAL);

drawBlock(0,1,16,VERTICAL);
drawBlock(MAXX-2,1,16,VERTICAL);

drawBlock(0,21,17,VERTICAL);
drawBlock(MAXX-2,21,17,VERTICAL);

drawBlock(8,10,20,VERTICAL);
drawBlock(40,12,20,VERTICAL);

drawBlock(3,3,3,HORIZONTAL);
drawBlock(3,4,2,VERTICAL);

drawBlock(3,35,3,HORIZONTAL);
drawBlock(3,33,2,VERTICAL);

drawBlock(MAXX-7,3,3,HORIZONTAL);
drawBlock(MAXX-5,4,2,VERTICAL);

drawBlock(MAXX-7,35,3,HORIZONTAL);
drawBlock(MAXX-5,33,2,VERTICAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterEight()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(0,0,MAXX-1,HORIZONTAL);
drawBlock(0,MAXY-2,MAXX-1,HORIZONTAL);

drawBlock(6,10,16,VERTICAL);
drawBlock(MAXX-9,20,15,VERTICAL);

drawBlock(12,10,20,VERTICAL);
drawBlock(35,12,20,VERTICAL);

drawBlock(3,3,3,HORIZONTAL);
drawBlock(3,4,2,VERTICAL);

drawBlock(3,35,3,HORIZONTAL);
drawBlock(3,33,2,VERTICAL);

drawBlock(MAXX-7,3,3,HORIZONTAL);
drawBlock(MAXX-5,4,2,VERTICAL);

drawBlock(MAXX-7,35,3,HORIZONTAL);
drawBlock(MAXX-5,33,2,VERTICAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterNine()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(0,0,MAXX-1,HORIZONTAL);
drawBlock(0,MAXY-2,MAXX-1,HORIZONTAL);

drawBlock(4,7,15,VERTICAL);
drawBlock(MAXX-7,7,15,VERTICAL);

drawBlock(20,21,16,VERTICAL);
drawBlock(MAXX-14,21,16,VERTICAL);

drawBlock(8,10,20,VERTICAL);
drawBlock(40,12,20,VERTICAL);

drawBlock(3,3,3,HORIZONTAL);

drawBlock(3,35,3,HORIZONTAL);

drawBlock(MAXX-7,3,3,HORIZONTAL);

drawBlock(MAXX-7,35,3,HORIZONTAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void chapterTen()
{
int isGameOver=FALSE;

initSnkPosition(20,20);

drawBlock(0,0,MAXX-1,HORIZONTAL);
drawBlock(0,MAXY-2,MAXX-1,HORIZONTAL);

drawBlock(0,1,MAXY-3,VERTICAL);
drawBlock(MAXX-2,1,MAXY-3,VERTICAL);

drawBlock(11,10,27,HORIZONTAL);
drawBlock(11,30,27,HORIZONTAL);

drawBlock(8,10,20,VERTICAL);
drawBlock(40,10,20,VERTICAL);

drawBlock(3,3,3,HORIZONTAL);
drawBlock(3,4,2,VERTICAL);

drawBlock(3,35,3,HORIZONTAL);
drawBlock(3,33,2,VERTICAL);

drawBlock(MAXX-7,3,3,HORIZONTAL);
drawBlock(MAXX-5,4,2,VERTICAL);

drawBlock(MAXX-7,35,3,HORIZONTAL);
drawBlock(MAXX-5,33,2,VERTICAL);

while(1)
{

setcolor(12);
rectangle(10,10,500,400);
setcolor(3);
rectangle(11,11,499,399);
setcolor(12);
rectangle(12,12,498,398);
setcolor(0);

drawStars();
moveSnake();
drawSnake();
hideLastPart();
getFood();

delay(timedelay);

snakeTouchesWall();
moveTheSnake();

if(isSnkHitItself()==TRUE || isSnkHitBlock())
{
isGameOver=TRUE;
break;
}

while(kbhit())
keyEvents(ch=getch());

}
if(isGameOver)
{
setcolor(0);
outtextxy(518,370,"PLAYING...");
setcolor(3);
outtextxy(520,370,"Game Over");
snakeCaught();
gameOver();
}

}

void SelectChapter()
{
reset();
mainScreen();
switch(chapter)
{
case 1:
chapterOne();
break;
case 2:
chapterTwo();
break;
case 3:
chapterThree();
break;
case 4:
chapterFour();
break;
case 5:
chapterFive();
break;
case 6:
chapterSix();
break;
case 7:
chapterSeven();
break;
case 8:
chapterEight();
break;
case 9:
chapterNine();
break;
case 10:
chapterTen();
break;
default:
chapterOne();
break;
}

}

/*--------------------------------------------------------------*/

void chapterMenu()
{
int _x=220,_y=50,_h=30;
int cY=_y+2*_h;
int choice=1;
int key;
cleardevice();
do{

settextstyle(0,0,4);
text3D(_x-70,_y,2,3,11,"CHAPTER MENU");
settextstyle(0,0,2);
text3D(_x,_y+2*_h,1,3,11,"1. Chapter 1");
text3D(_x,_y+3*_h,1,3,11,"2. Chapter 2");
text3D(_x,_y+4*_h,1,3,11,"3. Chapter 3");
text3D(_x,_y+5*_h,1,3,11,"4. Chapter 4");
text3D(_x,_y+6*_h,1,3,11,"5. Chapter 5");
text3D(_x,_y+7*_h,1,3,11,"6. Chapter 6");
text3D(_x,_y+8*_h,1,3,11,"7. Chapter 7");
text3D(_x,_y+9*_h,1,3,11,"8. Chapter 8");
text3D(_x,_y+10*_h,1,3,11,"9. Chapter 9");
text3D(_x,_y+11*_h,1,3,11,"10. Chapter 10");
text3D(_x,_y+12*_h,1,3,11,"11. Main Menu");
for(int j=0;j<3000;j++) putpixel(random(640),random(480),random(16)); setcolor(12); outtextxy(_x-50,cY,">>");

do{
key=getch();

if(key==UP_KEY)
{
setcolor(0);
outtextxy(_x-50,cY,">>");

cY-=_h;
choice--;

if(choice<1) { choice=11; cY =_y+12*_h; } setcolor(12); outtextxy(_x-50,cY,">>");

sound(1000);
delay(20);
nosound();

}
else if(key==DOWN_KEY)
{
setcolor(0);
outtextxy(_x-50,cY,">>");
cY+=_h;
choice++;
if(choice>11)
{
choice=1;
cY=_y+2*_h;
}

setcolor(12);
outtextxy(_x-50,cY,">>");

sound(1000);
delay(20);
nosound();

}

}while(key!=13);

switch(choice)
{
case 1:
chapter=1;
choice=0;
break;
case 2:
chapter=2;
choice=0;
break;
case 3:
chapter=3;
choice=0;
break;
case 4:
chapter=4;
choice=0;
break;
case 5:
chapter=5;
choice=0;
break;
case 6:
chapter=6;
choice=0;
break;
case 7:
chapter=7;
choice=0;
break;
case 8:
chapter=8;
choice=0;
break;
case 9:
chapter=9;
choice=0;
break;
case 10:
chapter=10;
choice=0;
break;
case 11:
mainMenu();
break;

}

}while(choice!=0);
cleardevice();
SelectChapter();
}
void mainMenu()
{
int _x=220,_y=100,_h=30;
int cY=_y+2*_h;
int choice=1;
int key;
cleardevice();
do{

settextstyle(0,0,4);
text3D(_x-70,_y,2,3,11,"MAIN MENU");
settextstyle(0,0,2);
text3D(_x,_y+2*_h,1,3,11,"1. BEGINNER");
text3D(_x,_y+3*_h,1,3,11,"2. ADVANCE");
text3D(_x,_y+4*_h,1,3,11,"3. EXPERT");
text3D(_x,_y+5*_h,1,3,11,"4. Exit");
for(int j=0;j<3000;j++) putpixel(random(640),random(480),random(16)); setcolor(12); outtextxy(_x-50,cY,">>");

do{
key=getch();

if(key==UP_KEY)
{
setcolor(0);
outtextxy(_x-50,cY,">>");

cY-=_h;
choice--;

if(choice<1) { choice=4; cY =_y+5*_h; } setcolor(12); outtextxy(_x-50,cY,">>");

sound(1000);
delay(20);
nosound();

}
else if(key==DOWN_KEY)
{
setcolor(0);
outtextxy(_x-50,cY,">>");
cY+=_h;
choice++;
if(choice>4)
{
choice=1;
cY=_y+2*_h;
}

setcolor(12);
outtextxy(_x-50,cY,">>");

sound(1000);
delay(20);
nosound();

}

}while(key!=13);

switch(choice)
{
case 1:
level=1;
choice=0;
break;
case 2:
level=2;
choice=0;
break;
case 3:
level=3;
choice=0;
break;
case 4:
closegraph();
restorecrtmode();
clrscr();
cout<<"\n\n\t\t Thank You for playing Snake Game.";
cout<<"\n\n\tPlease visit our hompage : www.justdocodings.blogspot.in\n\n\n";
exit(0);
break;

}

}while(choice!=0);
chapterMenu();
}

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");

Start();
Intro();
mainMenu();
}

Nella parte superiore del codice vengono definite quattro costanti per il controller. Successivamente vengono dichiarati diversi prototipi di funzione che invocheranno le opportuni funzioni a tempo debito. All’avvio System (cls) elimina le finestre da riga di comando per consentire l’esecuzione del gioco snake.  Il metodo di avvio viene utilizzato per avviare il gioco. Successivamente è possibile selezionare il livello di gioco dato in input dall’utente. Il metodo START viene utilizzato per dichiarare un’istruzione goto. Quando il serpente colpisce il muro, la funzione viene interrotta e l’istanza di gioco viene distrutta.

Lascia un commento