Last active
April 14, 2024 13:34
-
-
Save kyzalex/854fb803837429b6341dece7b4381aaa to your computer and use it in GitHub Desktop.
Brave new world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.CursorVisible = false; | |
char[,] map = { | |
{'#','#','#','#','#','#','#','#','#','#'}, | |
{'#','.','.','#','.','.','.','.','.','#'}, | |
{'#','.','.','#','.','.','.','.','.','#'}, | |
{'#','.','.','.','.','.','.','.','.','#'}, | |
{'#','#','#','#','#','#','#','#','#','#'}, | |
}; | |
int positionPlayerX = 1; | |
int positionPlayerY = 1; | |
while (true) | |
{ | |
Console.Clear(); | |
DrawMap(map); | |
Console.SetCursorPosition(positionPlayerX, positionPlayerY); | |
Console.Write('@'); | |
ConsoleKeyInfo pressedKey = Console.ReadKey(); | |
HandleInput(pressedKey, ref positionPlayerX, ref positionPlayerY, map); | |
} | |
} | |
static void DrawMap(char[,] array) | |
{ | |
for (int i = 0; i < array.GetLength(1); i++) | |
{ | |
for (int j = 0; j < array.GetLength(0); j++) | |
{ | |
Console.Write(array[j, i]); | |
} | |
Console.WriteLine(); | |
} | |
} | |
static void HandleInput(ConsoleKeyInfo pressedKey, ref int positionPlayerX, ref int positionPlayerY, char[,] map) | |
{ | |
int[] direction = GetDirection(pressedKey); | |
int nextPositionPlaeyrX = positionPlayerX + direction[0]; | |
int nextPositionPlayerY = positionPlayerY + direction[1]; | |
char nextCell = map[nextPositionPlaeyrX, nextPositionPlayerY]; | |
if (nextCell == ' ' || nextCell == '.') | |
{ | |
positionPlayerX = nextPositionPlaeyrX; | |
positionPlayerY = nextPositionPlayerY; | |
} | |
if (nextCell == '.') | |
{ | |
map[nextPositionPlaeyrX, nextPositionPlayerY] = ' '; | |
} | |
} | |
static int[] GetDirection(ConsoleKeyInfo pressedKey) | |
{ | |
int[] direction = { 0, 0 }; | |
if (pressedKey.Key == ConsoleKey.UpArrow) | |
{ | |
direction[1] = -1; | |
} | |
else if (pressedKey.Key == ConsoleKey.DownArrow) | |
{ | |
direction[1] = 1; | |
} | |
else if (pressedKey.Key == ConsoleKey.LeftArrow) | |
{ | |
direction[0] = -1; | |
} | |
else if (pressedKey.Key == ConsoleKey.RightArrow) | |
{ | |
direction[0] = 1; | |
} | |
return direction; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment