0% found this document useful (0 votes)
52 views

Code Snake

This document contains the code for a Snake game in C++. It defines classes for the Snake, Bait, Player, and GameOperating logic. The GameMenu class handles the main menu and game start/end screens. The main function initializes the menu and uses it to start a 1-player or 2-player game by calling the GameOperating class's Play method, which runs the core game loop until the snake dies or eats all the bait.

Uploaded by

Tước Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Code Snake

This document contains the code for a Snake game in C++. It defines classes for the Snake, Bait, Player, and GameOperating logic. The GameMenu class handles the main menu and game start/end screens. The main function initializes the menu and uses it to start a 1-player or 2-player game by calling the GameOperating class's Play method, which runs the core game loop until the snake dies or eats all the bait.

Uploaded by

Tước Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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