1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
enum Color // 1byte/8 bits of info, 128 = bgIntensity, 64 = bgRed, 32 = bgGreen, 16 = bgBlue, 8 = frIntensity, 4 = frRed, 2 = frGreen, 1 = frBlue, 0 = none
{
NONE = 0,
BLUE = 1,
GREEN = 2,
TEAL = 3,
RED = 4,
lightTEAL = 11,
lightBLUE = 9,
lightGREEN = 10,
lightRED = 12
};
void MoveCursor(int y, int x)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //Points to what console window that is currently beeing used
COORD coord; //??
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord); //??
}
void ObjectAdd(int y, int x)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //Points to what console window that is currently beeing used
COORD coord; //??
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord); //??
}
void SetColor(Color forecolor, Color backcolor) //pass color enums
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, (WORD)forecolor + ((WORD)backcolor << 4)); //Shift the 4 backcolor bits to left
}
class Map //group togeather relevant variables/functions, private by default
{
public:
Map(int width, int height) //Construct
{
mapWidth = width;
mapHeight = height;
map = new char*[height];
int heightCreated = 0;
int widthCreated = 0;
char toWrite;
while (heightCreated < height)
{
map[heightCreated] = new char[width];
widthCreated = 0;
while (widthCreated < width)
{
if (heightCreated == 0 || heightCreated == mapHeight - 1)
{
toWrite = 'X';
}
else if (widthCreated == 0 || widthCreated == mapWidth - 1)
{
toWrite = 'X';
}
else
{
toWrite = ' ';
}
map[heightCreated][widthCreated] = toWrite;
widthCreated = widthCreated + 1;
}
heightCreated = heightCreated + 1;
}
}
private:
int mapWidth;
int mapHeight;
char **map;
public:
void Draw(int yPos, int xPos)
{
MoveCursor(0, 0);
SetColor(Color::lightBLUE, Color::BLUE); //Use :: for clarification. Called Scope when using ::
int mapHeigthPrinted = 0;
int mapWidthPrinted = 0;
while (mapHeigthPrinted < mapHeight)
{
mapWidthPrinted = 0;
while (mapWidthPrinted < mapWidth)
{
cout << map[mapHeigthPrinted][mapWidthPrinted];
mapWidthPrinted = mapWidthPrinted + 1;
}
cout << endl;
mapHeigthPrinted++;
}
SetColor(Color::lightGREEN, Color::NONE); //Use :: for clarification. Called Scope when using ::
cout << "Coordinates " << endl << "X: " << xPos << " " << endl << "Y: " << yPos << " " << endl;
}
void DrawObject(int y, int x, char object) //gets object at y,x coord and what type of object it is.
{
SetColor(Color::RED, Color::RED);
ObjectAdd(y, x); //Send coords to console
cout << object; //Display object (not interactive??)
}
char GetObjectAt(int y, int x)
{
if (y >= 0 && y < mapHeight && x >= 0 && x < mapWidth)
{
return map[y][x]; //Computer reads Y-axis first, X-Axis after
}
return 'X';
}
};
class PlayerChar //Private from start, struct is used to clump variables togeather without worrying about changes
{
private:
public:
char symbol = 1;
int POSx = 1;
int POSy = 1;
void Draw()
{
SetColor(Color::lightTEAL, Color::BLUE); //Use :: for clarification. Called Scope when using ::
MoveCursor(POSy, POSx);
cout << symbol;
}
void TryMoveTo(int y, int x, char ObjectAt)
{
switch (ObjectAt)
{
case 'X':
break;
case ' ':
POSy = y;
POSx = x;
break;
case 'T':
POSy = y + 5;
POSx = x + 5;
break;
default:
break;
}
/*if (ObjectAt != 'X') // Collision agains X Use Switch instead of better management instead of hundreds of if statements.
{
POSy = y;
POSx = x;
}
*/
}
};
int main()
{
#pragma region Size_of_Map
int width;
int height;
//menu();
cout << "How big map do you want? (full screen is Width: 79, Height 21)" << endl
<< "Width (Max 79 or it will default to 10): ";
cin >> width;
cout << "Height (Max 21 or it will default to 10): ";
cin >> height;
if (width > 79 || width < 0)
{
width = 79;
}
else if (height > 21 || height < 0)
{
height = 21;
}
else
{
cout << "Critical error. Shutting down";
}
system("CLS");
#pragma endregion Size_of_Map
Map world(width, height);
PlayerChar hero;
world.Draw(hero.POSy, hero.POSx);
world.DrawObject(5, 5, 'T');
hero.Draw();
int toPosX = 0;
int toPosY = 0;
bool wantstomove = false;
bool play = true;
while (play)
{
char movement = _getch();
wantstomove = false;
switch (movement)
{
case 'a': //left
{
toPosX = hero.POSx - 1;
toPosY = hero.POSy;
wantstomove = true;
break;
}
case 'w': //up
{
toPosY = hero.POSy - 1;
toPosX = hero.POSx;
wantstomove = true;
break;
}
case 'd': //right
{
toPosX = hero.POSx + 1;
toPosY = hero.POSy;
wantstomove = true;
break;
}
case 's': //down
{
toPosY = hero.POSy + 1;
toPosX = hero.POSx;
wantstomove = true;
break;
}
case 'q': //quit
{
play = false;
break;
}
case 'r': //reset
{
hero.POSx = 1;
hero.POSy = 1;
break;
}
default: //Do nothing if anything not binded is not binded.
{
break;
}
}
if (wantstomove == true)
{
char ObjectAt = world.GetObjectAt(toPosY, toPosX); //Call Map class with world., use function GetObjectAt(Y-coord, X-Coord)
hero.TryMoveTo(toPosY, toPosX, ObjectAt); //Call PlayerChar class with hero., use function TryMoveTo(Y-coord, X-Coord, Send what object is where we are going to move
}
world.Draw(hero.POSy, hero.POSx); //don't pass any arguments in normal use. pass hero.POSx, hero.POSy for coords.
world.DrawObject(5, 5, 'T');
hero.Draw();
}
return 0;
}
|