Skip to content

Instantly share code, notes, and snippets.

@HunvejBeen
Created November 18, 2024 14:06
Show Gist options
  • Save HunvejBeen/5680310724690626816da07fc934ccf3 to your computer and use it in GitHub Desktop.
Save HunvejBeen/5680310724690626816da07fc934ccf3 to your computer and use it in GitHub Desktop.
using System;
namespace DZ_5
{
internal class Program
{
static void Main(string[] args)
{
Console.CursorVisible = false;
PlayGame();
}
static void PlayGame()
{
const ConsoleKey CommandUpArrow = ConsoleKey.UpArrow;
const ConsoleKey CommandDownArrow = ConsoleKey.DownArrow;
const ConsoleKey CommandLeftArrow = ConsoleKey.LeftArrow;
const ConsoleKey CommandRightArrow = ConsoleKey.RightArrow;
int height = 20;
int length = 40;
int userCoordinatesX = 1;
int userCoordinatesY = 1;
char user = '@';
char wall = '#';
char[,] map = null;
bool isOpen = true;
map = CreateMap(wall, height, length);
while (isOpen)
{
PrintMap(map);
Console.SetCursorPosition(userCoordinatesY, userCoordinatesX);
Console.Write(user);
ConsoleKeyInfo charKey = Console.ReadKey();
switch (charKey.Key)
{
case CommandUpArrow:
if (map[userCoordinatesX - 1, userCoordinatesY] != wall)
{
userCoordinatesX--;
}
break;
case CommandDownArrow:
if (map[userCoordinatesX + 1, userCoordinatesY] != wall)
{
userCoordinatesX++;
}
break;
case CommandLeftArrow:
if (map[userCoordinatesX, userCoordinatesY - 1] != wall)
{
userCoordinatesY--;
}
break;
case CommandRightArrow:
if (map[userCoordinatesX, userCoordinatesY + 1] != wall)
{
userCoordinatesY++;
}
break;
}
Console.Clear();
}
}
static void PrintMap(char[,] map)
{
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write(map[i, j]);
}
Console.WriteLine();
}
}
static char[,] CreateMap(char wall, int height, int length)
{
char[,] map = new char[height, length];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
if((i == 0 || i == height - 1) || (j == 0 || j == length - 1))
{
map[i, j] = wall;
}
else
{
map[i, j] = ' ';
}
}
}
return map;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment