I can't make my player show up right

Started by
2 comments, last by GameDev.net 18 years, 2 months ago
ok I'm makin a console version of pac-man to help me learn C++ better and I've run into a problem with making my player show up right. I can make him show up outside the maze and move around as long as he is still in the draw buffer; but, I can't get him to show up in the maze. If I initialize him to start up inside the maze the game either crashes or I don't see him anywhere. Any help would be much appericiated. My code is below. Thanks, Rubiks14

#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

#define WIDTH 15
#define HEIGHT 11
#define START_X 32
#define START_Y 8
#define WALL '#'
#define DOT '*'
#define SPACE ' '
#define PLAYER 'C'

char piece = ' ';
COORD playerpos;

void DrawMap(CHAR_INFO screenbuffer[], int playerx, int playery);

bool MovePlayer(CHAR_INFO screenbuffer[], COORD &position);

int main()
{
	CHAR_INFO screenbuffer[WIDTH * HEIGHT];
	COORD size = {WIDTH, HEIGHT};
	ifstream fin;

	fin.open("map1.map");

	if(fin.fail())
	{
		cout << "Can't find file\n";
	}

	for(int y = 0; y < size.Y; y++)
	{
		for(int x = 0; x < size.X; x++)
		{
			fin >> piece;
			
			if(piece == '_')
			{
				screenbuffer[x + y * WIDTH].Char.AsciiChar = ' ';
				screenbuffer[x + y * WIDTH].Attributes = 0x0000;
			}
			else if(piece == '-')
			{
				screenbuffer[x + y * WIDTH].Char.AsciiChar = DOT;
				screenbuffer[x + y * WIDTH].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
			}
			else
			{
				screenbuffer[x + y * WIDTH].Char.AsciiChar = WALL;
				screenbuffer[x + y * WIDTH].Attributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
			}
		}
	}
	fin.close();

	DrawMap(screenbuffer, playerpos.X, playerpos.Y);


	while(1)
	{
		if(MovePlayer(screenbuffer, playerpos))
		{
			DrawMap(screenbuffer, playerpos.X, playerpos.Y);
		}
	}
	return 0;
}

void DrawMap(CHAR_INFO screenbuffer[], int playerx, int playery)
{
	SMALL_RECT draw = {START_X, START_Y, START_X + WIDTH, START_Y + HEIGHT};
	COORD size = {WIDTH , HEIGHT};
	COORD zero = {0, 0};
	HANDLE outH;

	outH = GetStdHandle(STD_OUTPUT_HANDLE);

	screenbuffer[playerx + playery * WIDTH].Char.AsciiChar = 'C';
	screenbuffer[playerx + playery * WIDTH].Attributes = FOREGROUND_RED | FOREGROUND_GREEN;

	WriteConsoleOutput(outH, screenbuffer, size, zero, &draw);
}


bool MovePlayer(CHAR_INFO screenbuffer[], COORD &position)
{
	INPUT_RECORD input;
	COORD old = position;
	DWORD events = 0;
	HANDLE inH;

	inH = GetStdHandle(STD_INPUT_HANDLE);

	ReadConsoleInput(inH, &input, 1, &events);

	if(input.Event.KeyEvent.bKeyDown)
	{
		if(input.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
		{
			position.X++;

			if(screenbuffer[position.X + position.Y * WIDTH].Char.AsciiChar != WALL)
			{
				screenbuffer[old.X + old.Y * WIDTH].Char.AsciiChar = ' ';
			}
			else
			{
				position = old;
			}
		}
		else if(input.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
		{
			position.X--;

			if(screenbuffer[position.X + position.Y * WIDTH].Char.AsciiChar != WALL)
			{
				screenbuffer[old.X + old.Y * WIDTH].Char.AsciiChar = ' ';
			}
			else
			{
				position = old;
			}
		}
		else if(input.Event.KeyEvent.wVirtualKeyCode == VK_UP)
		{
			position.Y--;

			if(screenbuffer[position.X + position.Y * WIDTH].Char.AsciiChar != WALL)
			{
				screenbuffer[old.X + old.Y * WIDTH].Char.AsciiChar = ' ';
			}
			else
			{
				position = old;
			}
		}
		else if(input.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
		{
			position.Y++;

			if(screenbuffer[position.X + position.Y * WIDTH].Char.AsciiChar != WALL)
			{
				screenbuffer[old.X + old.Y * WIDTH].Char.AsciiChar = ' ';
			}
			else
			{
				position = old;
			}
		}
	return true;
	}
return false;
}
Advertisement
ha ha. I found it. for some reason I had to move fin.close(); to after my while loop. I dunno why that matters but it works.
i wish to ask where you came accross

HANDLE
COORD
DWORD
INPUT_RECORD
SMALL_RECT and
CHAR_INFO

in your C++ studies
Quote:Original post by Rubiks14
ha ha. I found it. for some reason I had to move fin.close(); to after my while loop. I dunno why that matters but it works.


I'm going to assume you mean move "fin.close();" to outside of your while loop, from inside the loop. The reason it matters is because you would be closing the file stream, and on the next loop cycle when it tries to access the file, it won't be able to anymore because you killed the connection.



This topic is closed to new replies.

Advertisement