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
|
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
char *map[] = {
"XXXXXXXXXXXXXXXXX",
"X X",
"X X",
"X X",
"X X",
"X X",
"XXXXXXXXXXXXXXXXX" };
void MoveCursor(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord);
}
void DrawMap(int xPos, int yPos) //normally don't take arguments otherwise get xPos, yPos
{
MoveCursor(0, 0);
int mapRows = 7;
int mapRowsPrinted = 0;
while (mapRowsPrinted < mapRows)
{
cout << map[mapRowsPrinted] << endl;
mapRowsPrinted++;
}
cout << "Coordinates: " << endl << "X: " << xPos << " " << endl << "Y: " << yPos << " "; // comment out to restore
}
int main()
{
char heroChar = 1;
int heroPOSx = 2;
int heroPOSy = 2;
DrawMap(heroPOSy, heroPOSx);
bool play = true;
MoveCursor(heroPOSx, heroPOSy);
cout << heroChar;
while (play)
{
char movement = _getch();
switch (movement)
{
case 'a':
{
heroPOSy = heroPOSy - 1;
if (map[heroPOSx][heroPOSy] == 'X') // Collision agains X from bottom
{
heroPOSy = heroPOSy + 1;
}
break;
}
case 'w':
{
heroPOSx = heroPOSx - 1;
if (map[heroPOSx][heroPOSy] == 'X') // Collision agains X from right
{
heroPOSx = heroPOSx + 1;
}
break;
}
case 'd':
{
heroPOSy = heroPOSy + 1;
if (map[heroPOSx][heroPOSy] == 'X') // Collision agains X from top
{
heroPOSy = heroPOSy - 1;
}
break;
}
case 's':
{
heroPOSx = heroPOSx + 1;
if (map[heroPOSx][heroPOSy] == 'X') // Collision agains X from left
{
heroPOSx = heroPOSx - 1;
}
break;
}
case 'q':
{
play = false;
break;
}
default: //return to starting spot
{
heroPOSx = 1;
heroPOSy = 1;
break;
}
}
DrawMap(heroPOSy, heroPOSx); //don't pass any arguments in normal use. pass heroPOSx, heroPOSy for coords.
MoveCursor(heroPOSy, heroPOSx);
cout << heroChar;
}
return 0;
}
|