Archived

This topic is now archived and is closed to further replies.

com

a few errors as usual, help is appreieated!

Recommended Posts

I went thro a tutorial today where it had code oready done to generate a random maze and player that you can control using a dos window. I thought i would have a little go at making the AI and colision detecion for this which i have finished but i have to errors that i am having trouble getting out: game.cpp C:\Windows\Desktop\rpg\edit\game.cpp(213) : error C2601: ''MoveAI1'' : local function definitions are illegal C:\Windows\Desktop\rpg\edit\game.cpp(287) : fatal error C1075: end of file found before the left brace ''{'' at ''C:\Windows\Desktop\rpg\edit\game.cpp(248)'' was matched Error executing cl.exe.
  


#include <windows.h>									// We need to include this for "console" functions.


#define SCREEN_WIDTH  30								// This is the width of our window 
#define SCREEN_HEIGHT 25								// This is the height of our window
#define START_X        0								// This is the starting X position for our character
#define START_Y        7								// This is the starting Y position for our character


#define PLAYER 1										// This defines our player character (smily face)
#define WALL ''#''
#define AI1 2                                             // This defines our wall character for collision
#define AI2 3	
// This is our prototype for our drawing maze function (We have this so we can define it below main())

void DrawMaze(CHAR_INFO screenBuffer[], int playerX, int playerY, int ai11X, int ai11Y, int ai22X, int ai12Y);

// This is the prototype for the function that moves our character around the screen

bool MovePlayer(CHAR_INFO screenBuffer[], COORD &playerPos);
bool MoveAI1(CHAR_INFO screenBuffer[], COORD &ai11Pos);
bool MoveAI2(CHAR_INFO screenBuffer[], COORD &ai22Pos);


void main()												// Here is our main().

{														// This is our buffer that will hold all the maze data we wish to display to the screen.

	CHAR_INFO screenBuffer[SCREEN_WIDTH * SCREEN_HEIGHT] = {0};	
	COORD bufferSize = {SCREEN_WIDTH , SCREEN_HEIGHT};	// This is a COORD that holds the width and height of our window that we will display

	COORD playerPos = {START_X, START_Y};				// This will hold our players position

    COORD ai11Pos = {START_X, START_Y};
	COORD ai22Pos = {START_X, START_Y};

	srand( GetTickCount() );							// This seeds our random numbers with time so it isn''t always the same maze


	// Below we create our maze with a bunch of random # signs.

	// This just fills in our screen buffer with the data, but DOESN''T draw it yet.

	// It is faster to draw it all at once, rather than using WriteConsoleOutputCharacter().


	for(int y = 1; y < bufferSize.Y; y++)				// This fills in the columns (y value) of our buffer

	{
		// You will notice that we start a x = 2.  This is so we have some room to freely walk around in front of the maze.


		for(int x = 2; x < bufferSize.X; x++)			// This fills in the rows (x value) of our buffer

		{												// Below, we fill in the current index with a wall and color.

			if(!(rand() % 3))							// If our random number is 2, then draw a wall in our maze.  This mixes up the maze.

			{
				screenBuffer[x + y * SCREEN_WIDTH].Char.AsciiChar = WALL;
				screenBuffer[x + y * SCREEN_WIDTH].Attributes = FOREGROUND_GREEN;
			}
		}
	}

	// This will draw the player in it''s current position, along with the maze

	// It takes our screen buffer, and the players positive to be draw at.

	// This will draw the intial maze to the screen

	DrawMaze(screenBuffer, playerPos.X, playerPos.Y, ai11Pos.X, ai11Pos.Y, ai22Pos.X, ai22Pos.Y);		

	// Below is our main loop that check to see if we move the player, and if so, redraw the screen.


	while(1)
	{
		// We pass in our player position because we will change it if we move.

		// If we do move, then we return TRUE from MovePlayer() and we need to redraw the screen


		if(MovePlayer(screenBuffer, playerPos))
		{
			// Draw the new updated player position within the maze

			DrawMaze(screenBuffer, playerPos.X, playerPos.Y, NULL, NULL, NULL, NULL);		
		}
		if(MoveAI1(screenBuffer, ai11Pos))
		{
			// Draw the new updated player position within the maze

			DrawMaze(screenBuffer, ai11Pos.X, ai11Pos.Y, NULL, NULL, NULL, NULL);		
		}
				if(MoveAI2(screenBuffer, ai22Pos))
		{
			// Draw the new updated player position within the maze

			DrawMaze(screenBuffer, ai22Pos.X, ai22Pos.Y, NULL, NULL, NULL, NULL);		
		}
	}
}														// End of the program



//////////////////////////////// DRAW MAZE /////////////////////////////////

/////

/////	This draws our screenBuffer to the screen, along with the player position

/////

//////////////////////////////// DRAW MAZE /////////////////////////////////


void DrawMaze(CHAR_INFO screenBuffer[], int playerX, int playerY, int ai11X, int ai11Y, int ai22X, int ai22Y)
{														// This is our rectangle that holds the coordinates that we are drawing too.

	SMALL_RECT drawRect = {0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1}; 
	COORD bufferSize = {SCREEN_WIDTH , SCREEN_HEIGHT};	// This is a COORD that holds the width and height of our window that we will display

	COORD zeroZero = {0, 0};							// This tells us the upper left corner to write from

	HANDLE hOutput;										// This holds our output handle to the screen

	
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);			// Get a OUTPUT handle to our screen.


	// Draw the player at the positive passed in

	screenBuffer[playerX + playerY * SCREEN_WIDTH].Char.AsciiChar = PLAYER;
	screenBuffer[playerX + playerY * SCREEN_WIDTH].Attributes = FOREGROUND_RED;
	screenBuffer[ai11X + ai11Y * SCREEN_WIDTH].Char.AsciiChar = AI1;
	screenBuffer[ai11X + ai11Y * SCREEN_WIDTH].Attributes = FOREGROUND_BLUE;
	screenBuffer[ai22X + ai22Y * SCREEN_WIDTH].Char.AsciiChar = AI2;
	screenBuffer[ai22X + ai22Y * SCREEN_WIDTH].Attributes = FOREGROUND_BLUE;

	// This draws our buffer to the screen lightning fast!

	WriteConsoleOutput(hOutput, screenBuffer, bufferSize, zeroZero, &drawRect);
}


//////////////////////////////// MOVE PLAYER /////////////////////////////////

/////

/////	This checks for keyboard input, changes the players position 

/////   and erases its previous position if the player moved.

/////

//////////////////////////////// MOVE PLAYER /////////////////////////////////


bool MovePlayer(CHAR_INFO screenBuffer[], COORD &playerPos)						
{													
	INPUT_RECORD InputRecord;							// Here is our structure to hold the information on the input buffer (what the user does)

	COORD oldPosition = playerPos;						// This stores the old player positive so we can erase it when we move.

	DWORD Events=0;										// We create DWORD to be compatible with ReadConsoleInput().  (Holds how many input events took place)

	HANDLE hInput;										// This will be used to query the keyboard

	bool bPlayerMoved = false;							// This holds if the player move or not

	int bKeyDown = 0;									// This tells us if we pressed DOWN a key, not UP too

				
	hInput = GetStdHandle(STD_INPUT_HANDLE);			// Here we initialize our input handle "hInput"  A HANDLE could be many things so we specify

														// Read in the input from the user, storing the information into the InputRecord structure.

	ReadConsoleInput(hInput, &InputRecord, 1, &Events);
													
	// Now we check if there was a keyboard event.


	// For Windows 95 and 98, we don''t need this, but for NT based systems, it handles

	// the console functions different.  In Windows 95 and 98, it only registers a key

	// when the key is pressed down, where NT OS''s register the key up and key down.

	// This makes the player go 2 moves every time, so we need to make sure we just

	// move the player when the key is DOWN.  We use the bKeyDown variable stored in the

	// input record, then just make sure it equals "true" when a key is hit.

	// Notice the "&& bKeyDown" in the following if statement.  That says that if the key

	// was pressed, we only want to enter that if statement if it was a key down press.

	bKeyDown = InputRecord.Event.KeyEvent.bKeyDown;

	if(InputRecord.EventType == KEY_EVENT && bKeyDown)
	{													// If the user hit the keyboard:

														// Check to see if the user hit the RIGHT arrow key														

		if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
		{												
			if(playerPos.X < (SCREEN_WIDTH - 1) )		// Check to make sure our new position doesn''t go outside of our buffer

			{
				playerPos.X++;							// Increase the player''s X value because we are going RIGHT.

				bPlayerMoved = true;					// Set the flag to TRUE that the player moved

			}
		}	
														// Check to see if we hit the LEFT arrow key

		else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
		{										
			if(playerPos.X > 0)							// Check to make sure our new position doesn''t go outside of our buffer

			{
				playerPos.X--;							// Increase the player''s X value because we are going LEFT.

				bPlayerMoved = true;					// Set the flag to TRUE that the player moved

			}
		}											
														// Lets check if we hit the UP arrow:								

		else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
		{
			if(playerPos.Y > 0)							// Check to make sure our new position doesn''t go outside of our buffer

			{
				playerPos.Y--;							// Increase the player''s X value because we are going UP.

				bPlayerMoved = true;					// Set the flag to TRUE that the player moved

			}
		}	
														// Let''s check if we hit the DOWN 

		else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
		{							
			if(playerPos.Y < (SCREEN_HEIGHT - 1) )		// Check to make sure our new position doesn''t go outside of our buffer

			{
				playerPos.Y++;							// Increase the player''s X value because we are going DOWN.

				bPlayerMoved = true;					// Set the flag to TRUE that the player moved

			}
		}												// If we hit ESCAPE

		else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
		{												
			exit(0);									// Quit the program

		}
	}	

	// If the player moved

	if(bPlayerMoved)
	{
		// Here we check for collision with a wall.  If our new positive is the same X and Y

		// as a wall, we want to set the characters position back to it''s old position.

		// Otherwise, if it isn''t a wall, then we want to erase the old position with a space


		// If we didn''t hit a wall, erase the old position

		if(screenBuffer[playerPos.X + playerPos.Y * SCREEN_WIDTH].Char.AsciiChar != WALL)
		{
			// Erase the old position of the player with a space

			screenBuffer[oldPosition.X + oldPosition.Y * SCREEN_WIDTH].Char.AsciiChar = '' '';
			screenBuffer[oldPosition.X + oldPosition.Y * SCREEN_WIDTH].Attributes = 0;
		}
		else // We hit a wall

		{
			// Set the players position back to it''s old position (Hence, we collided)

			playerPos = oldPosition;
		}

		return true;									// Return true to say we need to redraw the screen

	}

	return false;
	while(1)
	{
bool MoveAI1(CHAR_INFO screenBuffer[], COORD &ai11Pos)//line 213						

{																				
	COORD oldPosition1 = ai11Pos;
	bool bAI1Moved = false;
	switch(!(rand() %4))
	{
	case 1:ai11Pos.X++, bAI1Moved = true;
		break;
	case 2:ai11Pos.X--, bAI1Moved = true;
		break;
	case 3:ai11Pos.Y++, bAI1Moved = true;
		break;
	case 4:ai11Pos.Y--, bAI1Moved = true;
		break;
	}
		if(bAI1Moved)
	{
		// Here we check for collision with a wall.  If our new positive is the same X and Y

		// as a wall, we want to set the characters position back to it''s old position.

		// Otherwise, if it isn''t a wall, then we want to erase the old position with a space


		// If we didn''t hit a wall, erase the old position

		if(screenBuffer[ai11Pos.X + ai11Pos.Y * SCREEN_WIDTH].Char.AsciiChar != WALL)
		{
			// Erase the old position of the player with a space

			screenBuffer[oldPosition1.X + oldPosition1.Y * SCREEN_WIDTH].Char.AsciiChar = '' '';
			screenBuffer[oldPosition1.X + oldPosition1.Y * SCREEN_WIDTH].Attributes = 0;
		}
		else // We hit a wall

		{
			// Set the players position back to it''s old position (Hence, we collided)

			ai11Pos = oldPosition1;
		}
}

	while(1)
	{  //248

bool MoveAI2(CHAR_INFO screenBuffer[], COORD &ai22Pos)						
{																				
	COORD oldPosition2 = ai22Pos;
	bool bAI2Moved = false;
	switch(!(rand() %4))
	{
	case 1:ai22Pos.X++, bAI2Moved = true;
		break;
	case 2:ai22Pos.X--, bAI2Moved = true;
		break;
	case 3:ai22Pos.Y++, bAI2Moved = true;
		break;
	case 4:ai22Pos.Y--, bAI2Moved = true;
		break;
	}
		if(bAI2Moved)
	{
		// Here we check for collision with a wall.  If our new positive is the same X and Y

		// as a wall, we want to set the characters position back to it''s old position.

		// Otherwise, if it isn''t a wall, then we want to erase the old position with a space


		// If we didn''t hit a wall, erase the old position

		if(screenBuffer[ai22Pos.X + ai22Pos.Y * SCREEN_WIDTH].Char.AsciiChar != WALL)
		{
			// Erase the old position of the player with a space

			screenBuffer[oldPosition2.X + oldPosition2.Y * SCREEN_WIDTH].Char.AsciiChar = '' '';
			screenBuffer[oldPosition2.X + oldPosition2.Y * SCREEN_WIDTH].Attributes = 0;
		}
		else // We hit a wall

		{
			// Set the players position back to it''s old position (Hence, we collided)

			ai22Pos = oldPosition2;
		}
	}



}// 287

  
anyone please help?

Share this post


Link to post
Share on other sites
i think its best to ignore the strange comments that have got themselves way over the page, they are not like that in the code

Share this post


Link to post
Share on other sites
this tutorial isnt on game dev, its on game tutorials page 3, third article(http://www.gametutorials.com/Tutorials/c++/Cpp_Pg4.htm)

[edited by - com on August 31, 2002 1:41:47 PM]

Share this post


Link to post
Share on other sites
i believe part of the problem is that you are missing some braces.
also you might want to comment which braces belong to which loop or branch for better clarity.

Knowledge is what you learn, wisdom is how you apply it.

Share this post


Link to post
Share on other sites
You seem to be confused about the usage of the while loop. May I ask how long you''ve been using C++? The while loop, in the original code, is used to loop for as long as the game is running, updating the board as necessary. You''ll simply need to call the functions you''ve added from within the original loop (and you might want to add some timing).

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites