#include
#include
bool gameOver;
const int width = 20;
const int height = 20;
void startSetup(int* score, int* cordX, int* cordY, int* fruitX, int* fruitY)
{
score = 0;
gameOver = false;
*cordX = width / 2 - 1;
*cordY = height / 2 - 1;
*fruitX = rand() % width;
*fruitY = rand() % height;
}
void drawAll(int* cordX, int* cordY, int* fruitX, int* fruitY)
{
system("cls");
for (int i = 0; i < width + 1; i++)
{
std::cout << "#";
}
std::cout << '\n';
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0 or j == width - 1)
{
std::cout << "#" ;
}
if (*cordY == i and *cordX == j )
{
std::cout << "0";
}
else if (*fruitX == j and *fruitY == i)
{
std::cout << "F";
}
else
{
std::cout << ' ';
}
}
std::cout << '\n';
}
for (int i = 0; i < width + 1; i++)
{
std::cout << "#";
}
std::cout << '\n';
}
void control(int* x, int* y)
{
if (_kbhit())
{
auto key{ _getch() };
if (key == 'w' or key == 'W')
{
y--;
}
if (key == 's' or key == 'S')
{
y++;
}
if (key == 'a' or key == 'A')
{
x--;
}
if (key == 'd' or key == 'D')
{
x++;
}
}
}
int main()
{
int x;
int y;
int fruitX;
int fruitY;
int* ptx = &x;
int* pty = &y;
int* ptfruitX = &fruitX;
int* ptfruitY = &fruitY;
int score;
int* pscore = &score;
startSetup(pscore, ptx, pty, ptfruitX, ptfruitY);
while (gameOver != 1)
{
drawAll(ptx, pty, ptfruitX, ptfruitY);
control (ptx, pty);
}
}