#include <cstdlib>
#include <Windows.h>
#include <iostream>
#include <ctime>
#include <conio.h>
using namespace std;
void setcolor(unsigned char color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
void sp(int spalva)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), spalva);
}
void s()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
class SnakeClass
{
protected:
double Scoore;
int rad,FailRandm,x, y,a,b,i,SnakeLength = 4,PreviousDirection,DirectionBeforePreviousDirection;
char map [3600], key,c;
long double as;
bool down ,left,right,up ;
public:
SnakeClass()
{
Scoore = 0 ;
x = 1720;
y = 1400;
down = 0 ;
left = 0 ;
right = 0 ;
up = 0 ;
PreviousDirection = 0;
i = 0;
}
void clearscreen ();
void ToGetRandomApple();
void CheckRight ();
void CheckLeft ();
void ConstSnake ();
void Keyboard ();
void MoveLogic ();
void Keyboard2 ();
}jaja;
int main()
{
jaja.ConstSnake();
}
void SnakeClass ::ToGetRandomApple()
{
if(x < -0 || x > 3600 ){cout << endl << "\n\nGame Over!";cout << "You earned:" << Scoore/125;cin.ignore();}
if(map[rad] == map[x]){
Scoore += 2;
SnakeLength++;
if( left == true )
{
x--;
}
else if( down == true )
{
x+=80;
}
else if( up == true )
{
x-=80;
}
else if( right == true )
{
x++;
}
rad = rand()%36+1;
FailRandm = rand()%100+1;
rad*=FailRandm;
}}
void SnakeClass ::ConstSnake()
{
srand((unsigned)time(0));
rad = rand() % 3600+1;
system("color 19"); system("color 19");
do{
cout << "Dollers=" << Scoore/125 << " " <<endl;
cout << "LEFT=" << left << endl << "DOWN=" << down << endl << "UP=" << up << endl << "RIGHT=" << right;
cout << "PreviousDirection=" << PreviousDirection << endl<<endl;
cout << "LALALALA " << DirectionBeforePreviousDirection;
cout << endl << endl ;
jaja.ToGetRandomApple();
setcolor(15);
for(i = 0 ; i < 3600 ; i++)
{
sp(10);
if(x != i && y != i && rad != i)
{
map[i] = ' ';
cout << map[i];
}
else if(i == x)
{
map[i] = 219;
cout << map[i] ;
}
else if(i == rad)
{
map[rad] = 1;
cout << map[i];
}
else
{
map[i] = 219;
cout << map[i] ;
}
}
s();
jaja.Keyboard ();
jaja.MoveLogic();
jaja.clearscreen ();
}
while(true);
}
void SnakeClass::clearscreen()
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}
void SnakeClass::Keyboard()
{
if(kbhit()){
key = getch();
jaja.Keyboard2();
cout << y;
switch( key )
{
case 'd':
{
if (left == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
left = 0, up = 0, down = 0,
right = 1;
}
break;
}
case 'w':
{
if (down == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
left = 0, down = 0, right = 0,
up = 1;
}
break;
}
case 'a':
{
if (right == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
down = 0, up = 0, right = 0,
left = 1;
}
break;
}
case 's':
{
if (up == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
left = 0, up = 0, right = 0,
down = 1;
}
break;
}
}
}
}
void SnakeClass::MoveLogic()
{
if(left == 1 && right == 0)
{
if(PreviousDirection == 2)
{
x--;
if(x !=y-SnakeLength)
{
y+=80;
}
else y--;
}
else if(PreviousDirection == 4)
{
x--;
if(x !=y-SnakeLength)
{
y-=80;
}
else y--;
}
else if(PreviousDirection == 1)
{
x--;
y--;
}
}
else if(left == 0 && right == 1)
{
if(PreviousDirection == 2)
{
x++;
if(x!=y+SnakeLength)
{
y+=80;
}
else y++;
}
else if(PreviousDirection == 4)
{
x++;
if(x!=y+SnakeLength)
{
y-=80;
}
else y++;
}
else if(PreviousDirection == 3)
{
x++;
y++;
}
}
else if(down == 1 && right == 0)
{
if(PreviousDirection == 2)
{
x+=80;
y+=80;
}
else if(PreviousDirection == 3)
{
x+=80;
if(x!=y+SnakeLength * 80)
{
y++;
}
else y+=80;
}
else if(PreviousDirection == 1)
{
x+=80;
if(x!=y+SnakeLength * 80)
{
y--;
}
else y+=80;
}
}
else if(up == 1 && right == 0)
{
if(PreviousDirection == 3)
{
x-=80;
if(x!=y-80*SnakeLength)
{
y++;
}
else y-=80;
}
else if(PreviousDirection == 4)
{
x-=80;
y-=80;
}
else if(PreviousDirection == 1)
{
x-=80;
if(x!=y-80*SnakeLength)
{
y--;
}
else y-=80;
}
}
}
void SnakeClass::Keyboard2()
{
if( left == true )
{
PreviousDirection = 1;
}
else if( down == true )
{
PreviousDirection = 2;
}
else if( up == true )
{
PreviousDirection = 4;
}
else if( right == true )
{
PreviousDirection = 3;
}
}
#include <Windows.h>
#include <iostream>
#include <ctime>
#include <conio.h>
using namespace std;
void setcolor(unsigned char color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
void sp(int spalva)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), spalva);
}
void s()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
class SnakeClass
{
protected:
double Scoore;
int rad,FailRandm,x, y,a,b,i,SnakeLength = 4,PreviousDirection,DirectionBeforePreviousDirection;
char map [3600], key,c;
long double as;
bool down ,left,right,up ;
public:
SnakeClass()
{
Scoore = 0 ;
x = 1720;
y = 1400;
down = 0 ;
left = 0 ;
right = 0 ;
up = 0 ;
PreviousDirection = 0;
i = 0;
}
void clearscreen ();
void ToGetRandomApple();
void CheckRight ();
void CheckLeft ();
void ConstSnake ();
void Keyboard ();
void MoveLogic ();
void Keyboard2 ();
}jaja;
int main()
{
jaja.ConstSnake();
}
void SnakeClass ::ToGetRandomApple()
{
if(x < -0 || x > 3600 ){cout << endl << "\n\nGame Over!";cout << "You earned:" << Scoore/125;cin.ignore();}
if(map[rad] == map[x]){
Scoore += 2;
SnakeLength++;
if( left == true )
{
x--;
}
else if( down == true )
{
x+=80;
}
else if( up == true )
{
x-=80;
}
else if( right == true )
{
x++;
}
rad = rand()%36+1;
FailRandm = rand()%100+1;
rad*=FailRandm;
}}
void SnakeClass ::ConstSnake()
{
srand((unsigned)time(0));
rad = rand() % 3600+1;
system("color 19"); system("color 19");
do{
cout << "Dollers=" << Scoore/125 << " " <<endl;
cout << "LEFT=" << left << endl << "DOWN=" << down << endl << "UP=" << up << endl << "RIGHT=" << right;
cout << "PreviousDirection=" << PreviousDirection << endl<<endl;
cout << "LALALALA " << DirectionBeforePreviousDirection;
cout << endl << endl ;
jaja.ToGetRandomApple();
setcolor(15);
for(i = 0 ; i < 3600 ; i++)
{
sp(10);
if(x != i && y != i && rad != i)
{
map[i] = ' ';
cout << map[i];
}
else if(i == x)
{
map[i] = 219;
cout << map[i] ;
}
else if(i == rad)
{
map[rad] = 1;
cout << map[i];
}
else
{
map[i] = 219;
cout << map[i] ;
}
}
s();
jaja.Keyboard ();
jaja.MoveLogic();
jaja.clearscreen ();
}
while(true);
}
void SnakeClass::clearscreen()
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}
void SnakeClass::Keyboard()
{
if(kbhit()){
key = getch();
jaja.Keyboard2();
cout << y;
switch( key )
{
case 'd':
{
if (left == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
left = 0, up = 0, down = 0,
right = 1;
}
break;
}
case 'w':
{
if (down == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
left = 0, down = 0, right = 0,
up = 1;
}
break;
}
case 'a':
{
if (right == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
down = 0, up = 0, right = 0,
left = 1;
}
break;
}
case 's':
{
if (up == 0) {
DirectionBeforePreviousDirection = PreviousDirection;
left = 0, up = 0, right = 0,
down = 1;
}
break;
}
}
}
}
void SnakeClass::MoveLogic()
{
if(left == 1 && right == 0)
{
if(PreviousDirection == 2)
{
x--;
if(x !=y-SnakeLength)
{
y+=80;
}
else y--;
}
else if(PreviousDirection == 4)
{
x--;
if(x !=y-SnakeLength)
{
y-=80;
}
else y--;
}
else if(PreviousDirection == 1)
{
x--;
y--;
}
}
else if(left == 0 && right == 1)
{
if(PreviousDirection == 2)
{
x++;
if(x!=y+SnakeLength)
{
y+=80;
}
else y++;
}
else if(PreviousDirection == 4)
{
x++;
if(x!=y+SnakeLength)
{
y-=80;
}
else y++;
}
else if(PreviousDirection == 3)
{
x++;
y++;
}
}
else if(down == 1 && right == 0)
{
if(PreviousDirection == 2)
{
x+=80;
y+=80;
}
else if(PreviousDirection == 3)
{
x+=80;
if(x!=y+SnakeLength * 80)
{
y++;
}
else y+=80;
}
else if(PreviousDirection == 1)
{
x+=80;
if(x!=y+SnakeLength * 80)
{
y--;
}
else y+=80;
}
}
else if(up == 1 && right == 0)
{
if(PreviousDirection == 3)
{
x-=80;
if(x!=y-80*SnakeLength)
{
y++;
}
else y-=80;
}
else if(PreviousDirection == 4)
{
x-=80;
y-=80;
}
else if(PreviousDirection == 1)
{
x-=80;
if(x!=y-80*SnakeLength)
{
y--;
}
else y-=80;
}
}
}
void SnakeClass::Keyboard2()
{
if( left == true )
{
PreviousDirection = 1;
}
else if( down == true )
{
PreviousDirection = 2;
}
else if( up == true )
{
PreviousDirection = 4;
}
else if( right == true )
{
PreviousDirection = 3;
}
}