hi i am a newbie at c++.. this is code of game am wondering why the text is blinking ?

/


/* Include section */
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <time.h>

/* Using Statements*/
using std::cout;
using std::endl;

/* Constants */
#define X 20
#define Y 10
#define _D_UP		0
#define _D_DOWN		1
#define _D_RIGHT	2
#define _D_LEFT		3
	
HANDLE h;

/* Variables */
bool	_quit,
		_redraw,
		_gameOver;

short _score;						// The score of the player
short _direction, _prevDirection;	// Stores the current direction the snake is going and the previous directions
short _scPosX, _scPosY;				// The x and y position of the score thing to collect

char _map[24][80];
char _bar[11];

char _character, _scChar;

/* functions */

void initialize();			// Initializes the program
void update();				// Updates anything
void draw();				// Draws the stuff to the screen
void escape();				// Check if the escape key is pressed
void moveKey();				// Check if the up, down, left or right arrow is pressed
void updateMove();			// Updates the movement
void updateScore();			// Check if the score needs to be updated
void setGameOver();			// Sets that the player is game over
void checkGameOver();		// Check if the player is game over
void wait(int time);		// Wait for x milli seconds
void setRandomScorePos();	// Sets a new random position of the score thing to collect
void cleanup();				// Cleanup anything afterwards

/* Part structure */
struct part
{
	/* Variables */
	short posX;			// X position 
	short posY;			// Y position
	short prevX;		// Previous X position
	short prevY;		// Previous Y position
	part * child;		// Pointer to the child node

	/* Functions */
	void updateChild()	// Update the child node with this current position
	{
		if(this->child != NULL)		// Child exists so update it
		{
			this->child->prevX = this->child->posX;
			this->child->prevY = this->child->posY;
			this->child->posX = this->prevX;
			this->child->posY = this->prevY;
			this->child->updateChild();			// Update the childs child too
		}
	}

	void addChild()		// Add a new child to the stack
	{
		if(this->child == NULL)		// This does not have a child yet, so add one
		{
			this->child = new part();
		}
		else						// This has a child, so add a child one level below (continues until child == NULL)
		{
			this->child->addChild();
		}
		return;
	}

	void deleteChild()	// Delete the child
	{	
		if(this->child == NULL)
		{
			return; // Child is deleted already or does not exist
		}
		else		// Child exists > delete that child
		{
			if(this->child->hasChild())			// If the child has a child, delete that child
			{
				this->child->deleteChild();
			}
			else								// This child does not have a child, so delete it;
			{
				delete child;
				child = NULL;					// Make a NULL pointer so that hasChild() won't crash
				return;
			}
			return;
		}
		return;
	}

	void drawChild()
	{
		if(this->child == NULL)
		{
			return; // No child exists
		}
		else
		{
			// Child exists...
			// But the child has no child itself
			if(!this->child->hasChild())
			{
				_map[this->child->posY][this->child->posX] = _character;
				_map[this->child->prevY][this->child->prevX] = ' ';
			}
			// The child has a child itself
			else
			{
				_map[this->child->posY][this->child->posX] = _character;
				this->child->drawChild();	// Draw the childs children...
			}
		}
	}

	bool hasChild() // Check if part has a child
	{
		if(this->child == NULL)
			return false;
		else
			return true;
	}
} root;

/* Application entry point */
void main()
{
	// Initialize
	initialize();
	// Run loop
	while(!_quit &&  !_gameOver)
	{
		// Update and draw the screen
		update();
		if(_redraw)
		{
			draw();
			_redraw = false;
		}
		// Wait for some time
		Sleep(10);
	}
	// Show that the user is game over
	setGameOver();
	// Draw the screen for the last time
	draw();
	system("pause");	// Wait for the user to quit
	cleanup();
	//return 0;
}

void initialize()
{
	// Init
	_redraw = true;
	_quit = false;
	_gameOver = false;
	_character = 'o';
	_scChar = '$';
	// Root node
	root.posX = X;
	root.posY = Y;
	root.prevX = NULL;
	root.prevY = NULL;
	root.child = NULL;
	// Set the console title
	SetConsoleTitle(L"A Snake Clone in WIN32 Console!");
	// Fill the map
	for(int ln = 0; ln < 24; ln++)
	{
		for(int nr = 0; nr < 80; nr++)
		{
			_map[ln][nr] = ' ';
		}
	}
	_map[root.posY][root.posX] = _character;
	_direction = _D_RIGHT;
	_prevDirection = -1;
	_score = 0;
	/*for(int i = 0; i < 11; i++)
	{
		_bar[i] = ' ';
	}
	_bar[2] = '>';
	_bar[4] = 'S';
	_bar[5] = 'c';
	_bar[6] = 'o';
	_bar[7] = 'r';
	_bar[8] = 'e';
	_bar[9] = ':';*/
	h = GetStdHandle(STD_OUTPUT_HANDLE);
	// Seed random number generator
	srand(time(NULL));
	setRandomScorePos();
}

void update()
{
	// Key presses
	escape();		// Check if the user wants to quit
	moveKey();		// update movement by keypress
	updateMove();	// update the movement
	_map[root.posY][root.posX] = _character;		// Set the new character position
	// DONE: add improved child draw function
	if(root.child == NULL)
	{
		_map[root.prevY][root.prevX] = ' ';				// Set the old character position
	}
	else
	{
		root.drawChild();
	}
	// Check if the player is out of boundaries and is game over
	checkGameOver();
	updateScore();
}

void moveKey()
{
	if(GetAsyncKeyState(VK_UP) != 0)
	{
		_prevDirection = _direction;
		_direction = _D_UP;
	}
	if(GetAsyncKeyState(VK_DOWN) != 0)
	{
		_prevDirection = _direction;
		_direction = _D_DOWN;
	}
	if(GetAsyncKeyState(VK_LEFT) != 0)
	{
		_prevDirection = _direction;
		_direction = _D_LEFT;
	}
	if(GetAsyncKeyState(VK_RIGHT) != 0)
	{
		_prevDirection = _direction;
		_direction = _D_RIGHT;
	}
	// DONE: Remove this in final build (after all score related functionality is added)
	//if(GetAsyncKeyState(VK_SPACE) != 0)
	//{
	//	_score++;
	//	root.addChild();
	//}
}

void escape()
{
	if(GetAsyncKeyState(VK_ESCAPE) != 0)
	{
		_quit = true;
	}
}

void updateMove()
{
	switch(_direction)
	{
	case _D_UP:
		// Check if snake is not eating itself
		if(_prevDirection == _D_DOWN && _score > 0) 
		{
			_gameOver = true;
			break;
		}
		// Update root first
		root.prevX = root.posX;
		root.prevY = root.posY;
		root.posY--;
		// Update child
		root.updateChild();
		break;
	case _D_DOWN:
		// Check if snake is not eating itself
		if(_prevDirection == _D_UP && _score > 0) 
		{
			_gameOver = true;
			break;
		}
		root.prevX = root.posX;
		root.prevY = root.posY;
		root.posY++;
		// Update child
		root.updateChild();
		break;
	case _D_RIGHT:
		// Check if snake is not eating itself
		if(_prevDirection == _D_LEFT && _score > 0) 
		{
			_gameOver = true;
			break;
		}
		root.prevX = root.posX;
		root.prevY = root.posY;
		root.posX++;
		// Update child
		root.updateChild();
		break;
	case _D_LEFT:
		// Check if snake is not eating itself
		if(_prevDirection == _D_RIGHT && _score > 0) 
		{
			_gameOver = true;
			break;
		}
		root.prevX = root.posX;
		root.prevY = root.posY;
		root.posX--;
		// Update child
		root.updateChild();
		break;
	default:
		break;		
	}
	_redraw = true;
}

void checkGameOver()
{
	// Player is game over > _gameOver = true;
	if(root.posX < 0 || root.posX > 79 || root.posY < 1 || root.posY > 24)
	{		
		_gameOver = true;
		_redraw = true;
		return;
	}
	return;
}

void setGameOver()
{
	cout << "Game over !";
	/*_bar[1] = 'G';
	_bar[2] = 'a';
	_bar[3] = 'm';
	_bar[4] = 'e';
	_bar[5] = ' ';
	_bar[6] = 'o';
	_bar[7] = 'v';
	_bar[8] = 'e';
	_bar[9] = 'r';*/
}

void draw()
{
	// clear the screen
	system("cls");
	// Draw the first row
	//SetConsoleTextAttribute(h, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY); // Gray background - white foreground
	cout << "Score:" << _score;
	//cout << _score;
	// Fill the rest of the line
	if(_score < 10)
	{
		for(int i = 0; i < 68 ;i++)
		{
			cout << ' ';
		}
	}
	else if(_score < 100)
	{	
		for(int i = 0; i < 67;i++)
		{
			cout << ' ';
		}
	}
	else
	{
		for(int i = 0; i < 66;i++)
		{
			cout << ' ';
		}
	}
	//SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Default color Black background - grey foreground
	// Loop through _map and draw it to the screen
	for(int ln = 0; ln < 23; ln++)
	{
		for(int nr = 0; nr < 79; nr++)
		{
			cout << _map[ln][nr];
		}
		cout << endl;
	}
}

void wait(int time)
{
	for(int i = 0; i < time; i++)
	{
		// wait...
	}
}

void updateScore()
{
	if(root.posX == _scPosX && root.posY == _scPosY)
	{
		_score++;
		root.addChild();
		setRandomScorePos();
	}
}

void setRandomScorePos()
{
	short tempX, tempY;
	// TODO: add random position and check if the random position is not one of the positions of the position of the snake
	tempX = rand() % 70 + 1;
	tempY = rand() % 20 + 1;
	while(_map[tempY][tempX] == _character)
	{
		// Get a new random number
		tempX = rand() % 70 + 1;
		tempY = rand() % 20 + 1;
	}
	_scPosX = tempX;
	_scPosY = tempY;
	_map[_scPosY][_scPosX] = _scChar;
}

// Delete al temporary child interfaces
void cleanup()
{
	while(root.child != NULL)
	{
		root.deleteChild();
	}
}

The text is blinking because the text attribute is set to blink. Hove you bothered to set up the text atrribute, or are you just accepting the default value? When the text attribute is set you are setting the forgraound color and the background color. I believe it is the background color setting that can cause the text to blink.

I notice in line 368 and 393 that there is possibly a TO DO thing where you can set the forground / background text attribute. So I would suggest setting that with particular attention to the background attribute - try different settings to see how it may affect the blinking.

But in any case, the text is blinking because the text attribute is set for it to blink.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.