I made an object-oriented maze game. It compiles but I can't move the character around in the maze. Please help me out, thank you.
Person.h file:
#include <windows.h>
#include <process.h>
#pragma once
class Person
{
private:
int m_X;
int m_Y;
public:
WORD Color;
Person();
~Person();
void DrawASCIICharacter(int x, int y, unsigned char ch, WORD CharAttribute, CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight);
void DrawPerson(CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight);
void SetX(int x);
void SetY(int y);
int GetX() const {return m_X;}
int GetY() const {return m_Y;}
};`
Person.cpp file:
#include "Person.h"
Person::Person()
{
m_X = 1;
m_Y = 1;
Color = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
}
Person::~Person()
{
}
void Person::DrawASCIICharacter(int x, int y, unsigned char ch, WORD CharAttribute, CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight)
{
if (x < 0) return;
if (y < 0 ) return;
if (x >= ScreenWidth) return;
if (y >= ScreenHeight) return;
pScreen[y*ScreenWidth + x].Char.AsciiChar = ch;
pScreen[y*ScreenWidth + x].Attributes = CharAttribute;
}
void Person::DrawPerson(CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight)
{
DrawASCIICharacter(m_X, m_Y, 2, Color, pScreen, ScreenWidth, ScreenHeight);
}
void Person::SetX(int x)
{
m_X = x;
x = 1;
}
void Person::SetY(int y)
{
m_Y = y;
y = 1;
}
Map.h file:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
#include <process.h>
#include <cstdlib>
#include <ctime>
using namespace std;
#pragma once
class Map
{
private:
int m_mazeWidth;
int m_mazeHeight;
//int m_mazeArray[45*20];
public:
WORD mazeColor;
WORD endColor;
WORD keyColor;
WORD doorColor;
WORD backgroundColor;
WORD winColor;
Map();
~Map();
bool CanPlayerEnterSpace(int x, int y);
void DrawASCIICharacter(int x, int y, unsigned char ch, WORD CharAttribute, CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight);
void DrawMaze1234(CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight);
void ClearScreen(WORD CharAttribute, CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight);
};
Map.cpp file:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <cstdlib>
#include <ctime>
#include "Map.h"
#include "Person.h"
Map::Map()
{
m_mazeWidth;
m_mazeHeight;
mazeColor = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_INTENSITY;
endColor = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_GREEN | BACKGROUND_INTENSITY;
keyColor = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_INTENSITY;
doorColor = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_RED | BACKGROUND_INTENSITY;
backgroundColor = 0;
winColor = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
}
Map::~Map()
{
}
void Map::DrawASCIICharacter(int x, int y, unsigned char ch, WORD CharAttribute, CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight)
{
if (x < 0) return;
if (y < 0 ) return;
if (x >= ScreenWidth) return;
if (y >= ScreenHeight) return;
pScreen[y*ScreenWidth + x].Char.AsciiChar = ch;
pScreen[y*ScreenWidth + x].Attributes = CharAttribute;}
void Map::DrawMaze1234(CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight)
{
int mazeArray[45*20] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
int mazeHeight = 45;
int mazeWidth = 20;
//int mazeArray[45*20];
int r, c;
for (r = 0; r < mazeHeight; r++)
{
for (c=0; c < mazeWidth; c++)
{
switch (mazeArray[r*mazeWidth + c])
{
case 1: DrawASCIICharacter(c, r, 219, mazeColor, pScreen, 20, 45); break;
case 2: DrawASCIICharacter(c, r, 212, mazeColor, pScreen, 20, 45); break;
case 3: DrawASCIICharacter(c, r, '*', endColor, pScreen, 20, 45); break;
case 4: DrawASCIICharacter(c, r, 'K', keyColor, pScreen, 20, 45); break;
case 5: DrawASCIICharacter(c, r, 68, doorColor, pScreen, 20, 45); break;
}
}
}
}
bool Map::CanPlayerEnterSpace(int x, int y)
{
int mazeArray[45*20];
int mazeWidth = 20;
int keys = 0;
if (mazeArray[y*mazeWidth + x] == 1) return false;
if (mazeArray[y*mazeWidth + x] == 5)
{
if (keys == 0) return false;
else
{
keys--;
mazeArray[y*mazeWidth + x] = 0;
return true;
}
}
return true;
}
void Map::ClearScreen(WORD CharAttribute, CHAR_INFO *pScreen, int ScreenWidth, int ScreenHeight)
{
int r, c;
for (r = 0; r < ScreenHeight; r++)
{
for (c = 0; c < ScreenWidth; c++)
{
pScreen[r*ScreenWidth + c].Char.AsciiChar = ' ';
pScreen[r*ScreenWidth + c].Attributes = CharAttribute;
}
}
}
main.cpp file:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <cstdlib>
#include <ctime>
#include "Person.h"
#include "Map.h"
using namespace std;
//Constant Global Variables
const int WIDTH = 45;
const int HEIGHT = 30;
const WORD BACKGROUND_COLOR = 0;
const WORD BACKGROUND_COLOR_WIN = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
const WORD PERSON_COLOR = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_COLOR;
const WORD END_COLOR = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_GREEN | BACKGROUND_COLOR;
const WORD KEY_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_COLOR;
const WORD DOOR_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_RED | BACKGROUND_COLOR;
const WORD WALL_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD MAZE_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_COLOR;
CHAR_INFO SCREEN_BUFFER[HEIGHT * WIDTH];
COORD BUFFER_SIZE = {WIDTH, HEIGHT};
COORD CHARACTER_POSITION = {0,0};
SMALL_RECT WINDOW_SIZE = {0, 0, WIDTH-1, HEIGHT-1};
//Function Prototypes
unsigned int __stdcall UpdateScreenThread(void *pThreadData);
//Global Variables
HANDLE g_hWin; // Handle to the Standard Output
HANDLE g_hInput; // Handle to the Standard Input
bool g_ISRUNNING = true;
bool isGameDone = false;
int keys = 0;
Map maze;
Person person;
int main()
{
//person.DrawPerson(SCREEN_BUFFER, WIDTH, HEIGHT);
//maze.DrawMaze1234(SCREEN_BUFFER, WIDTH, HEIGHT);
DWORD numEvents, numEventsRead = 0; // number of Events that have happened
INPUT_RECORD inputEvent; // Event from the input (or windows)
//------------------------------------------
// Setup the windows console
//------------------------------------------
unsigned int seed = time(0);
srand(seed);
// get the handles to the standard windows
g_hWin = GetStdHandle(STD_OUTPUT_HANDLE);
g_hInput = GetStdHandle(STD_INPUT_HANDLE);
// Set the title of the window
SetConsoleTitle(TEXT("The Labyrinth"));
// Set the console window size
SetConsoleWindowInfo(g_hWin, TRUE, &WINDOW_SIZE);
// Set the buffer size
SetConsoleScreenBufferSize(g_hWin, BUFFER_SIZE);
maze.ClearScreen(BACKGROUND_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
//------------------------------------------
// This starts a thread that draws the screen
// ever 50 milliseconds
//------------------------------------------
unsigned int threadID = 0;
_beginthreadex(NULL, 0, UpdateScreenThread, NULL, 0, &threadID);
//------------------------------------------
// This is the main Loop
//------------------------------------------
while (g_ISRUNNING)
{
// Check the events
GetNumberOfConsoleInputEvents(g_hInput, &numEvents);
if (numEvents)
{
ReadConsoleInputA(g_hInput, &inputEvent, 1, &numEventsRead);
if (numEventsRead)
{
// An event has been read in
// Check the event type: was it a key?
switch (inputEvent.EventType)
{
case KEY_EVENT:
if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
{
g_ISRUNNING = false;
}
if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
{
int p = person.GetY();
if (maze.CanPlayerEnterSpace(person.GetX(), person.GetY() + 1))
p++;
}
if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_UP)
{
int p = person.GetY();
if (maze.CanPlayerEnterSpace(person.GetX(), person.GetY() - 1))
p--;
}
if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
int p = person.GetX();
if (maze.CanPlayerEnterSpace(person.GetX() - 1, person.GetY()))
p--;
}
if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
int p = person.GetX();
if (maze.CanPlayerEnterSpace(person.GetX() + 1, person.GetY()))
p++;
}
break;
}
}
}
//------------------------------------------
// You Game code that you want to run "fast" goes here
//------------------------------------------
/*if(maze.DrawMaze1234(SCREEN_BUFFER, WIDTH, HEIGHT)
{
if (person.DrawPerson(SCREEN_BUFFER, WIDTH, HEIGHT) == 3)
{
isGameDone = true;
}
if(person.DrawPerson(SCREEN_BUFFER, WIDTH, HEIGHT) == 4)
{
keys++;
mazeArray[g_personY*mazeWidth + g_personX] = 0;
}
}*/
} //while
return 0;
}
unsigned int __stdcall UpdateScreenThread(void *pThreadData)
{
while(g_ISRUNNING)
{
// call out timer update function
// TimerUpdate();
if (isGameDone)
{
// Update the Screen
maze.ClearScreen(BACKGROUND_BLUE | BACKGROUND_INTENSITY, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(17, 9, 'Y', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(18, 9, 'o', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(19, 9, 'u', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(20, 9, ' ', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(21, 9, 'W', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(22, 9, 'i', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(23, 9, 'n', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawASCIICharacter(24, 9, '!', PERSON_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
}
else
{
// Update the Screen
maze.ClearScreen(BACKGROUND_COLOR, SCREEN_BUFFER, WIDTH, HEIGHT);
maze.DrawMaze1234(SCREEN_BUFFER, WIDTH, HEIGHT);
person.DrawPerson(SCREEN_BUFFER, WIDTH, HEIGHT);
}
// WriteConsoleOutputA is WriteConsoleOutput ASCII
WriteConsoleOutputA(g_hWin, SCREEN_BUFFER, BUFFER_SIZE, CHARACTER_POSITION, &WINDOW_SIZE);
// Wait 50 milli-seconds
Sleep(50);
}
return 0;
}