Jul 4, 2013 at 11:18am UTC
//*******************************
//THIS IS THE HEADER FILE
#include <iostream>
#include <ctime>
//snake base struct
struct tagSnakeBody{
COORD snakePart;
char character;
struct tagSnakeBody* pNext;
};
class CTimer {
private:
unsigned startingTime;
unsigned endTime;
public:
void start();
unsigned elapsedTime();
};
void CTimer::start() {
startingTime = clock();
}
unsigned CTimer::elapsedTime() {
endTime = clock();
return (endTime - startingTime) / CLOCKS_PER_SEC;
}
//snake class
class CSnake
{
private:
//snake it self
struct tagSnakeBody *pHead;
public:
//constructor
CSnake(int, int);
//global vars
int lifes;
int score;
bool foodExist;
//functions
void Add2Tail();
void SetCursorAndDraw(HANDLE);
void Add2Coords(int x, int y);
void ClearAllSnake(HANDLE console);
void SetTalePositions();
void ResetSnake();
void DetectFoodcollision(COORD* food);
bool DetectAreacollision();
bool DetectBodycollision();
};
//arena class
class CArena
{
public:
void DrawArena(HANDLE console);
};
//food class
class CFood
{
public:
void CreateFood(HANDLE console, COORD* food);
void ClearFood(HANDLE console, COORD* food);
};
//contructor
CSnake::CSnake(int x, int y)
{
struct tagSnakeBody *pNew;
pHead = NULL;
//new Snake Head
pNew = (tagSnakeBody*)malloc(sizeof(struct tagSnakeBody));
pNew->pNext = NULL;
pNew->character = '@';
pNew->snakePart.X = x;
pNew->snakePart.Y = y;
pHead = pNew;
score = 0;
lifes = 3;
};
//adds a new bodypart to tail
void CSnake::Add2Tail()
{
tagSnakeBody *pNew;
tagSnakeBody *pAux;
//new Snake Body Part
//pNew = (tagSnakeBody*)malloc(sizeof(struct tagSnakeBody));
pNew = new tagSnakeBody;
pNew->pNext = NULL;
pNew->character = '*';
if(pHead == NULL)
pHead = pNew;
else
{
pAux = pHead;
while(pAux->pNext != NULL)
pAux = pAux->pNext;
pNew->snakePart.X = pAux->snakePart.X - 1;
pNew->snakePart.Y = pAux->snakePart.Y;
pAux->pNext = pNew;
}
}
//Places the cursor on console at the same coords as Snake Head is
void CSnake::SetCursorAndDraw(HANDLE console)
{
struct tagSnakeBody *pAux;
if(pHead == NULL)
return;
else
{
pAux = pHead;
while(pAux != NULL)
{
SetConsoleCursorPosition(console, pAux->snakePart);
std::cout << pAux->character;
pAux = pAux->pNext;
}
}
}
//make it move
void CSnake::Add2Coords(int x, int y)
{
pHead->snakePart.X += x;
pHead->snakePart.Y += y;
}
//clears the last place where the snake has been at
void CSnake::ClearAllSnake(HANDLE console)
{
struct tagSnakeBody *pAux;
if(pHead == NULL)
return;
else
{
pAux = pHead;
while(pAux != NULL)
{
SetConsoleCursorPosition(console, pAux->snakePart);
std::cout << " ";
pAux = pAux->pNext;
}
}
}
//makes tale to move with the head
void CSnake::SetTalePositions()
{
struct tagSnakeBody *pAux;
struct tagSnakeBody *beforeAux;
if(pHead == NULL)
return;
else
{
pAux = pHead;
while(pAux->pNext != NULL)
pAux = pAux->pNext;
while(pAux != pHead)
{
beforeAux = pHead;
while(beforeAux->pNext != pAux)
beforeAux = beforeAux->pNext;
pAux->snakePart = beforeAux->snakePart;
pAux = beforeAux;
}
}
}
//creates a piece of food at the console
void CFood::CreateFood(HANDLE console, COORD* food)
{
(*food).X = rand()%77+1;
(*food).Y = rand()%19+1;
SetConsoleCursorPosition(console, *food);
std::cout << "*";
}
//clears the food if not eaten
void CFood::ClearFood(HANDLE console, COORD* food)
{
SetConsoleCursorPosition(console, *food);
std::cout << " ";
}
//food collision
void CSnake::DetectFoodcollision(COORD* food)
{
if(pHead->snakePart.X == (*food).X && pHead->snakePart.Y == (*food).Y)
{
foodExist = false;
score+=10;
Add2Tail();
}
}
//draw snake arena
void CArena::DrawArena(HANDLE console)
{
COORD auxCoord;
for(int i = 0; i < 80; i++)
{
for(int j = 0; j < 22; j++)
{
auxCoord.X = i;
auxCoord.Y = j;
if(j == 0 || j == 21 || i == 0 || i == 79)
{
SetConsoleCursorPosition(console, auxCoord);
std::cout << "#";
}
}
}
}
//detect area collision
bool CSnake::DetectAreacollision()
{
if(pHead->snakePart.X == 0 || pHead->snakePart.X == 79
|| pHead->snakePart.Y == 0 || pHead->snakePart.Y == 21)
{
lifes--;
return true;
}
return false;
}
//reset snake
void CSnake::ResetSnake()
{
struct tagSnakeBody *pNew;
pHead = NULL;
//new Snake Head
pNew = (tagSnakeBody*)malloc(sizeof(struct tagSnakeBody));
pNew->pNext = NULL;
pNew->character = '@';
pNew->snakePart.X = 10;
pNew->snakePart.Y = 10;
pHead = pNew;
for(int i = 0; i < 5; i++) //starts with Head + 5 on tail
Add2Tail();
}
//detect collision with own body
bool CSnake::DetectBodycollision()
{
struct tagSnakeBody *pAux;
if(pHead == NULL)
return false;
else
{
pAux = pHead->pNext;
while(pAux != NULL)
{
if(pHead->snakePart.X == pAux->snakePart.X && pHead->snakePart.Y == pAux->snakePart.Y)
{
lifes--;
return true;
}
pAux = pAux->pNext;
}A
}
return false;
}
Jul 4, 2013 at 11:18am UTC
// this is the main.cpp file
//**************************************
#include <iostream>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <string>
#include "class.h"
using namespace std;
void intro();
void logo();
string getFileContents(ifstream&);
void game();
int main()
{
system ("color 0a");
char menu;
ifstream Reader ("Logo.txt");
string logo = getFileContents (Reader);
cout << logo;
cout<<"\t\t\t\t[1] Start game"<<endl;
cout<<"\n\t\t\t\t[2] Exit"<<endl;
cout<<"\n\t\t\t\t";
cin >> menu;
system("cls");
switch(menu)
{
case '1':
game();
break;
case '2':
cout << "\n\n\n\n\n\t\t\tThank you for playing!\n\n";
Sleep (2000);
break;
default:
break;
}
return 0;
}
void intro()
{
cout<<"\n\n\n\t\t\t\t loading...\n\n\n\n\n\n\n\t\t";
for (int i=0; i<45; i++)
{
system("color 0a");
cout<<char(219);
Sleep(5);
}
system("cls");
}
string getFileContents(ifstream& File)
{
string Lines = "";
if (File) //Check if everything is good
{
while (File.good ())
{
string TempLine; //Temp line
getline (File , TempLine); //Get temp line
TempLine += "\n"; //Add newline character
Lines += TempLine; //Add newline
}
return Lines;
}
else //Return error
{
return "ERROR File does not exist.";
}
}
void game()
{
{
intro();
bool started = false;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD lastCoord = {0,0};
COORD scoreCoord = {3, 23};
CTimer timer;
CSnake snake(10,10);
CArena arena;
CFood food;
COORD* pagkain = new COORD;
unsigned foodInterval = 5;
for(int i = 0; i < 5; i++) //starts with Head + 5 on tail
snake.Add2Tail();
arena.DrawArena(console);
snake.SetCursorAndDraw(console);
timer.start();
while(GetAsyncKeyState(VK_ESCAPE) == 0)
{
if(timer.elapsedTime() >= foodInterval)
{
if(snake.foodExist)
food.ClearFood(console, pagkain);
food.CreateFood(console, pagkain);
snake.foodExist = true;
timer.start();
}
else
{
snake.ClearAllSnake(console);
if(GetAsyncKeyState(VK_LEFT) != 0)
{
snake.SetTalePositions();
snake.Add2Coords(-1, 0);
lastCoord.X = -1; lastCoord.Y = 0; started = true;
}
else if(GetAsyncKeyState(VK_RIGHT) != 0)
{
snake.SetTalePositions();
snake.Add2Coords(1, 0);
lastCoord.X = 1; lastCoord.Y = 0; started = true;
}
else if(GetAsyncKeyState(VK_UP) != 0 )
{
snake.SetTalePositions();
snake.Add2Coords(0, -1);
lastCoord.X = 0; lastCoord.Y = -1; started = true;
}
else if(GetAsyncKeyState(VK_DOWN) != 0)
{
snake.SetTalePositions();
snake.Add2Coords(0, 1);
lastCoord.X = 0; lastCoord.Y = 1; started = true;
}
else
{
if(started)
snake.SetTalePositions();
snake.Add2Coords(lastCoord.X, lastCoord.Y);
}
if(snake.DetectAreacollision() || snake.DetectBodycollision())
{
lastCoord.X = 0;
lastCoord.Y = 0;
snake.ClearAllSnake(console);
started = false;
snake.ResetSnake();
arena.DrawArena(console);
}
else
{
snake.SetCursorAndDraw(console);
snake.DetectFoodcollision(pagkain);
}
SetConsoleCursorPosition(console, scoreCoord);
cout << "Score: " << snake.score << " || Lives: " << snake.lifes << " " << timer.elapsedTime();
if(snake.lifes <= 0)
break;
Sleep(50);
}
}
system("cls");
cout << "\n\n\n\n\n\t\t\tYour score is: " << snake.score << endl;
cout << "\n\n\n\n\n\t\t\tGame Over! ";
Sleep(2000);
}
}
Last edited on Jul 4, 2013 at 11:19am UTC
Jul 4, 2013 at 3:19pm UTC
Maybe I'm missing something, but I see no global variables in you program.
What variables do you think are global?
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
edit:
What you have marked as globals variables are not global.
1 2 3 4
//global vars
int lifes;
int score;
bool foodExist;
Those are public variables within the Csnake class. If you don't want those as public,
simply move them to the
private
section.
Last edited on Jul 4, 2013 at 3:27pm UTC
Jul 5, 2013 at 1:10am UTC
Yea, I'd be cool if you used code tags.
It's even worse when the answer doesn't contain code tags.
I can't understand the code, so I can't help you. Once you use code tags, then I could maybe be able to read it if you use proper formatting.