Dokumen - Tips - Computer Project For Class Xii Topic The Snake Game
Dokumen - Tips - Computer Project For Class Xii Topic The Snake Game
1. Overview of C++ 1
4. File Handling 3
7. Conclusion 31
8. Bibliography 31
CERTIFICATE OF MERIT
This is to certify that this project work on “Snake – The Game” submitted by Pritam Samanta of
class 12 'B' of DAV Sushil Kedia Vishwa Bharati Higher Secondary School, was carried out by him
under the guidance and supervision during the academic session 2016-2017.
C++ is a superset of C, and that virtually any legal C program is a legal C++
program and “Its main purpose was to make writing good programs easier
and more pleasant for the individual programmer.”
Object-Oriented Programming
C++ fully supports object-oriented programming, including the four pillars of
object-oriented development:
Encapsulation
Data hiding
Inheritance
Polymorphism
Standard Libraries
Standard C++ consists of three important parts:
The core language giving all the building blocks including variables,
data types and literals, etc.
The C++ Standard Library giving a rich set of functions manipulating
files, strings, etc.
The Standard Template Library (STL) giving a rich set of methods
manipulating data structures, etc.
Page | 1
The ANSI Standard
The ANSI standard is an attempt to ensure that C++ is portable -- that code
you write for Microsoft's compiler will compile without errors, using a compiler
on a Mac, UNIX, a Windows box, or an Alpha.
The ANSI standard has been stable for a while, and all the major C++
compiler manufacturers support the ANSI standard.
Learning C++
The most important thing to do when learning C++ is to focus on concepts
and not get lost in language technical details.
C++ supports a variety of programming styles. You can write in the style of
Fortran, C, Smalltalk, etc., in any language. Each style can achieve its aims
effectively while maintaining runtime and space efficiency.
Use of C++
C++ is used by hundreds of thousands of programmers in essentially every
application domain.
C++ is being highly used to write device drivers and other softwares that rely
on direct manipulation of hardware under real-time constraints.
C++ is widely used for teaching and research because it is clean enough for
successful teaching of basic concepts.
Page | 2
Header files used in the program
1. #include<iostream>
Basic header file used in C++ for input output stream.
2. #include<stdlib.h>
This header file is required to declare standard library functions.
3. #include<conio.h>
This header file is used for console input output stream.
4. #include<ctime>
This header file is used for time related function.
5. #include<fstream>
This header file is used for file handling.
6. #include<stdio.h>
This is header file is used of standard input output stream.
7. #include<windows.h>
This is header file is used for windows related function.
Page | 3
Source Code
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<ctime>
#include<fstream>
#include<stdio.h>
#include<windows.h>
class getScore
public:
int score,highScore;
getScore()
score=highScore=0;
};
void run();
void printMap();
void initMap();
Page | 4
void update();
void generateFood();
void changeDirection();
void flashColor();
void maxConsoleScreen(void);
void hideCur();
int getScreenSize();
void displaySnake1();
void displaySnake();
void displayCredits();
void saveScores();
void displayScores();
void menuScreen();
void forScore();
void printInstruction();
// Map dimensions
Page | 5
// The tile values for the map
int map[size];
int headxpos;
int headypos;
int direction;
// Amount of food the snake has (How long the body is)
int food = 3;
//For score
int score1 = 0;
// For level up
int levelUp = 0;
bool running;
long sizes;
int rows,cols;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
Page | 6
void maxConsoleScreen(void) //To maximize the console screen
HANDLE cmd;
CONSOLE_CURSOR_INFO cur;
char *str=(char*)malloc(32);
cur.dwSize=1;
cur.bVisible=FALSE;
cmd=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(cmd,&cur);
int col,row,total;
maxConsoleScreen();
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&csbi);
col=csbi.srWindow.Right-csbi.srWindow.Left+1;
row=csbi.srWindow.Bottom-csbi.srWindow.Top+1;
total=col*1000+row;
return total;
Page | 7
// Main game function
void run()
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
initMap();
running = true;
while (running)
// If a key is pressed
if (kbhit())
changeDirection();
update();
system("cls");
printMap();
_sleep(250);
Page | 8
// Saves the scores
saveScores();
flashColor();
gotoxy(rows+9,cols+36);cols++;
cout<<"___________________________________________________";
gotoxy(rows+30,cols+36); cols+=2;
getch();
void flashColor()
sizes=getScreenSize();
rows=sizes%1000;
cols=sizes/1000;
Page | 9
cols=cols/15;
rows=(44*rows)/45+5;
system("cls");
displaySnake1();
long size;
int row,col;
size=getScreenSize();
row=size%1000;
col=size/1000;
col=(col/3)-5;
row=row/2;
gotoxy(col-1,row); cout<<"Classic snake game! Player can move the snake all around
the enclosed box."; row++;
gotoxy(col,row); cout<<"Player can use WASD keys or the arrow keys to control the
snake. Press W ";row++;
gotoxy(col-1,row); cout<<"or up arrow key to go up, press A or left arrow key to go left,
press S or"; row++;
gotoxy(col-1,row); cout<<"slight problem with the arrows keys. Bonus food appears only
after player"; row++;
gotoxy(col+9,row+3); cout<<"_______________________________________________";
getch();
menuScreen();
Page | 10
}
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
/*
A+D
*/
switch (getch())
case 'w':
if (direction != 2) direction = 0;
break;
case 'd':
if (direction != 3) direction = 1;
break;
case 's':
if (direction != 4) direction = 2;
break;
case 'a':
if (direction != 5) direction = 3;
break;
case 'W':
if (direction != 2) direction = 0;
Page | 11
break;
case 'D':
if (direction != 3) direction = 1;
break;
case 'S':
if (direction != 4) direction = 2;
break;
case 'A':
if (direction != 5) direction = 3;
break;
if (direction != 5) direction = 3;
break;
if (direction != 3) direction = 1;
break;
if (direction != 2) direction = 0;
break;
if (direction != 4) direction = 2;
break;
case 27:
gotoxy(rows+20,cols+16);
getch();
break;
Page | 12
// Moves snake head to new location
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
food++; levelUp++;
levelUp=0;
score1=score1+3;
else
score1++;
generateFood();
Page | 13
// Check location is free
running = false;
headxpos = newx;
headypos = newy;
void generateFood()
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
srand(time(0));
int x = 0;
int y = 0;
do {
x = rand() % (mapwidth - 2) + 1;
y = rand() % (mapheight - 2) + 1;
Page | 14
// Place new food
void update()
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
switch (direction)
break;
break;
break;
break;
Page | 15
// Initializes map
void initMap()
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
headxpos = mapwidth / 2;
headypos = mapheight / 2;
map[x] = -1;
generateFood();
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
long size;
int row,col;
size=getScreenSize();
row=size%1000;
col=size/1000;
col=col/15;
row=(44*row)/45;
gotoxy(row+5,col);
srand(time(0));
Page | 17
int randFood;
switch(value)
return 'X';
case -2:
return char(162);
randFood = rand()%10+1;
if(randFood==1)
return char(145);
if(randFood==2)
return char(148);
if(randFood==3)
return char(235);
if(randFood==4)
return char(237);
if(randFood==5)
return char(232);
if(randFood==6)
return char(233);
if(randFood==7)
return char(149);
Page | 18
if(randFood==8)
return char(236);
if(randFood==9)
return char(167);
if(randFood==10)
return char(234);
gotoxy(x,y); cout<<"|___________|"<<endl;
Page | 19
void displayA(int x, int y)
gotoxy(x,y); cout<<"|_________|"<<endl;
Page | 20
void displaySnake1() //To display SNAKE in screen without delay
long size;
int row,col;
size=getScreenSize();
row=size%1000;
col=size/1000;
row=row/3;
col=col/4;
displayS(col,row);
displayN(col+19,row);
displayA(col+45,row);
displayK(col+67,row);
displayE(col+85,row);
long size;
int row,col;
size=getScreenSize();
row=size%1000;
col=size/1000;
row=row/3;
col=col/4; _sleep(300);
displayS(col,row); _sleep(300);
displayN(col+19,row); _sleep(300);
displayA(col+45,row); _sleep(300);
displayK(col+67,row); _sleep(300);
displayE(col+85,row);
Page | 21
gotoxy(col+30,row+17);
_sleep(300);
getch();
displaySnake1();
long size;
int row,col;
size=getScreenSize();
row=size%1000;
col=size/1000;
col=(col/3)-5;
row=row/2;
gotoxy(col,row); cout<<" These are the people because of which this project has been
possible."; row++;
gotoxy(col,row);
cout<<"===================================================================
======"; row+=3;
gotoxy(col+9,row+3); cout<<"_______________________________________________";
getch();
menuScreen();
Page | 22
void saveScores() // This will save scores
fstream fSave,fRead;
getScore scores;
fRead.open("score.dat",ios::in|ios::binary);
fRead.read((char*)&scores,sizeof(scores));
fRead.close();
scores.highScore=score1;
scores.score=score1;
fSave.open("score.dat",ios::out|ios::binary);
fSave.write((char*)&scores,sizeof(scores));
fSave.close();
displaySnake1();
long size;
int row,col;
size=getScreenSize();
row=size%1000;
col=size/1000;
col=(col/3)-5;
row=row/2;
fstream fIn;
getScore score;
fIn.open("score.dat",ios::in|ios::binary);
fIn.read((char*)&score,sizeof(score));
fIn.close();
Page | 23
gotoxy(col+18,row+3); cout<<"Score : "<<score.score;
gotoxy(col+9,row+10); cout<<"_______________________________________________";
getch();
menuScreen();
if(!strcmp(ans,"1"))
return 1;
else if(!strcmp(ans,"2"))
return 2;
else if(!strcmp(ans,"3"))
return 3;
else if(!strcmp(ans,"4"))
return 4;
else if(!strcmp(ans,"5"))
return 5;
else
ans[100]=NULL;
gotoxy(col+3,row+7);
_sleep(1500);
menuScreen();
Page | 24
void menuScreen() // To display menu screen
system("cls");
displaySnake1();
long size;
int select,row,col;
char ans[100];
size=getScreenSize();
row=size%1000;
col=size/1000;
col=col/3+8;
row=row/2;
gets(ans);
select=checkChoice(ans,row,col);
switch(select)
case 1:
system("cls");
getch();
break;
case 2:
system("cls");
Page | 25
displayScores(); //To display scores
break;
case 3:
system("cls");
break;
case 4:
break;
case 5:
break;
int main()
system("COLOR 70");
hideCur();
menuScreen();
Page | 26
Output Screen
Page | 27
Page | 28
Page | 29
Page | 30
Conclusion
Bibliography
Dhanpat Rai & Co. Computer Science Textbook XII
http://www.cplusplus.com/forum/general/7258/
https://social.msdn.microsoft.com/Forums/vstudio/en-
US/5e20d71d-a538-45b7-9cd1-b5b0d95be948/maximizing-
the-c-console-window?forum=vcgeneral
https://msdn.microsoft.com/en-
us/library/windows/desktop/ms648396(v=vs.85).aspx
http://www.gamedev.net/topic/201413-hiding-text-cursor-in-
c-console-applications/
http://www.cplusplus.com/forum/beginner/5830/
http://www.dreamincode.net/forums/topic/99121-time-delay-
output/
Page | 31