Simple Snake Game in C++

Introduction:

The Snake Game Project is a classic and timeless implementation of the popular Snake game, a simple yet engaging concept that has entertained generations of gamers. Originally created in the late 1970s, the Snake game has since become a staple in the world of computer programming and game development. This project aims to provide a modern and customizable version of the Snake game, showcasing fundamental programming principles and offering a hands-on learning experience for developers at various skill levels.

Objective:

The primary goal of the Snake Game Project is to create a functional and visually appealing rendition of the classic Snake game. The player controls a snake that moves around a two-dimensional grid, consuming food items to grow longer. The challenge lies in avoiding collisions with the snake’s own body and the boundaries of the game area. As the snake grows, the difficulty increases, creating an engaging and progressively challenging gaming experience.

Key Features:

  1. User Interface (UI): A clean and intuitive user interface will provide a seamless gaming experience. Display elements will include the current score, the length of the snake, and any additional information relevant to the gameplay.
  2. Game Mechanics: Implement the core game mechanics, such as snake movement, food generation, and collision detection. Ensure that the snake grows longer each time it consumes food and that the game ends when the snake collides with itself or the boundaries.
  3. Scoring System: Develop a scoring system that rewards players for each food item consumed. The score could be based on the number of food items eaten, the speed of the snake, or other relevant factors.
  4. Customization Options: Allow users to customize aspects of the game, such as the appearance of the snake, the grid size, and other aesthetic features. This adds a layer of personalization to the gaming experience.
  5. Difficulty Levels: Implement multiple difficulty levels to cater to players of varying skill levels. This could involve adjusting the speed of the snake, altering the rate of food appearance, or introducing additional challenges.

Technologies:

The Snake Game Project can be implemented using various programming languages and frameworks. Common choices include C++, Python with libraries like Pygame, JavaScript for web-based versions, or other languages based on the developer’s preference and platform requirements.

Software & Programming Language:

The choice of software for a Snake Game Project largely depends on the programming language and platform you intend to use. Here we are used tools and frameworks for developing a Snake Game:

SFML (Simple and Fast Multimedia Library) – C++:

  • Description:

SFML is a multimedia library for C++ that provides simple interfaces to various features needed for game development, including graphics, audio, and window management.

  • How to Get Started:

Download and install SFML, set up a C++ project, and use a C++ IDE (e.g., Visual Studio, Code::Blocks) to write your Snake game code.

Software [ Code blocks] Installation Link:

[epcl_button label=”Download Code Blocks” url=”https://www.codeblocks.org/downloads/binaries/” type=”outline” color=”red” size=”fluid” icon=”fa-arrow-circle-down” target=”_self” rel=”dofollow”][/epcl_button]

Source Code:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<string.h>
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
void textcolor(int fc,int bc=-1){
if(fc<0 || fc>15)
return;
HANDLE h;
h = GetStdHandle(STD_OUTPUT_HANDLE);
if(bc>=0 && bc<16)
SetConsoleTextAttribute(h,fc|bc*16);
else
SetConsoleTextAttribute(h,fc);
}
void textcolor(char *fc,char *bc=""){
int x,y=16;
char *colors[]={"Black","Blue","Green","Aqua","Red","Purple","Yellow","White","Gray",
"LightBlue","LightGreen","LightAqua","LightRed","LightPurple","LightYellow","BrightWhite"};
for(x=0;x<16;x++)
if(strcmpi(colors[x],fc)==0)
break;
if(strlen(bc)>0)
for(y=0;y<16;y++)
if(strcmpi(colors[y],bc)==0)
break;
textcolor(x,y);
}
void textcolor(char *fc,int bc){
int x;
char *colors[]={"Black","Blue","Green","Aqua","Red","Purple","Yellow","White","Gray",
"LightBlue","LightGreen","LightAqua","LightRed","LightPurple","LightYellow","BrightWhite"};
for(x=0;x<16;x++)
if(strcmpi(colors[x],fc)==0)
break;
textcolor(x,bc);
}
void textcolor(int fc,char *bc){
int y;
char *colors[]={"Black","Blue","Green","Aqua","Red","Purple","Yellow","White","Gray",
"LightBlue","LightGreen","LightAqua","LightRed","LightPurple","LightYellow","BrightWhite"};
if(strlen(bc)>0)
for(y=0;y<16;y++)
if(strcmpi(colors[y],bc)==0)
break;
textcolor(fc,y);
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void getup(){
HANDLE hout;
CONSOLE_CURSOR_INFO cursor;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
cursor.dwSize=1;
cursor.bVisible=false;
SetConsoleCursorInfo(hout, &cursor);
system("mode con:cols=80 lines=25");
system("title Snake Game - gpkumar.com");
system("cls");
textcolor("LightPurple");
printf("\n %c",218);
int x;
for(x=0;x<75;x++)
printf("%c",196);
printf("%c ",191);
for(x=0;x<17;x++){
gotoxy(2,x+2);
printf("%c",179);
gotoxy(78,x+2);
printf("%c ",179);
}
printf(" %c",192);
for(x=0;x<75;x++)
printf("%c",196);
printf("%c ",217);
printf(" %c",218);
for(x=0;x<21;x++)
printf("%c",196);
printf("%c\n",191);
printf(" %c S N A K E G A M E %c\n",179,179);
printf(" %c",192);
for(x=0;x<21;x++)
printf("%c",196);
printf("%c",217);

gotoxy(59,20);
printf("%c",218);
for(x=0;x<18;x++)
printf("%c",196);
printf("%c",191);
gotoxy(59,21);
printf("%c SCORE : 100 %c",179,179);
gotoxy(59,22);
printf("%c STATUS: Playing %c",179,179);
gotoxy(59,23);
printf("%c",192);
for(x=0;x<18;x++)
printf("%c",196);
printf("%c",217);
gotoxy(28,20);
printf("Press 'x' to Exit");
gotoxy(28,21);
printf("Press Space to Pause and Play");
gotoxy(10,23);
textcolor("white","blue");
printf(" gpkumar.com ");
textcolor(7);
}
void score(int sc){
gotoxy(69,21);
printf("%6d",sc*10);
}
void status(char *s,int c=7){
gotoxy(69,22);
textcolor(c);
int x;
for(x=0;x<strlen(s);x++)
printf("%c",s[x]);
for(;x<8;x++)
printf(" ");
textcolor(7);
}
int main(){
getup();
register int flow,size,i,xb,yb;
int speed,restart=1,tmp,xpos[100],ypos[100],scr;
srand(time(NULL));
while(true){
if(restart){
status("Playing",10);
for(int k=1;k<75;k+=2)
for(int j=0;j<17;j++){
gotoxy(k+3,j+2);
printf(" ");
}
size=5;
speed=200;
scr=0;
score(scr);
flow=RIGHT;
xpos[0]=20;
for(i=0;i<size;i++){
xpos[i]=xpos[0]-i*2;
ypos[i]=10;
}
for(i=0;i<size;i++){
gotoxy(xpos[i],ypos[i]);
printf("o");
}
for(tmp=1;true;){
do{
xb=rand()%75+3;
}while(xb%2!=0);
yb=rand()%17+2;
for(i=0;i<size;i++)
if(xb==xpos[i] && yb==ypos[i]){
tmp=0; break;
}
if(tmp)
break;
}
gotoxy(xb,yb);
textcolor("lightgreen");
printf("@");
textcolor(7);
restart=0;
}
while(!kbhit() && !restart) {
if(xpos[0]==xb && ypos[0]==yb){
for(tmp=1;true;){
do{
xb=rand()%75+3;
}while(xb%2!=0);
yb=rand()%17+2;
for(i=0;i<size;i++)
if(xb==xpos[i] && yb==ypos[i]){
tmp=0; break;
}
if(tmp)
break;
}
gotoxy(xb,yb);
textcolor("lightgreen");
printf("@");
textcolor(7);
size++;
scr++;
speed-=3;
score(scr);
}
gotoxy(xpos[size-1],ypos[size-1]);
for(i=size-1;i>0;i--){
xpos[i]=xpos[i-1];
ypos[i]=ypos[i-1];
}
switch(flow){
case RIGHT :xpos[i]+=2; break;
case LEFT : xpos[i]-=2; break;
case UP : ypos[i]-=1; break;
case DOWN : ypos[i]+=1;
}
tmp=1;
for(i=1;i<size;i++)
if(xpos[i]==xpos[0] && ypos[i]==ypos[0]){
tmp=0;
break;
}
if(xpos[0]>76 || xpos[0]<4 || ypos[0]<2 ||ypos[0]>18)
tmp=0;
if(tmp){
printf(" ");
gotoxy(xpos[0],ypos[0]);
printf("O");
gotoxy(xpos[1],ypos[1]);
printf("o");
}
else{
textcolor("LIGHTRED");
printf("o");
gotoxy(xpos[1],ypos[1]);
printf("O");
for(i=2;i<size;i++){
gotoxy(xpos[i],ypos[i]);
printf("o");
}
textcolor(7);
status("GameOver",12);
restart=1;
getch();
}
//delay(speed);
Sleep(speed);
}
char ch=getch();
switch(tolower(ch)){
case 'x' : system("cls");
return 0;
case ' ' : status("Paused");
while(true){
char z=getch();
if(z=='x')
return 0;
if(z==' ')
break;
}
status("Playing",10);
break;
case -32: {
char chh=getch();
if(chh==72 && flow!=DOWN)
flow=UP;
else if(chh==80 && flow!=UP)
flow=DOWN;
else if(chh==75 && flow!=RIGHT)
flow=LEFT;
else if(chh==77 && flow!=LEFT)
flow=RIGHT;
break;
}
}
}
}

Output:

Conclusion:

The Snake Game Project offers an exciting opportunity into game development, honing programming skills while recreating a beloved classic. Whether you are a beginner seeking a hands-on learning experience or an experienced developer looking for a fun project, the Snake Game Project provides a platform to explore game mechanics, user interfaces, and customization options in a practical and enjoyable way.