this is my entire game ^_^;;
Code:
#include <iostream.h>
#include <stdlib.h>
#include <utility.h>
#include <conio.h>
#include <lvp\vector.h>
const char KB_ARROWUP='i';
const char KB_ARROWLEFT='j';
const char KB_ARROWRIGHT='l';
const char KB_ARROWDOWN='k';
class SnakeClass
{
public:
SnakeClass();
~SnakeClass();
void Initializer();
void EraseSnake();
void DisplaySnake();
void CheckMove();
void MoveSnake();
void SnakeDirection();
void Delay();
bool CheckCrash();
vector<int> SnakeX;
vector<int> SnakeY;
private:
bool bCrash;
char cKeyPressed, cTemp;
int iXerase, iYerase;
};
//Default Constructor
SnakeClass::SnakeClass()
{
SnakeX.resize(4);
SnakeY.resize(4);
bCrash=false;
cKeyPressed=' ';
cTemp=' ';
}
//Deconstructor
SnakeClass::~SnakeClass()
{
}
//Initializes the game
void SnakeClass::Initializer()
{
//Draws the box
for(int iSides=10; iSides<=40; iSides++) // Leftwall 20 Rightwall 60
{
gotoxy(20, iSides);
cout<<"#";
gotoxy(60, iSides);
cout<<"#";
}
for(int iTopBot=21; iTopBot<=59; iTopBot++) // Topwall 10 Botwall 40
{
gotoxy(iTopBot, 10);
cout<<"#";
gotoxy(iTopBot, 40);
cout<<"#";
}
// Initialize snake values
for (int iIndex=0; iIndex<4; iIndex++)
{
SnakeX[iIndex]=iIndex+40;
SnakeY[iIndex]=25;
}
}
//Erase snake as it moves
void SnakeClass::EraseSnake()
{
gotoxy(iXerase, iYerase);
cout << " ";
iXerase=SnakeX[SnakeX.length()-1];
iYerase=SnakeY[SnakeY.length()-1];
}
//Draw a snake on the screen
void SnakeClass::DisplaySnake()
{
//Display Snake
for(int iIndex=0; iIndex<SnakeX.length(); iIndex++)
{
gotoxy(SnakeX[iIndex], SnakeY[iIndex]);
cout << "o";
}
}
void SnakeClass::CheckMove()
{
// Check for player move
if(kbhit())
{
cTemp=getch();
if(cTemp==KB_ARROWUP || cTemp==KB_ARROWLEFT || cTemp==KB_ARROWRIGHT || cTemp==KB_ARROWDOWN)
cKeyPressed=cTemp;
}
}
void SnakeClass::MoveSnake()
{
if (cKeyPressed!=' ')
{
//Move snake body pieces
for(int iIndex=SnakeX.length()-1; iIndex>0; iIndex--)
{
SnakeX[iIndex]=SnakeX[iIndex-1];
SnakeY[iIndex]=SnakeY[iIndex-1];
}
}
}
void SnakeClass::SnakeDirection()
{
//Set continuous direction for snake
if (cKeyPressed!=' ')
{
if(cKeyPressed==KB_ARROWUP)
SnakeY[0]--;
else if(cKeyPressed==KB_ARROWLEFT)
SnakeX[0]--;
else if(cKeyPressed==KB_ARROWRIGHT)
SnakeX[0]++;
else if(cKeyPressed==KB_ARROWDOWN)
SnakeY[0]++;
else;
}
}
//Checks if the snake crashed
bool SnakeClass::CheckCrash()
{
if(SnakeX[0]==20 || SnakeX[0]==60)
bCrash=true;
else if (SnakeY[0]==10 || SnakeY[0]==40)
bCrash=true;
else;
for (int iCounter=1; iCounter<SnakeX.length(); iCounter++)
{
if (SnakeX[0]==SnakeX[iCounter]&&SnakeY[0]==SnakeY[iCounter])
{
bCrash=true;
}
}
return(bCrash);
}
//Slows down the snake
void SnakeClass::Delay()
{
//Delay and erase code
Sleep(200);
}
class FruitClass : public SnakeClass
{
public:
FruitClass();
~FruitClass();
void Initializer();
void GenerateFruit();
void CheckFruit();
int iScore;
private:
int iXFruit, iYFruit;
bool bFruit;
};
//Default Constructor
FruitClass::FruitClass()
{
randomize();
iXFruit=random(39)+21;
iYFruit=random(29)+11;
iScore=0;
bFruit=true;
}
//Deconstructor
FruitClass::~FruitClass()
{
}
//Initializes the game
void FruitClass::Initializer()
{
iXFruit=random(39)+21;
iYFruit=random(29)+11;
bFruit=true;
}
//Generate Fruits
void FruitClass::GenerateFruit()
{
//if there is no fruit, generate one
if(bFruit==false)
{
iXFruit=21+(random(39));
iYFruit=11+(random(29));
bFruit=true;
}
//if there is a fruit, draw it on the screen
else if(bFruit==true)
{
gotoxy(iXFruit, iYFruit);
cout << "*";
}
}
//Check if fruit is where the snake head is
void FruitClass::CheckFruit()
{
gotoxy(1,1);
cout<<SnakeX[0]<<" "<<SnakeY[0]<<endl;
cout<<iXFruit<<" "<<iYFruit;
if(SnakeX[0]== iXFruit && SnakeY[0]==iYFruit)
{
iScore+=10;
SnakeX.resize(SnakeX.length()+1);
SnakeY.resize(SnakeY.length()+1);
bFruit=false;
}
}
int main()
{
bool bCrash;
int iScore;
bool bFruit;
SnakeClass Snake;
FruitClass Fruit;
Snake.Initializer();
Fruit.Initializer();
Snake.DisplaySnake();
// Start of game tick
do
{
Snake.CheckMove();
Fruit.GenerateFruit();
Fruit.CheckFruit();
Snake.EraseSnake();
Snake.MoveSnake();
Snake.DisplaySnake();
Snake.SnakeDirection();
bCrash=Snake.CheckCrash();
Snake.Delay();
gotoxy(20,9);
cout<<"Score: "<<Fruit.iScore;
}
while(!bCrash);
gotoxy(38, 25);
cout << "CRASH!!";
gotoxy(1, 49);
system("PAUSE");
return (0);
}