Results 1 to 10 of 10
  1. #1
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy

    [Play me] Snake on Console [C++]

    Okay, last afternoon i was bored at work (waiting..) and decided to make a cookie cutter snake on a console project "Easy" stuff.
    Plus i used Linked Lists as a good example on how to actually use them. (Tutorial about Linked Lists on my signature)

    Basic stuff:
    -You score +1 when you eat;
    -You loss one life when you hit the wall, when you hit yourself, or when you try to walk backwards;
    -Your size increases when you eat. (increases by 1);
    -If you don't eat the food for 5 secs, it will re-spawn somewhere else on console. (for 5 secs);

    How to play:
    -Avoid collisions;
    -Eat food;
    -Score;
    -Use ESCAPE to end the game;
    -Use Arrows to play.

    Screens:

    Game Start:



    Random Screens:




    Game Over:




    Source Code:

    Snake.h
    Code:
    //
    //  snake.h
    //  UniqueSnake
    //
    //  Created by Bruno Monteiro on 2010. (Aka Brinuz @ mpgh.net)
    //
    
    #include <iostream>
    #include <windows.h>
    #include <time.h>
    
    //console size: 25x80
    
    //snake base struct
    struct tagSnakeBody{
    	COORD snakePart;
    	char character;
    	struct tagSnakeBody* pNext;
    };
    
    class CTimer {
    	private:
    		unsigned startingTime;
    	public:
    		void start();
    		unsigned elapsedTime();
    };
    
    void CTimer::start() {
    	startingTime = clock();
    }
    
    unsigned CTimer::elapsedTime() {
    	return ((unsigned) clock() - 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;
    		COORD food;
    		bool foodExist;
    
    		//functions
    		void add2Tail();	
    		void setCursorAndDraw(HANDLE);
    		void add2Coords(int x, int y);
    		void clearAllSnake(HANDLE console);
    		void setTalePositions();
    		void createFood(HANDLE console);
    		void clearFood(HANDLE console);
    		void detectFoodcollision();
    		void drawArena(HANDLE console);
    		bool detectAreacollision();
    		void resetSnake();
    		bool detectBodycollision();
    };
    
    //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() {
    	struct tagSnakeBody *pNew;
    	struct tagSnakeBody *pAux;
    
    	//new Snake Body Part
    	pNew = (tagSnakeBody*)malloc(sizeof(struct 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 CSnake::createFood(HANDLE console) {
    	food.X = rand()%77+1;
    	food.Y = rand()%19+1;
    
    	SetConsoleCursorPosition(console, food);
    	std::cout << "&";
    }
    
    //clears the food if not eaten
    
    void CSnake::clearFood(HANDLE console) {
    	SetConsoleCursorPosition(console, food);
    	std::cout << " ";
    }
    
    //food collision
    
    void CSnake::detectFoodcollision() {
    	if(pHead->snakePart.X == food.X && pHead->snakePart.Y == food.Y)
    	{
    		foodExist = false;
    		score++;
    		add2Tail();
    	}
    }
    
    //draw snake arena
    
    void CSnake::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;
    		}
    	}
    
    	return false;
    }
    mainLogic.cpp

    Code:
    //
    //  mainLogic.cpp
    //  UniqueSnake
    //
    //  Created by Bruno Monteiro on 2010. (Aka Brinuz @ mpgh.net)
    //
    
    #include <iostream>
    #include <windows.h>
    #include "snake.h"
    
    using namespace std;
    
    int main()
    {
    	bool started = false;
    	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    	COORD lastCoord = {0,0};
    	COORD scoreCoord = {3, 23};
    	CTimer timer;
    	CSnake snake(10,10);
    	unsigned foodInterval = 5;
    
    	for(int i = 0; i < 5; i++) //starts with Head + 5 on tail
    		snake.add2Tail();
    
    	snake.drawArena(console);
    	snake.setCursorAndDraw(console);
    
    	timer.start(); 
    
    	while(GetAsyncKeyState(VK_ESCAPE) == 0)
    	{
    		if(timer.elapsedTime() >= foodInterval)
    		{
    			if(snake.foodExist)
    				snake.clearFood(console);
    
    			snake.createFood(console);
    			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();
    				snake.drawArena(console);
    			}
    			else
    			{
    				snake.setCursorAndDraw(console);
    				snake.detectFoodcollision();
    			}
    
    			SetConsoleCursorPosition(console, scoreCoord);
    			cout << "Score: " << snake.score << " || Lives: " << snake.lifes << "    ";
    
    			if(snake.lifes <= 0)
    				break;
    
    			Sleep(80);
    		}
    	}
    
    	cout << "Game Over :)  ";
    
    	return 0;
    }

    Attachment Contains:
    mainLogic.cpp
    snake.h
    uniquesnake.exe

    VirusTotal Scan:
    VirusTotal - Free Online Virus and Malware Scan - Result
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  2. The Following 18 Users Say Thank You to 'Bruno For This Useful Post:

    258456 (06-23-2010),blekless (12-17-2012),Eugenald (01-21-2014),falzarex (06-22-2010),fishtic (11-23-2012),geekstay (02-06-2013),Hell_Demon (06-22-2010),inmate (07-16-2010),kebdah (12-21-2013),Lilith777 (05-07-2013),Lolland (06-24-2010),matypatty (06-25-2010),Melodia (06-24-2010),nathanie0221 (02-25-2013),Sixx93 (07-25-2013),tambre (06-24-2010),Void (06-23-2010),XANIZA (12-16-2013)

  3. #2
    falzarex's Avatar
    Join Date
    Apr 2008
    Gender
    male
    Location
    here
    Posts
    417
    Reputation
    14
    Thanks
    145
    Ingenious^^ tho I'm sure it's been done before
    Quote Originally Posted by falzarex aka myself
    GTFO FUCKER U DONT BELONG IN THE INTERNETZ WORLD COZ ITS MINE


    This is an epic fail resume
    Hello VBfags.
    A 'member' of the almighty C++ section will soon join you, he is 13 year old, has the IQ and typing skills of a VBfag, so I thought he would fit in here nicely.

    A few reasons why he should be in this section instead of the C++ section:
    1) He has the IQ of a VBfag.
    2) He has no sense of grammer/spelling at all.
    3) He thinks he is pro(like most of the people in here)
    4) He thinks copy pasting is fun(exactly what you guys do)
    5) He loves it up the ass(he will keep you VBfags nice and warm)

  4. The Following User Says Thank You to falzarex For This Useful Post:

    'Bruno (06-23-2010)

  5. #3
    schim's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    My chair
    Posts
    367
    Reputation
    10
    Thanks
    114
    My Mood
    Twisted
    WOW, this is realy cool :O
    Think I'm going to do something like this 2

  6. #4
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    +rep for clean code =)
    Ah we-a blaze the fyah, make it bun dem!

  7. The Following User Says Thank You to Hell_Demon For This Useful Post:

    'Bruno (06-23-2010)

  8. #5
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Thanks everyone, actually the logic i used on that game i was using it in here (xna):



    But i lost the project, and didn't felt like restarting it again. So made it in c++ @ console ;o
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  9. #6
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    Daaaum great job Brinuz. +Rep.

  10. The Following User Says Thank You to Void For This Useful Post:

    'Bruno (06-23-2010)

  11. #7
    thekm1994's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    79
    Reputation
    8
    Thanks
    1
    My Mood
    Daring
    Good Job !
    If you wanna see how to make a someone dumb press here

  12. #8
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Update

    Added:
    -Highscores (uses a file to save them)
    -Starting Menu
    - Play
    - Highscore (check the file that has them)
    - Options (change snake speed)
    - Exit
    Every game you finish, the score will be saved and compared to others and added to the list (this if there's a lower score then that one (top 10)).
    Speed snake is changed by changing the Sleep(xxx) speed.

    Code:
    //
    //  mainLogic.cpp
    //  UniqueSnake
    //
    //  Created by Bruno Monteiro on 2010. (Aka Brinuz @ mpgh.net)
    //
    
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    #include "snake.h"
    
    using namespace std;
    
    int menu();
    void play(CSnake *snake);
    void highscore(CSnake *snake);
    void options(CSnake *snake);
    void sortFileScore(CSnake *snake);
    
    int main()
    {
    	CSnake snake(10,10);
    	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	do
    	{
    		switch(menu())
    		{
    		case 1:
    			snake.clearAllSnake(console);
    			snake.resetSnake();
    			snake.drawArena(console);
    			play(&snake);
    			sortFileScore(&snake);
    			break;
    		case 2:
    			highscore(&snake);
    			break;
    		case 3:
    			options(&snake);
    			break;
    		case 0:
    			return 0;
    			break;
    		}
    		system("cls");
    	}
    	while(true);
    
    	return 0;
    }
    
    int menu()
    {
    	cout << "~v~ Menu ~v~" << endl;
    	cout << "1- Play" << endl;
    	cout << "2- Highscores" << endl;
    	cout << "3- Options" << endl;
    	cout << "0- Exit" << endl;
    
    	int op = 0;
    	do
    	{
    		cout << "Choose a valid option:" << endl; cin >> op;
    	}
    	while(op < 0 || op > 3);
    
    	system("cls");
    	return op;
    }
    
    void options(CSnake *snake)
    {
    
    	cout << "~v~ Speed ~v~" << endl;
    	cout << "1- Fast" << endl;
    	cout << "2- Normal" << endl;
    	cout << "3- Slow" << endl;
    	cout << "Your current is: " << snake->gameSpeed << endl;
    
    	int op = 0;
    	do
    	{
    		cout << "Choose a valid option:" << endl; cin >> op;
    	}
    	while(op < 1 || op > 3);
    
    	switch(op)
    	{
    	case 1: snake->gameSpeed = 30;
    			break;
    	case 2: snake->gameSpeed = 50;
    			break;
    	case 3: snake->gameSpeed = 70;
    			break;
    	}
    
    	system("cls");
    }
    
    void highscore(CSnake *snake)
    {
    	ifstream fScore;
    	fScore.open("score.snk");
    	int i = 0; int aux;
    
    	cout << "Top Ten Scores:" << endl;
    
    	while (fScore >> aux) //top 10
    	{
    		cout << aux << endl;
    		i++;
    	}
    	fScore.close();
    
    	system("pause");
    	system("cls");
    }
    
    void play(CSnake *snake)
    {
    	bool started = false;
    	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    	COORD lastCoord = {0,0};
    	COORD scoreCoord = {3, 23};
    	CTimer timer;
    	unsigned foodInterval = 5;
    
    	//for(int i = 0; i < 5; i++) //starts with Head + 5 on tail
    	//	snake->add2Tail();
    
    	snake->setCursorAndDraw(console);
    
    	timer.start(); 
    
    	while(GetAsyncKeyState(VK_ESCAPE) == 0)
    	{
    		if(timer.elapsedTime() >= foodInterval)
    		{
    			if(snake->foodExist)
    				snake->clearFood(console);
    
    			snake->createFood(console);
    			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();
    				snake->drawArena(console);
    			}
    			else
    			{
    				snake->setCursorAndDraw(console);
    				snake->detectFoodcollision();
    			}
    
    			SetConsoleCursorPosition(console, scoreCoord);
    			cout << "Score: " << snake->score << " || Lives: " << snake->lifes << "    ";
    
    			if(snake->lifes <= 0)
    				break;
    
    			
    			Sleep(snake->gameSpeed);
    		}
    	}
    
    	cout << "Game Over :)  ";
    
    	system("pause");
    	system("cls");
    }
    
    void sortFileScore(CSnake *snake)
    {
    	int scores[11]; int aux;
    	int i = 0;
    
    	ifstream fScoreRead;
    	fScoreRead.open("score.snk");
    
    	while(fScoreRead >> aux)
    	{
    		scores[i] = aux;
    		i++;
    	}
    
    	scores[i] = snake->score;
    
    	int auxSort;
    	for(int i = 0; i < 11; i++)
    	{
    		for(int j = 0; j < 11; j++)
    		{
    
    			if(scores[i] > scores[j])
    			{
    				auxSort = scores[i];
    				scores[i] = scores[j];
    				scores[j] = auxSort;
    			}
    		}
    	}
    
    	ofstream fScoreWrite;
    	fScoreWrite.open("score.snk");
    
    	for(int i = 0; i < 10; i++)
    	{
    		fScoreWrite << scores[i] << "\n";
    	}
    }
    Code:
    //
    //  snake.h
    //  UniqueSnake
    //
    //  Created by Bruno Monteiro on 2010. (Aka Brinuz @ mpgh.net)
    //
    
    #include <iostream>
    #include <windows.h>
    #include <time.h>
    
    //console size: 25x80
    
    //snake base struct
    struct tagSnakeBody{
    	COORD snakePart;
    	char character;
    	struct tagSnakeBody* pNext;
    };
    
    class CTimer {
    	private:
    		unsigned startingTime;
    	public:
    		void start();
    		unsigned elapsedTime();
    };
    
    void CTimer::start() {
    	startingTime = clock();
    }
    
    unsigned CTimer::elapsedTime() {
    	return ((unsigned) clock() - 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;
    		COORD food;
    		bool foodExist;
    		DWORD gameSpeed;
    
    		//functions
    		void add2Tail();	
    		void setCursorAndDraw(HANDLE);
    		void add2Coords(int x, int y);
    		void clearAllSnake(HANDLE console);
    		void setTalePositions();
    		void createFood(HANDLE console);
    		void clearFood(HANDLE console);
    		void detectFoodcollision();
    		void drawArena(HANDLE console);
    		bool detectAreacollision();
    		void resetSnake();
    		bool detectBodycollision();
    };
    
    //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;
    	gameSpeed = 50;
    }
    
    //adds a new bodypart to tail
    void CSnake::add2Tail() {
    	struct tagSnakeBody *pNew;
    	struct tagSnakeBody *pAux;
    
    	//new Snake Body Part
    	pNew = (tagSnakeBody*)malloc(sizeof(struct 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 CSnake::createFood(HANDLE console) {
    	food.X = rand()%77+1;
    	food.Y = rand()%19+1;
    
    	SetConsoleCursorPosition(console, food);
    	std::cout << "&";
    }
    
    //clears the food if not eaten
    
    void CSnake::clearFood(HANDLE console) {
    	SetConsoleCursorPosition(console, food);
    	std::cout << " ";
    }
    
    //food collision
    
    void CSnake::detectFoodcollision() {
    	if(pHead->snakePart.X == food.X && pHead->snakePart.Y == food.Y)
    	{
    		foodExist = false;
    		score++;
    		add2Tail();
    	}
    }
    
    //draw snake arena
    
    void CSnake::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;
    		}
    	}
    
    	return false;
    }
    enjoy
    Last edited by 'Bruno; 08-27-2012 at 06:40 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  13. The Following 2 Users Say Thank You to 'Bruno For This Useful Post:

    Hell_Demon (06-24-2010),Melodia (06-24-2010)

  14. #9
    Melodia's Avatar
    Join Date
    Dec 2009
    Gender
    female
    Posts
    2,608
    Reputation
    276
    Thanks
    1,662
    My Mood
    Dead
    +Rep.

    Releasing this proves you learned / know something useful.

    +Clean Code,

    Have Fun Coding In Life .
    Love You All~

  15. The Following User Says Thank You to Melodia For This Useful Post:

    'Bruno (06-24-2010)

  16. #10
    Shark23's Avatar
    Join Date
    Mar 2010
    Gender
    male
    Location
    Kansas
    Posts
    424
    Reputation
    10
    Thanks
    55
    My Mood
    Cool
    Looks nice. I once tried making a snake in vb. It didn't go so well.
    Assembly Programmer

  17. The Following User Says Thank You to Shark23 For This Useful Post:

    Hell_Demon (06-24-2010)

Similar Threads

  1. Play Helbreath Int Free! Free Mol Pins!
    By Dave84311 in forum Helbreath Hacks & Cheats
    Replies: 95
    Last Post: 07-09-2011, 02:45 PM
  2. [SOLVED]I play in COD MW2 With Steam The console don't work..
    By F-I-R-E in forum Call of Duty Modern Warfare 2 Help
    Replies: 9
    Last Post: 02-05-2011, 03:53 PM
  3. How to play Snake on Youtube!
    By Stifmeister in forum General
    Replies: 4
    Last Post: 08-30-2010, 04:02 AM
  4. Play snake on youtube.
    By aosma8 in forum General
    Replies: 3
    Last Post: 07-30-2010, 11:01 PM
  5. Can't find any games/players to play MP after using console hack!
    By tom95ek in forum Call of Duty Modern Warfare 2 Help
    Replies: 9
    Last Post: 03-30-2010, 12:56 PM