Created
December 17, 2020 15:00
-
-
Save mr5z/4a813ba7b7a8a00a05b8c4c63d779498 to your computer and use it in GitHub Desktop.
Test console
This file contains hidden or 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
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace TestCsharpConsole | |
{ | |
public class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
char key = ' '; | |
int left = 1; | |
int top = 1; | |
const int width = 20; | |
const int height = 20; | |
var semaphore = new SemaphoreSlim(1); | |
var map = new int[][] | |
{ | |
new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } | |
}; | |
_ = Task.Run(() => | |
{ | |
while (true) | |
{ | |
key = Console.ReadKey(true).KeyChar; | |
semaphore.Release(); | |
} | |
}); | |
Console.CursorVisible = false; | |
while (true) | |
{ | |
await semaphore.WaitAsync(); | |
DrawMap(0, 0, width, height, map); | |
//if (Console.KeyAvailable) | |
{ | |
DrawChar(left, top, ' ', ConsoleColor.White); | |
switch (key) | |
{ | |
case 'w': | |
top = Math.Max(1, top - 1); | |
break; | |
case 's': | |
top = Math.Min(height - 2, top + 1); | |
break; | |
case 'a': | |
left = Math.Max(1, left - 1); | |
break; | |
case 'd': | |
left = Math.Min(width - 2, left + 1); | |
break; | |
} | |
DrawChar(left, top, 'X', ConsoleColor.Red); | |
} | |
} | |
} | |
private static void DrawMap(int left, int top, int widht, int height, int [][]map) | |
{ | |
for (var row = 0; row < height; ++row) | |
{ | |
for(var col = 0;col < widht; ++col) | |
{ | |
var c = map[row][col]; | |
switch (c) | |
{ | |
case 0: | |
break; | |
case 1: | |
DrawChar(left + col, top + row, 'O', ConsoleColor.White); | |
break; | |
} | |
} | |
} | |
} | |
private static void DrawChar(int left, int top, char c, ConsoleColor color) | |
{ | |
Console.ForegroundColor = color; | |
Console.SetCursorPosition(left, top); | |
Console.Write(c); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment