Beruflich Dokumente
Kultur Dokumente
SARVODAYA BAL
VIDYALAYA
E-Block, Nand Nagri, Delhi-93
Project work on
1. Overview of C++ 1
4. File Handling 3
7. Conclusion 31
8. Bibliography 31
GOVT. SARVODAYA BAL
VIDYALAYA
E-Block, Nand Nagri, Delhi-93
CERTIFICATE
2019-2020
This is to certify that this project work on “Snake – The Game” submitted by Ankit of class
12 'A' of Govt. Sarvodaya Bal Vidyalaya, was carried out by him under the guidance and
supervision during the academic session 2019-2020.
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.
Uses 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:
#include<iostream>
Basic header file used in C++ for input output stream.
#include<stdlib.h>
This header file is required to declare standard library functions.
#include<conio.h>
This header file is used for console input output stream.
#include<ctime>
This header file is used for time related function.
#include<fstream>
This header file is used for file handling.
#include<stdio.h>
This is header file is used of standard input output stream.
#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();
changeDirection();
void flashColor();
maxConsoleScreen(void); void
hideCur();
getScreenSize();
displaySnake(); void
displayCredits(); void
saveScores(); void
displayScores();
menuScreen();
void forScore();
void printInstruction();
// Map dimensions
Page | 5
// The tile values for the map int
map[size];
headxpos;
direction;
// Amount of food the snake has (How long the body is) int food
= 3;
score1 = 0;
levelUp = 0;
running;
rows,cols;
void gotoxy(short x,short y) //This is to declare gotoxy() function is a Turbo-C++ Function not a ANSI
function
Page | 6
void maxConsoleScreen(void) //To maximize the console screen
HWND consoleWindow = GetConsoleWindow(); //This gets the value Windows uses to identify the
output window
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);
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();
cout<<" ________________________________________________________";
gotoxy(rows+30,cols+36); cols+=2;
getch();
void flashColor()
sizes=getScreenSize();
rows=sizes%1000;
Page | 9
cols=sizes/1000;
Page | 10
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,row); cout<<"down arrow key to go down, press D or right arrow key to go right. It is";
row++;
gotoxy(col,row); cout<<"highly recommended to use WASD keys instead of arrows keys as there is a ";
row++;
gotoxy(col-1,row); cout<<"slight problem with the arrows keys. Bonus food appears only after player";
row++;
menuScreen();
Page | 11
}
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
/*
A+D
*/
switch (getch())
case 'w':
case 'd':
case 's':
case 'a':
case 'W':
if (direction != 2) direction = 0;
Page | 12
break;
case 'D':
case 'S':
case 'A':
case 27:
gotoxy(rows+20,cols+16);
getch();
break;
Page | 13
// Moves snake head to new location void
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
food++; levelUp++;
levelUp=0;
score1=score1+3;
}
else
score1++;
}
generateFood();
Page | 14
// Check location is free
running = false;
headxpos = newx;
headypos = newy;
generateFood()
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
srand(time(0));
int x = 0;
int y = 0;
do {
rand() % (mapwidth - 2) + 1;
y = rand() % (mapheight - 2) + 1;
Page | 15
// Place new food
void update()
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
(direction)
break;
break;
break;
break;
Page | 16
}
Page | 17
// Initializes map
void initMap()
forScore();
gotoxy(rows+75,cols+16);
cout<<"Score : "<<score1;
= mapwidth / 2;
headypos = mapheight / 2;
map[x] = -1;
+ y * mapwidth] = -1;
generateFood();
Page | 18
// Prints the map to console
Page | 19
void printMap()
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);
getMapValue(int value)
{
Page | 20
srand(time(0));
Page | 21
int randFood;
switch(value)
return 'X';
case -2:
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)
Page | 22
return char(149);
Page | 23
if(randFood==8)
return char(236);
if(randFood==9)
return char(167);
if(randFood==10)
return char(234);
Page | 25
void displayA(int x, int y)
Page | 27
void displaySnake1() //To display SNAKE in screen without delay
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);
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 | 28
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;
menuScreen();
Page | 29
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);
Page | 30
fIn.read((char*)&score,sizeof(score));
fIn.close();
Page | 31
gotoxy(col+18,row+3); cout<<"Score : "<<score.score; gotoxy(col+18,row+5);
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); cout<<"Error.
_sleep(1500);
}
menuScreen();
}
Page | 32
void menuScreen() // To display menu screen
system("cls");
displaySnake1();
long size;
ans[100];
size=getScreenSize();
row=size%1000;
col=size/1000;
col=col/3+8; row=row/2;
select=checkChoice(ans,row,col);
switch(select)
case 1:
system("cls");
getch();
break;
case 2:
system("cls");
Page | 33
displayScores(); //To display scores break;
case 3:
system("cls");
case 4:
case 5:
break;
int main()
system("COLOR 70");
hideCur();
menuScreen();
Page | 34
Output Screen
Page | 35
Page | 36
Page | 37
Page | 38
Conclusion
In this way a snake game was successfully created in C++.
Bibliography
Sumita Arora – Computer text book for Class - 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 | 39