Hi guys just making typical console game where hero moves with w a s d.
However, in my code, while a and w work (minusing), s and d dont work (adding). I have used a switch statement to change the char position of the hero based on kbhit.When you hit s or d, the buttons that add 1 to the 2nd dimension of the array, the screen goes crazy! Help appreciated
const char PLAYER = 'H';
const char WALLS = '=';
const int ROWS = 20;
const int COLS = 50;
//Map class generates map, player and enemies
class CMap{
public:
char m_cMap[20][50];
//Map constructor
CMap(int _row, int _col){
//Spawn boarders
for (int i = 0; i < ROWS; i++){
for (int j = 0; j < COLS; j++){
if (i == 0 || i == ROWS - 1){
m_cMap[i][j] = WALLS;
}else if (j == 0 || j == COLS - 1){
m_cMap[i][j] = WALLS;
}else{
m_cMap[i][j] = ' ';
}
}
}
//Spawn player
m_cMap[_row][_col] = PLAYER;
}
};
class CGame{
private:
void Move(CMap& _map, char _move, int _i, int _j){
_map.m_cMap[_i][_j] = ' ';
switch (_move){
case 'w':
case 'W':
_i--;
break;
case 's':
case 'S':
_i++;
break;
case 'a':
case 'A':
_j--;
break;
case 'd':
case 'D':
_j++;
break;
default:
break;
}
_map.m_cMap[_i][_j] = PLAYER;
}
public:
//Functions for the main gameloop
void Update(CMap& _map, char _move){
for (int i = 0; i < ROWS; i++){
for (int j = 0; j < COLS; j++){
//Move Player
if (_map.m_cMap[i][j] == PLAYER){
Move(_map, _move, i, j);
}
//Move Enemies...
}
}
}
void Check(CMap _map){}
void Display(CMap _map){
system("CLS");
for (int i = 0; i < ROWS; i++){
for (int j = 0; j < COLS; j++){
cout << _map.m_cMap[i][j];
}
cout << endl;
}
}
};
int main(){
//Generate random numbers for player spawn
srand(time(0));
int randRow = (rand() % 17) + 1;
int randCol = randRow + 20;
//Instantiate the game and map objects
CGame game;
CMap map(randRow, randCol);