# Silly Car Game in Unity's Console

Hello everyone!

I didn’t finish the 2D Course before, so I decided to give it another go but this time I wanted to challenge myself by not just doing the project, but doing another project based on the original project. For this particular section, Console Number Wizard, I decided to create an entirely different game that can be played in Unity’s console window!

This was actually quite challenging because I had to totally change my mind on how to make games, here is the result!

The challenge was the console itself! I had to clear the console everytime something moved and put everything back in the correct place! I was getting tired, so the code is kinda messy and I still have to fix a tiny bug, but I’ll post it here so you guys can play it or simply check it out.

``````    const string space = "                                        ";
int playerPosition = 1;
int obstaclePosition;
bool endGame;
float timer;
List<int> linesData = new List<int>();

private void Start()
{
for (int i = 0; i < 5; i++)
{
}

PrintNewWorldLine();
PrintWorld();
PlaceInteractable(1, "▲");
}

private void Update()
{
if (endGame) { return; }

if (Input.GetKeyDown(KeyCode.RightArrow))
{
playerPosition = Mathf.Clamp(playerPosition + 1, 0, 2);
PrintAll();
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
playerPosition = Mathf.Clamp(playerPosition - 1, 0, 2);
PrintAll();
}

timer += Time.deltaTime;
if (timer >= 0.75f)
{
timer = 0;
PrintAll();
}
}

private void PrintAll()
{
ClearConsole();
PrintNewWorldLine();
PrintWorld();
PlaceInteractable(playerPosition, "▲");
}

private void PrintNewWorldLine()
{
int chanceToObstacle = Random.Range(0, 6);
obstaclePosition = 4;

if (chanceToObstacle >= 3)
{
obstaclePosition = Random.Range(0, 3);
}

PlaceInteractable(obstaclePosition, "█");
}

private void PrintWorld()
{
linesData.Reverse();

for (int i = 0; i < 3; i++)
{
PlaceInteractable(linesData[i], "█");
}

linesData.Reverse();

linesData.Remove(linesData[0]);
}

private void PlaceInteractable(int obstaclePosition, string type)
{
string line = space;

for (int i = 0; i < 3; i++)
{
string toAdd = i == obstaclePosition ? type : "   ";

if (type == "▲")
{
if (obstaclePosition == linesData[0] && i == obstaclePosition)
{
endGame = true;
}
}

}

Debug.Log(line);

if (endGame)
{ Debug.Log(space + "You lost!"); }
}

private void ClearConsole()
{
var assembly = Assembly.GetAssembly(typeof(SceneView));
var type = assembly.GetType("UnityEditor.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
``````