Code Snake
Code Snake
Code Snake
h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <ctime>
CONSOLE_CURSOR_INFO ConCurInf;
ConCurInf.dwSize = 10;
ConCurInf.bVisible = CursorVisibility;
SetConsoleCursorInfo(handle, &ConCurInf);
void TextColor(int x)
HANDLE h= GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, x);
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, a);
void printBox()
{
for(int i=0;i<=100;i++){
cout<<"+";
cout<<endl;
for(int i=1;i<20;i++){
gotoxy(0,i);cout<<"+";
gotoxy(100,i);cout<<"+";
gotoxy(0,20);
for(int i=0;i<=100;i++){
cout<<"+";
class Snake
public:
pair<int,int> snakeHead;
vector<pair<int,int>> body;
public:
Snake()
snakeHead={50,10};
body={{50,10}};
void setSnake(){
snakeHead={30,10};
body={{30,10}};
void printSnake()
{
gotoxy(body[0].first,body[0].second);cout<<" ";
gotoxy(snakeHead.first,snakeHead.second);cout<<"O";
if(!body.empty()){
for(int i=1;i<body.size();i++){
gotoxy(body[i].first,body[i].second);
cout<<"o";
void setBodySnake(bool z)
body.push_back(make_pair(snakeHead.first,snakeHead.second));
if(!z){
body.erase(body.begin());
};
class Bait
public:
pair<int,int> bait;
public:
Bait()
bait={51,10};
void setBaitPosition()
{
bait.first = rand() % 99 +1;
srand(time(NULL));
void printBait()
gotoxy(bait.first,bait.second);
cout<<"*";
};
class Player
public:
string name;
int score;
Snake x;
public:
Player(){
score=0;
void display(){
};
class GameOperating
public:
Player t;
Snake x=t.x;;
Bait y;
char diretion;
bool check;
public:
GameOperating()
diretion='6';
check=false;
void setsnake(){
x.setSnake();
bool isGameOver()
if(!x.body.empty()){
for(int i=1;i<x.body.size();i++){
return false;
bool isEatBait()
void setDirectional()
{
if(kbhit()){
if(diretion=='6')x.snakeHead.first++;
else if(diretion=='8')x.snakeHead.second--;
else if(diretion=='4')x.snakeHead.first--;
else if(diretion=='2')x.snakeHead.second++;
gotoxy(x.snakeHead.first,x.snakeHead.second);
void Play()
system("cls");
printBox();
while(!isGameOver())
x.setBodySnake(check);
setDirectional();
x.printSnake();
y.printBait();
if(isEatBait()){
y.setBaitPosition();
check=true;
t.score++;
}else check=false;
gotoxy(0,21);cout<<"YOUR SCORE: "<<t.score;
Sleep(200);
gotoxy(50,10);cout<<"GameOver";
gotoxy(45,11);t.display();
};
class GameMenu
public:
void printTitle()
gotoxy(30,6);cout<<" ** * * * * * * * **"<<endl;
gotoxy(30,7);cout<<" ** * * * * * ** ******"<<endl;
void printMenuChoose()
void printMenuChoose1()
system("cls");
printBox();
printTitle();
void printMenuChoose1_1(Player t)
system("cls");
printBox();
printTitle();
gotoxy(40,11);cout<<" Level(1-10):";//cin>>level;
system("cls");
printBox();
gotoxy(45,10);cout<<"Start Game";
Sleep(3000);
void printExit()
system("cls");
printBox();
gotoxy(0,21);
};
int main()
{
SetConsoleTitle("Snake");
ShowCur(false);
GameMenu game;
printBox();
game.printTitle();
game.printMenuChoose();
while(1){
if(kbhit()){
switch (key)
case '1':
game.printMenuChoose1();
while(1){
if(kbhit()){
switch (key)
case '1':
GameOperating snake1;
game.printMenuChoose1_1(snake1.t);
snake1.Play();
system("pause");
return 0;
}
case '2':
//game.printMenuChoose1_1(t);
GameOperating snake1;
GameOperating snake2;
snake2.setsnake();
snake1.Play();
snake2.Play();
system("pause");
return 0;
break;
case '3':
game.printExit();
return 0;
}
}
return 0;