Untitled
unknown
lua
2 years ago
3.3 kB
5
Indexable
char playerChar = '*';
ConsoleColor playerColor = ConsoleColor.Green;
char pointChar = '$';
int statPlayerPositionX = 1; //int i
int statPlayerPositionY = 1; //int j
int currentPlayerPositionX = statPlayerPositionX;
int currentPlayerPositionY = statPlayerPositionY;
int pointAmount = 0;
int targetPoints = 3;
bool isWon=false; //Victory state
char[,] map = new char[10, 10]
{
{ '#','#','#','#','#','#','#','#','#','#', },
{ '#',' ',' ',' ',' ',' ','#',' ',' ','#', },
{ '#','$',' ',' ','$',' ','#',' ',' ','#', },
{ '#',' ',' ',' ',' ',' ',' ',' ',' ','#', },
{ '#',' ',' ','#','#','#','#','$',' ','#', },
{ '#',' ','$','#',' ',' ',' ',' ',' ','#', },
{ '#',' ',' ','#',' ',' ',' ',' ',' ','#', },
{ '#',' ',' ',' ','#',' ',' ',' ',' ','#', },
{ '#',' ',' ',' ','#',' ',' ','$',' ','#', },
{ '#','#','#','#','#','#','#','#','#','#', },
};
while (true)
{
Render(map);
Input();
if(isWon)
{
Console.WriteLine();
Console.WriteLine("Congratulations! You have WON!");
break;
}
}
void Render(char[,] map)
{
Console.Clear();
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
if (i == currentPlayerPositionY && j == currentPlayerPositionX)
{
if (map[i,j]=='$')
{
pointAmount++;
map[i, j] = 'X';
}
Console.ForegroundColor = playerColor;
Console.Write(playerChar + " ");
}
else
{
if (map[i, j] == '$')
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
Console.Write(map[i, j] + " ");
}
Console.ResetColor();
}
Console.WriteLine();
}
Console.WriteLine($"Points collected: {pointAmount}/{targetPoints}");
if (pointAmount >= targetPoints)
{
Console.WriteLine("You have collected sufficient points. Please click ENTER to continue");
}
else
{
}
}
void Input()
{
switch (Console.ReadKey().Key)
{
case ConsoleKey.LeftArrow:
if (map[currentPlayerPositionY, currentPlayerPositionX - 1] != '#')
{
currentPlayerPositionX--;
}
break;
case ConsoleKey.RightArrow:
if (map[currentPlayerPositionY, currentPlayerPositionX + 1] != '#')
{
currentPlayerPositionX++;
}
break;
case ConsoleKey.UpArrow:
if (map[currentPlayerPositionY - 1, currentPlayerPositionX] != '#')
{
currentPlayerPositionY--;
}
break;
case ConsoleKey.DownArrow:
if (map[currentPlayerPositionY + 1, currentPlayerPositionX] != '#')
{
currentPlayerPositionY++;
}
break;
case ConsoleKey.Enter:
if (pointAmount>=targetPoints)
{
isWon = true;
}
break;
default:
break;
}
}Editor is loading...
Leave a Comment