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
|
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char move;
int plrX = 1, plrY = 1; // player start point
int map[10][10] = {
5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 1, 0, 5, 5, 5, 0, 0, 0, 5,
5, 0, 0, 0, 0, 0, 0, 0, 0, 5,
5, 0, 5, 0, 5, 5, 0, 5, 5, 5,
5, 0, 5, 0, 5, 0, 0, 0, 0, 5,
5, 0, 0, 0, 0, 0, 0, 0, 0, 5,
5, 5, 5, 5, 0, 0, 5, 5, 0, 5,
5, 0, 5, 0, 0, 0, 0, 0, 0, 5,
5, 0, 0, 0, 0, 0, 0, 0, 0, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
};
// I know, I know _getch() is frowned
// upon but Ive used it here just to
// keep it simple and to demonstrate
// the array/player question.. so
// before someone corrects me dont
// bother :)
do
{
// grr i hate using system commands
// but for this it keeps things tidy
// so i am sure i can live with it
// just for today :)
system("cls");
// display the map, just a simple loop
for (int down = 0; down < 10; down++) {
for (int across = 0; across < 10; across++) {
switch (map[down][across])
{
case 0: // draw nothing
cout << " ";
break;
case 1: // player1
cout << "@";
break;
case 5: // wall
cout << "*";
break;
}
}
cout << endl;
}
// read the move
move = _getch();
move = toupper(move);
switch (move)
{
// left
case 'L' :
if (plrY > 1) // if 1 we are against the wall.
{
if (map[plrX][plrY-1] == 0)
{
map[plrX][plrY] = 0; // clear existing spot
map[plrX][plrY - 1] = 1; // new position.
plrY--;
}
}
break;
// right
case 'R' :
if (plrY < 9) // if 9 we are against the wall.
{
if (map[plrX][plrY + 1] == 0)
{
map[plrX][plrY] = 0; // clear existing spot
map[plrX][plrY + 1] = 1; // new position.
plrY++;
}
}
break;
// up
case 'U' :
if (plrX > 1) // if 1 we are against the wall.
{
if (map[plrX-1][plrY] == 0)
{
map[plrX][plrY] = 0; // clear existing spot
map[plrX-1][plrY] = 1; // new position.
plrX--;
}
}
break;
// down
case 'D' :
if (plrX < 9) // if 9 we are against the wall.
{
if (map[plrX + 1][plrY] == 0)
{
map[plrX][plrY] = 0; // clear existing spot
map[plrX + 1][plrY] = 1; // new position.
plrX++;
}
}
break;
}
} while (move != 'Q');
return 0;
}
|