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
|
#include <iostream>
#include <Windows.h>
#define MAP_ROWS 15
#define MAP_COLS 20
using namespace std;
void displayMap( char mapArray[MAP_ROWS][MAP_COLS] );
void MovePlayer( char mapArray[MAP_ROWS][MAP_COLS], int& plrX, int& plrY, char Dir );
int main()
{
int playerX = 5;
int playerY = 5;
char playerMove = ' ';
char mapArray[MAP_ROWS][MAP_COLS] =
{
{'_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','|'}
};
displayMap( mapArray );
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CurPos;
CurPos.X = 5;
CurPos.Y = MAP_ROWS + 3 ;
SetConsoleCursorPosition( console, CurPos );
cout << "Choose a direction to move (U, D, L, R): ";
while( true )
{
CurPos.X = 46; // at end of prompt string
CurPos.Y = MAP_ROWS + 3 ;
SetConsoleCursorPosition( console, CurPos );
cin >> playerMove;
if( playerMove == 'q' ) // Quit
break;
else
MovePlayer( mapArray, playerX, playerY, playerMove );
}
return 0;
}
void displayMap( char mapArray[MAP_ROWS][MAP_COLS] )
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CurPos;
for( int row = 0; row < MAP_ROWS; row++ )
{
// moves the map in 5 chars from left and leave an empty row at top
CurPos.X = 5;
CurPos.Y = row + 1 ;
SetConsoleCursorPosition( console, CurPos );
for( int col = 0; col < MAP_COLS; col++ )
{
cout << mapArray[row][col];
}
}
}
void MovePlayer( char mapArray[MAP_ROWS][MAP_COLS], int& plrX, int& plrY, char Dir )
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CurPos;
// restore map tile at player's current location
CurPos.X = plrX + 5;
CurPos.Y = plrY + 1 ;
SetConsoleCursorPosition( console, CurPos );
cout << mapArray[plrY][plrX];
// Move the player
switch( Dir )
{
case 'U':
case 'u':
if( plrY > 1 )
plrY--;
break;
case 'D':
case 'd':
if( plrY < (MAP_ROWS - 2) )
plrY++;
break;
case 'L':
case 'l':
if( plrX > 1 )
plrX--;
break;
case 'R':
case 'r':
if( plrX < (MAP_COLS - 2) )
plrX++;
break;
default:
// ignore bad input
break;
}
// Draw character at new location
CurPos.X = plrX + 5;
CurPos.Y = plrY + 1 ;
SetConsoleCursorPosition( console, CurPos );
cout << '@';
return;
}
| |