The problem is located under the Draw function, where I use the system()
command.
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
bool gameover;
const int width = 60;
const int height = 30;
int x, y, FruitX, FruitY;
enum edirection { Stop = 0, Left, Right, Up, Down, };
edirection dir;
void Setup()
{
gameover = true;
dir = Stop;
x = width / 2;
y = height / 2;
FruitX = rand() % width;
FruitY = rand() % height;
}
void Draw()
{
system("cls"); //Clears Screen, but is not working!!!
//Top Line
for (int i = 0; i <= width; i++)
cout << "#";
cout << endl;
//Side Lines
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0 || j == width - 1)
cout << "#";
if (i == y && j == x)
cout << "<";
else if (i == FruitY && j == FruitX)
cout << "@";
else if (j > 0 || j != width - 1)
cout << " ";
} cout << endl;
}
//Bottom Line
for (int i = 0; i <= width; i++)
cout << "#";
cout << endl;
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir = Left;
break;
case 'd':
dir = Right;
break;
case 'w':
dir = Up;
break;
case 's':
dir = Down;
break;
case 'x':
gameover = true;
break;
}
}
}
void Logic()
{
switch (dir)
{
case Left:
x--;
break;
case Right:
x++;
break;
case Up:
y--;
break;
case Down:
y++;
break;
default:
break;
}
}
int main()
{
Setup();
while (!gameover);
{
Draw();
Input();
Logic();
}
}