Erroneous discoloration of movement in the console game
-
I'm writing a full game and I'm having a problem.
Created a method that records the character symbol in the new coordinates of the two-storey area and transforms it into new coordinates.'.'
- empty field where he was.ChangePositiont
the character moves right in three directions, but when moving to the left, it leaves a trail behind, what could be a mistake?#include<iostream> #include<conio.h>
using namespace std;
const int size_x=15;
const int size_y=30;
char world[size_x][size_y];class Player
{
public:
int point_x;
int point_y;
int HP;
Player()
{
point_x = 0;
point_y = 0;
HP = 100;
};
Player(int x, int y, char skin)
{this->point_x = x; this->point_y = y; this->HP = 100; for (int i = 0; i < size_x; i++) { for (int k = 0; k < size_y; k++) { if (i == this->point_x && k == this->point_y) { world[i][k] = skin; break; } } } }; void ChangePositiont(char skin) { for (int i = 0; i < size_x; i++) { for (int k = 0; k < size_y; k++) { if (i == this->point_x && k == this->point_y) { world[i][k] = skin; break; } else { world[i][k] = '.'; } } } }
};
class Enemy :public Player
{
public:
Enemy(int x, int y, char skin)
{this->point_x = x; this->point_y = y; this->HP = 100; for (int i = 0; i < size_x; i++) { for (int k = 0; k < size_y; k++) { if (i == this->point_x && k == this->point_y) { world[i][k] = skin; break; } } } };
};
/class Sqare : public Player
{
public:
Sqare(int x1, int x2, int y1, int y2)
{
for (int k = 0; k < y2-y1; k++)
{
for (int i = 0; i < x2-x1; i++)
{
world[y1 + k][x1 + i] = '1';
}
}
}
};/void ArrFill(char a)
{
for (int i = 0; i < size_x; i++)
{for (int j = 0; j < size_y; j++) { world[i][j] = a; } }
};
void ShowScene()
{for (int i = 0; i < size_x; i++) { for (int k=0; k < size_y; k++) { cout << world[i][k]; } cout << endl; }
}
void Move(Player &pl)
{
int a = _getche();
switch (a)
{
case 'a':
pl.point_y=pl.point_y-1;
pl.ChangePositiont('$');
break;
case 'd':
pl.point_y = pl.point_y + 1;
pl.ChangePositiont('$');
break;
case 's':
pl.point_x = pl.point_x + 1;
pl.ChangePositiont('$');
break;
case 'w':
pl.point_x = pl.point_x - 1;
pl.ChangePositiont('$');
break;
}
}int main()
{
setlocale(LC_ALL, "ru");ArrFill('.'); Player player(size_x/2, size_y/2, '$'); Enemy DrowRanger(5, 5, '%'); while (1) { cout << "Здоровье: " << player.HP << endl; ShowScene(); Move(player); system("cls"); } return 0;
}
-
if (i == this->point_x && k == this->point_y) { world[i][k] = skin; // break; <-- } else { world[i][k] = '.'; }