Code Snake

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

#include<bits/stdc++.

h>

#include <stdlib.h>

#include <windows.h>

#include <conio.h>

#include <ctime>

using namespace std;

void ShowCur(bool CursorVisibility)

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

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);

void gotoxy(int x,int y)

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

COORD a = {x, y};

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));

bait.second = rand() % 19 +1;

void printBait()

gotoxy(bait.first,bait.second);

cout<<"*";

};

class Player

public:

string name;

int score;

Snake x;

public:

Player(){

score=0;

void display(){

cout<<name<<" has a "<<score<<" score";

};

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.snakeHead.first==0 || x.snakeHead.first==100 || x.snakeHead.second==0 ||


x.snakeHead.second==20) return true;

if(!x.body.empty()){

for(int i=1;i<x.body.size();i++){

if(x.body[i]==x.snakeHead) return true;

return false;

bool isEatBait()

if(x.snakeHead.first==y.bait.first && x.snakeHead.second==y.bait.second) return true;

else return false;

void setDirectional()
{

if(kbhit()){

char key = getch();

if(key=='s' && diretion!='8') diretion='2';

else if(key=='w' && diretion!='2') diretion='8';

else if(key=='a' && diretion!='6') diretion='4';

else if(key=='d' && diretion!='4') diretion='6';

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,5);cout<<" ***** ** * * * * ******"<<endl;

gotoxy(30,6);cout<<" ** * * * * * * * **"<<endl;

gotoxy(30,7);cout<<" ** * * * * * ** ******"<<endl;

gotoxy(30,8);cout<<" ** * ** ******* * * **"<<endl;

gotoxy(30,9);cout<<" ***** * * * * * * ******"<<endl;

void printMenuChoose()

gotoxy(40,10);cout<<"Play Game (press 1)";

gotoxy(40,11);cout<<"High Score (press 2)";

gotoxy(40,12);cout<<"Exit Game (press 3)";

void printMenuChoose1()

system("cls");
printBox();

printTitle();

gotoxy(40,10);cout<<"1 player (press 1)";

gotoxy(40,11);cout<<"2 player (press 2)";

void printMenuChoose1_1(Player t)

system("cls");

printBox();

printTitle();

gotoxy(40,10);cout<<"Enter your name:";getline(cin,t.name);

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(40,10);cout<<"THANK YOU FOR PLAYING";

gotoxy(0,21);

};

int main()

{
SetConsoleTitle("Snake");

ShowCur(false);

GameMenu game;

printBox();

game.printTitle();

game.printMenuChoose();

while(1){

if(kbhit()){

char key = getch();

switch (key)

case '1':

game.printMenuChoose1();

while(1){

if(kbhit()){

char key = getch();

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();

#pragma omp parallel sections

#pragma omp section

snake1.Play();

#pragma omp section

snake2.Play();

system("pause");

return 0;

break;

case '3':

game.printExit();

return 0;

}
}

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy