A Snake and fruit game in C++ using basic data
structures and algorithm. In this game initially there is a fruit at some
random location and snake head lies in the middle of the console. By pressing
left, right, top, down buttons, snake head moves accordingly. If stop button is
pressed, game is paused. If the snake
eats the fruit then its tail’s length increase, score increases by ten points
and a new fruit is generated at some random location in the console. If snake
hits the wall or bites itself then the game will terminate. This game was
developed in C/C++ using code-blocks IDE.
Code :
// header files
#include <bits\stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
bool gameOver; // to check whether game is over or not
const int width = 30; // width of the window
const int height = 20; // height of the window
int x; // current x-position of the snake
int y; // current y-position of the snake
int fruitX; // current x-position of the fruit
int fruitY; // current y-position of the fruit
int score; // total score
int tailX[100]; // x-position of tail segments
int tailY[100]; // y-position of tail segments
int nTail;// to keep track of number of tail segments
enum eDirection{
STOP=0, // snake is stopped
LEFT, // if snake wants to move left
RIGHT, // if snake wants to move right
UP, // if snake wants to move up
DOWN // if snake wants to move down
}dir; // a global variable to keep track of the snake's direction
// initial setup
void Setup(){
gameOver = false;
dir = STOP;
x = width/2;
y = height/2;
fruitX = rand()% width;
fruitY = rand()% height;
score = 0;
}
// draw function to position the walls of the window , snake postion, fruit position, tail addition
void Draw(){
system("cls");
// top wall of the window
for(int j=0; j<width; j++)
cout<<"#";
cout<<endl;
// for left and right walls of the window, snake head, fruit position, tail printing
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
if(i==y && j==x) // if its snake head position then print 0
cout<<"0";
else if(i==fruitY && j==fruitX) // if its fruit position then print F
cout<<"F";
else{
if(j==0) // left wall
cout<<"#";
else if(j==width-1) // right wall
cout<<"#";
else{ //printing tail segments
bool isTailPrinted =false;
for(int k=0;k<nTail;k++){
if(tailX[k]==j && tailY[k]==i){
cout<<"o";
isTailPrinted=true;
}
}
if(!isTailPrinted) // blank cell printed
cout<<" ";
}
}
}
cout<<endl;
}
// bottom wall of the window
for(int j=0; j<width; j++)
cout<<"#";
cout<<endl<<endl;
// Score is printed here
cout<<"Your score: "<<score<<endl;
}
// setting directions by giving inputs by pressing buttons
void Input(){
if(_kbhit()){
switch(_getch()){
case 'a':
dir=LEFT;
break;
case 'w':
dir=UP;
break;
case 'd':
dir=RIGHT;
break;
case 'z':
dir=DOWN;
break;
case 's':
dir = STOP;
break;
}
}
}
// logic of the game
void Logic(){
// moving tail segments
int prevX=tailX[0];
int prevY=tailY[0];
int prev2X, prev2Y;
tailX[0]=x;
tailY[0]=y;
for(int i=1;i<nTail;i++){
prev2X=tailX[i];
prev2Y=tailY[i];
tailX[i]=prevX;
tailY[i]=prevY;
prevX=prev2X;
prevY=prev2Y;
}
// snake moves according to the value of the dir
switch(dir){
case LEFT:
x--;
break;
case UP:
y--;
break;
case RIGHT:
x++;
break;
case DOWN:
y++;
break;
case STOP:
break;
default:
break;
}
// if it's fruit position increase the score and generate new fruit at some random location
if(x==fruitX && y==fruitY){
score+=10;
nTail++;
fruitX = rand()% width;
fruitY = rand()% height;
// customization
if(fruitX==0) fruitX+=2;
if(fruitX==width-1) fruitX-=2;
if(fruitY==0) fruitY+=2;
if(fruitY==height-1) fruitY-=2;
}
//if snake head hitting any one of the walls game is over
if(x<=0 || x>=width || y<=0 || y>=height)
gameOver = true;
// if snake bites any one of the tail segments game is over
for(int i=0;i<nTail;i++){
if(tailX[i]==x && tailY[i]==y)
gameOver=true;
}
}
// main entry point of the program
int main()
{
Setup();
while(!gameOver){
Draw();
Sleep(450); // just to slow down the speed of the snake movement
Input();
Logic();
}
cout<<"Game Over and you have scored total "<<score<<" points."<<endl;
return 0;
}
I hope you enjoy the game.
Nice content. Helpful for the beginner to start with.
ReplyDelete